简体   繁体   English

使用Tapestry和Hibernate的Web应用程序在调用persist()时引发异常

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

I'm trying to set up a web application using JBoss and Hibernate, but I can't get the SQL database running, encountering several problems. 我正在尝试使用JBoss和Hibernate设置Web应用程序,但是我无法运行SQL数据库,遇到了一些问题。

My main problem is that when calling persist() Hibernate tries to insert an empty object into my table, the log is starting with this exception: 我的主要问题是,在调用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)

After using Google I couldn't really figure out how to solve it, but my application continues running. 使用Google后,我无法真正解决该问题,但我的应用程序仍在运行。 Somewhere after this Hibernate seems to try to call persist() to a created object. 在此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, ?, ?, ?, ?)

Using Logger I saw that this Person object is correctly created, but as you can see Hibernate sees the values as (null, ?, ?, ?, ?). 使用Logger,我看到此Person对象已正确创建,但是如您所见,Hibernate将值视为(null,?,?,?,?)。

After this this exception is thrown: 之后,抛出此异常:

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

So then, my persistence.xml is: 因此,我的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>

The relevant code is: 相关代码为:

@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;
}

My entity looks like: 我的实体看起来像:

@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;
}

So I'm not an expert with all this, someone sees some shit in there? 所以我不是这一切的专家,有人看到里面有东西吗?

Looks like your primary key is not being set when hibrenate saves the file. 看起来在Hibrenate保存文件时未设置您的主键。 the @Id column in your @Entity should specify a primary key generation strategy, rather than leaving @GeneratedValue without any paramaters and having hibernate trying to pick a default generation startegy. @Entity中的@Id列应指定主键生成策略,而不是将@GeneratedValue保留为没有任何参数并且让休眠状态尝试选择默认的生成策略。

If you are using an identity column in the database you set. 如果您在数据库中使用标识列,则进行设置。

    @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