簡體   English   中英

使用Tapestry和Hibernate的Web應用程序在調用persist()時引發異常

[英]Web application using Tapestry and Hibernate throws Exception when calling persist()

我正在嘗試使用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;
}

所以我不是這一切的專家,有人看到里面有東西嗎?

看起來在Hibrenate保存文件時未設置您的主鍵。 @Entity中的@Id列應指定主鍵生成策略,而不是將@GeneratedValue保留為沒有任何參數並且讓休眠狀態嘗試選擇默認的生成策略。

如果您在數據庫中使用標識列,則進行設置。

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="pkey")
    private Integer pkey;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM