I am faced with what, in my opinion, is a simple problem with Hibernate, but I cannot solve it (Hibernate forums are not available, of course, they do not help).
I have a simple class that I would like to keep, but keep getting:
SEVERE: Field 'id' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [hibtest.model.Mensagem] at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) [ a bunch more ] Caused by: java.sql.SQLException: Field 'id' doesn't have a default value [ a bunch more ]
The corresponding code for the persistent class is:
package hibtest.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Mensagem { protected Long id; protected Mensagem() { } @Id @GeneratedValue public Long getId() { return id; } public Mensagem setId(Long id) { this.id = id; return this; } }
And the actual current code is simple:
SessionFactory factory = new AnnotationConfiguration() .configure() .buildSessionFactory(); { Session session = factory.openSession(); Transaction tx = session.beginTransaction(); Mensagem msg = new Mensagem("YARR!"); session.save(msg); tx.commit(); session.close(); }
I tried some “strategies” in the GeneratedValue annotation, but it just doesn't work. Initializing id doesn't help either! (e.g. Long id = 20L ).
Can anyone shed some light?
EDIT 2: confirmed: messing with @GeneratedValue(strategy = GenerationType.XXX) does not solve it
SOLVED: re-creating the database solved the problem
java hibernate jpa persistence
André Chalella Apr 29 '09 at 22:13 2009-04-29 22:13
source share