1
我試圖設置一個使用JBoss和Hibernate的Web應用程序,但我無法使SQL數據庫運行,遇到幾個問題。使用Tapestry和Hibernate的Web應用程序拋出異常調用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)
使用谷歌之後,我真的不能弄清楚如何解決它,但我的應用程序繼續運行。在這之後的某個地方Hibernate似乎試圖呼叫堅持()到一個創建的對象。
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, ?, ?, ?, ?)
使用記錄器,我看到這人對象是正確創建的,但你可以看到休眠看到值(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;
}
所以我不是這一切的專家,有人認爲有些事情在那裏?