我正在尝试使用 JBoss 和 Hibernate 设置 Web 应用程序,但我无法运行 SQL 数据库,遇到了几个问题。
我的主要问题是,当调用persist() Hibernate 尝试将一个空对象插入我的表中时,日志以这个异常开始:
12:39:26,985 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000228: Running hbm2ddl schema update
12:39:26,986 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000102: Fetching database metadata
12:39:26,987 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] (http--127.0.0.1-8080-1) HHH000319: Could not get database metadata: java.sql.SQLException: You cannot set autocommit during a managed transaction!
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:888)
使用 Google 后,我真的不知道如何解决它,但我的应用程序继续运行。在此之后的某个地方,Hibernate 似乎尝试将persist()调用到创建的对象。
12:39:27,202 INFO [stdout] (http--127.0.0.1-8080-1) Hibernate:
12:39:27,202 INFO [stdout] (http--127.0.0.1-8080-1) insert
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) into
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) Person
12:39:27,203 INFO [stdout] (http--127.0.0.1-8080-1) (id, birthdate, gender, name, password)
12:39:27,204 INFO [stdout] (http--127.0.0.1-8080-1) values
12:39:27,204 INFO [stdout] (http--127.0.0.1-8080-1) (null, ?, ?, ?, ?)
使用 Logger 我看到这个Person对象是正确创建的,但是你可以看到 Hibernate 将值视为 (null, ?, ?, ?, ?)。
在此之后抛出此异常:
12:39:27,218 ERROR [org.apache.tapestry5.ioc.Registry] (http--127.0.0.1-8080-1) org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10031 table: PERSON column: ID
那么,我的 persistence.xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="facePlace">
<non-jta-data-source>java:jboss/facePlace</non-jta-data-source>
<class>webtech2.faceplace.entities.Person</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
相关代码为:
@Inject
@Persistence
EntityManager em;
public boolean signUp(String name,
String password,
String repeatPassword,
Date birthdate,
String gender) {
if (!password.equals(repeatPassword)) {
return false;
}
log.info("person data: " + name + " " + password + " " + repeatPassword + " " + birthdate.toString() + " " + gender);
String saltedPassword = hashText + password;
String hashedPassword = generateHash(saltedPassword);
em.getTransaction().begin();
Person xperson = new Person(name, hashedPassword, birthdate, gender);
em.persist(xperson);
em.getTransaction().commit();
return true;
}
我的实体看起来像:
@Entity
public class Person implements Serializable {
private String name;
private Date birthdate;
private long id;
private String password;
private String gender;
private Set<Person> friends;
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
所以我不是这方面的专家,有人看到里面有什么东西吗?