Syex
Syex

Reputation: 1350

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.

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:

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. Somewhere after this Hibernate seems to try to call persist() to a created object.

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, ?, ?, ?, ?).

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:

<?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?

Upvotes: 1

Views: 492

Answers (1)

ams
ams

Reputation: 62652

Looks like your primary key is not being set when hibrenate saves the file. 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.

If you are using an identity column in the database you set.

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

Upvotes: 1

Related Questions