The problem:
I am trying to save an object.
I don't really know know how the generators work.
As far as I understood there's no need for me to set the id.
The generator will take care of that.
I tried it with other generators (sequence,hilo,...) but it won't work.
The sql-code
Code:
insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)
tells me there is a 'null' value to be inserted. ok i can understand that this will not work since pk values must be != null. But even when I initialise the property id with a value e.g. new Long(0) it will always insert a 'null'.
What about the other values they all are mapped with a question mark. Why does the code not show the real values I set before?
I tried to find sth. in the docs and forum about how id are generated and used. but I could not find anything that talks about null values, other initial values in detail.
Thx in advance...
The beanThe pojo later in the code...
Code:
public class ContactEntry {
public final static int UNKNOWN_SEX = 0;
// the primary key
private Long id;
private String organisation = null;
private String firstName = null;
private String lastName = null;
private String middleName = null;
private int gender = UNKNOWN_SEX;
//...simple getter and setter
}
Hibernate version: 3.0rc1
Mapping documents:Code:
<hibernate-mapping>
<class name="de.greyshine.telab.model.ContactEntry" table="contact_entrys">
<id name="id" column="id" type="long">
<generator class="identity"/>
</id>
<property name="organisation" type="string"/>
<property name="lastName" type="string"/>
<property name="firstName" type="string"/>
<property name="middleName" type="string"/>
<property name="gender" type="integer"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():pojo is the object to be stored
Code:
Session session = getSession( );
Transaction tx = session.beginTransaction( );
DbModulException dme = null;
try {
session.save( pojo );
tx.commit( );
// Log.log( "STORED: " + pojo );
} catch ( HibernateException e ) {
tx.rollback( );
// Log.log( "Storing failed: " + e.getMessage( ) );
dme = new DbModulException( "Add failed: " + pojo, e );
}
Full stack trace of any exception that occurs:
Quote:
org.hibernate.exception.GenericJDBCException: could not insert: [de.greyshine.telab.model.ContactEntry]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1747)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:34)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:238)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:429)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:424)
at de.greyshine.telab.database.HibernateModul.add(HibernateModul.java:177)
... 26 more
Caused by: java.sql.SQLException: Try to insert null into a non-nullable column: column: ID table: CONTACT_ENTRYS in statement [insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)]
at org.hsqldb.jdbc.jdbcUtil.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1737)
... 40 more
Name and version of the database you are using:
hsqldb 1.7
The generated SQL (show_sql=true):
insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)
Debug level Hibernate log excerpt:
11:00:35,209 WARN JDBCExceptionReporter:57 - SQL Error: -10, SQLState: 23000
11:00:35,219 ERROR JDBCExceptionReporter:58 - Try to insert null into a non-nullable column: column: ID table: CONTACT_ENTRYS in statement [insert into contact_entrys (organisation, lastName, firstName, middleName, gender, id) values (?, ?, ?, ?, ?, null)]
11:00:35,410 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:hsqldb:file:resources/db/telabdb