Hello everyone !
I'm a bit new to hibernate and have a problem for mapping a list. Here is some description :
Problem description :
When I try to save an object (
WordEN) that contains a Set with only one element (
WordFormEN) inside it, the foreign key linking this element with the container (
WordEN) is NULL, what brings up a "integrity constraint violation message from server".
In our project we use an object factory (
BOMClassManager) that generates the POJO that will be used afterwards (
WordEN,
WordFormEN,...). I guess the problem comes from here but I'm not sure and most of all, I don't know how to solve that problem. I must also say that in our project we also define our own Interceptor. I'm not quite sure whether this is a good strategy or not.
But enough presentation here are the technical details ;)
Hibernate version : 3.0.5
Mapping documents :
WordEN
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="eserve">
<class name="WordEN" table="WORDS_EN">
<id name="key" column="id">
<generator class="assigned"/>
</id>
<property name="noPartWordMatch" column="no_part_word_match"/>
<property name="title"/>
<set name="wordForms" inverse="true" table="WORD_FORMS_EN" cascade="all-delete-orphan">
<key column="WORD_KEY"/>
<one-to-many class="WordFormEN"/>
</set>
</class>
</hibernate-mapping>
WordFormENCode:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="eserve">
<class name="WordFormEN" table="WORD_FORMS_EN">
<id name="id">
<generator class="assigned"/>
</id>
<property name="form"/>
<many-to-one name="word_key" class="WordEN"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close() :Code:
// Get the object factory from where it is initialized
BOMClassManager factory = bomRepository.getBOMClassManager();
// Initialize the instance of the container and the content.
InstanceIntf instanceWord = factory.getClassIntf("WordEN").createInstance();
InstanceIntf instanceWordForm = factory.getClassIntf("WordFormEN").createInstance();
try
{
// Initialize the common attributes of the both classes
instanceWordForm.setAttrValue("form","some form");
instanceWord.setAttrValue("title","word");
// Initialize the list and add the WordForm to the Word
Set words = (Set)instanceWord.getAttrValue("wordForms");
words.add(instanceWordForm);
instanceWord.setAttrValue("wordForms",words);
// Persist the entire Word in the database
Session s = this.hibernateRepository.currentSession();
Transaction tx = s.beginTransaction();
s.save(instanceWord);
tx.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
Full stack trace of any exception that occurs :Code:
DEBUG - Could not execute JDBC batch update [insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)]
java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Column 'word_key' cannot be null"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at eserve.components.hibernate.HibernateComponent.doStart(HibernateComponent.java:83)
at eserve.framework.stdimpl.component.StdAbstractComponent.start(StdAbstractComponent.java:190)
at eserve.framework.stdimpl.component.StdApplicationComponent$StartVisitor.visit(StdApplicationComponent.java:563)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:365)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:372)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:360)
at eserve.framework.stdimpl.component.StdApplicationComponent._start(StdApplicationComponent.java:274)
at eserve.framework.stdimpl.component.StdApplicationComponent.start(StdApplicationComponent.java:312)
at eserve.plugin.EservePlugin$EserveStartThread.run(EservePlugin.java:414)
WARN - SQL Error: 1048, SQLState: 23000
ERROR - Duplicate key or integrity constraint violation message from server: "Column 'word_key' cannot be null"
ERROR - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
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.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at eserve.components.hibernate.HibernateComponent.doStart(HibernateComponent.java:83)
at eserve.framework.stdimpl.component.StdAbstractComponent.start(StdAbstractComponent.java:190)
at eserve.framework.stdimpl.component.StdApplicationComponent$StartVisitor.visit(StdApplicationComponent.java:563)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:365)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:372)
at eserve.framework.stdimpl.component.StdApplicationComponent.visit(StdApplicationComponent.java:360)
at eserve.framework.stdimpl.component.StdApplicationComponent._start(StdApplicationComponent.java:274)
at eserve.framework.stdimpl.component.StdApplicationComponent.start(StdApplicationComponent.java:312)
at eserve.plugin.EservePlugin$EserveStartThread.run(EservePlugin.java:414)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Column 'word_key' cannot be null"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 16 more
Name and version of the database you are using : MySQL version : 5.0.12-beta-nt-max
The generated SQL (show_sql=true) :Code:
Hibernate: select wordformen_.id, wordformen_.form as form38_, wordformen_.word_key as word3_38_ from WORD_FORMS_EN wordformen_ where wordformen_.id=?
Hibernate: insert into WORDS_EN (no_part_word_match, title, id) values (?, ?, ?)
Hibernate: insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)
Debug level Hibernate log excerpt :Code:
DEBUG - Static SQL for entity: eserve.WordFormEN
DEBUG - Version select: select id from WORD_FORMS_EN where id =?
DEBUG - Snapshot select: select wordformen_.id, wordformen_.form as form38_, wordformen_.word_key as word3_38_ from WORD_FORMS_EN wordformen_ where wordformen_.id=?
DEBUG - Insert 0: insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)
DEBUG - Update 0: update WORD_FORMS_EN set form=?, word_key=? where id=?
DEBUG - Delete 0: delete from WORD_FORMS_EN where id=?
DEBUG - Static SQL for entity: eserve.WordEN
DEBUG - Version select: select id from WORDS_EN where id =?
DEBUG - Snapshot select: select worden_.id, worden_.no_part_word_match as no2_36_, worden_.title as title36_ from WORDS_EN worden_ where worden_.id=?
DEBUG - Insert 0: insert into WORDS_EN (no_part_word_match, title, id) values (?, ?, ?)
DEBUG - Update 0: update WORDS_EN set no_part_word_match=?, title=? where id=?
DEBUG - Delete 0: delete from WORDS_EN where id=?
DEBUG - Static SQL for collection: eserve.WordEN.wordForms
DEBUG - Row insert: update WORD_FORMS_EN set WORD_KEY=? where id=?
DEBUG - Row delete: update WORD_FORMS_EN set WORD_KEY=null where WORD_KEY=? and id=?
DEBUG - One-shot delete: update WORD_FORMS_EN set WORD_KEY=null where WORD_KEY=?
DEBUG - Static select for entity eserve.WordFormEN: select wordformen0_.id as id0_, wordformen0_.form as form38_0_, wordformen0_.word_key as word3_38_0_ from WORD_FORMS_EN wordformen0_ where wordformen0_.id=?
DEBUG - Static select for entity eserve.WordFormEN: select wordformen0_.id as id0_, wordformen0_.form as form38_0_, wordformen0_.word_key as word3_38_0_ from WORD_FORMS_EN wordformen0_ where wordformen0_.id=?
DEBUG - Static select for entity eserve.WordFormEN: select wordformen0_.id as id0_, wordformen0_.form as form38_0_, wordformen0_.word_key as word3_38_0_ from WORD_FORMS_EN wordformen0_ where wordformen0_.id=? for update
DEBUG - Static select for entity eserve.WordFormEN: select wordformen0_.id as id0_, wordformen0_.form as form38_0_, wordformen0_.word_key as word3_38_0_ from WORD_FORMS_EN wordformen0_ where wordformen0_.id=? for update
DEBUG - Static select for entity eserve.WordEN: select worden0_.id as id0_, worden0_.no_part_word_match as no2_36_0_, worden0_.title as title36_0_ from WORDS_EN worden0_ where worden0_.id=?
DEBUG - Static select for entity eserve.WordEN: select worden0_.id as id0_, worden0_.no_part_word_match as no2_36_0_, worden0_.title as title36_0_ from WORDS_EN worden0_ where worden0_.id=?
DEBUG - Static select for entity eserve.WordEN: select worden0_.id as id0_, worden0_.no_part_word_match as no2_36_0_, worden0_.title as title36_0_ from WORDS_EN worden0_ where worden0_.id=? for update
DEBUG - Static select for entity eserve.WordEN: select worden0_.id as id0_, worden0_.no_part_word_match as no2_36_0_, worden0_.title as title36_0_ from WORDS_EN worden0_ where worden0_.id=? for update
DEBUG - Static select for one-to-many eserve.WordEN.wordForms: select wordforms0_.WORD_KEY as WORD4_1_, wordforms0_.id as id1_, wordforms0_.id as id0_, wordforms0_.form as form38_0_, wordforms0_.word_key as word3_38_0_ from WORD_FORMS_EN wordforms0_ where wordforms0_.WORD_KEY=?
DEBUG - initializing class SessionFactoryObjectFactory
DEBUG - registered: 2c9082d20712670d010712671f680000 (unnamed)
INFO - Not binding factory to JNDI, no JNDI name configured
DEBUG - instantiated session factory
INFO - Checking 0 named queries
DEBUG - opened session at timestamp: 4628009562173440
DEBUG - closing session
DEBUG - opened session at timestamp: 4628009562685440
DEBUG - begin
DEBUG - opening JDBC connection
DEBUG - total checked-out connections: 0
DEBUG - using pooled JDBC connection, pool size: 0
DEBUG - current autocommit status: false
DEBUG - saving transient instance
DEBUG - generated identifier: 00400276217010000000f9a04b6ea0da, using strategy: org.hibernate.id.Assigned
DEBUG - saving [eserve.WordEN#00400276217010000000f9a04b6ea0da]
DEBUG - processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - done processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - Wrapped collection in role: eserve.WordEN.wordForms
DEBUG - processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - cascade ACTION_SAVE_UPDATE for collection: eserve.WordEN.wordForms
DEBUG - cascading to saveOrUpdate: eserve.WordFormEN
DEBUG - id unsaved-value strategy UNDEFINED
DEBUG - Getting current persistent state for: [eserve.WordFormEN#10400276217010000000f9a04b6ea0da]
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - select wordformen_.id, wordformen_.form as form38_, wordformen_.word_key as word3_38_ from WORD_FORMS_EN wordformen_ where wordformen_.id=?
Hibernate: select wordformen_.id, wordformen_.form as form38_, wordformen_.word_key as word3_38_ from WORD_FORMS_EN wordformen_ where wordformen_.id=?
DEBUG - preparing statement
DEBUG - running Session.finalize()
DEBUG - binding '10400276217010000000f9a04b6ea0da' to parameter: 1
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - transient instance of: eserve.WordFormEN
DEBUG - saving transient instance
DEBUG - generated identifier: 10400276217010000000f9a04b6ea0da, using strategy: org.hibernate.id.Assigned
DEBUG - saving [eserve.WordFormEN#10400276217010000000f9a04b6ea0da]
DEBUG - done cascade ACTION_SAVE_UPDATE for collection: eserve.WordEN.wordForms
DEBUG - deleting orphans for collection: eserve.WordEN.wordForms
DEBUG - done deleting orphans for collection: eserve.WordEN.wordForms
DEBUG - done processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - commit
DEBUG - automatically flushing session
DEBUG - flushing session
DEBUG - processing flush-time cascades
DEBUG - processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - cascade ACTION_SAVE_UPDATE for collection: eserve.WordEN.wordForms
DEBUG - cascading to saveOrUpdate: eserve.WordFormEN
DEBUG - persistent instance of: eserve.WordFormEN
DEBUG - ignoring persistent instance
DEBUG - object already associated with session: [eserve.WordFormEN#10400276217010000000f9a04b6ea0da]
DEBUG - done cascade ACTION_SAVE_UPDATE for collection: eserve.WordEN.wordForms
DEBUG - deleting orphans for collection: eserve.WordEN.wordForms
DEBUG - done deleting orphans for collection: eserve.WordEN.wordForms
DEBUG - done processing cascade ACTION_SAVE_UPDATE for: eserve.WordEN
DEBUG - dirty checking collections
DEBUG - Flushing entities and processing referenced collections
DEBUG - Collection found: [eserve.WordEN.wordForms#00400276217010000000f9a04b6ea0da], was: [<unreferenced>] (initialized)
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 2 insertions, 0 updates, 0 deletions to 2 objects
DEBUG - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
DEBUG - listing entities:
DEBUG - eserve.WordEN{key=00400276217010000000f9a04b6ea0da, title=word, wordForms=[eserve.WordFormEN#10400276217010000000f9a04b6ea0da], noPartWordMatch=false}
DEBUG - eserve.WordFormEN{form=some form, word_key=null, id=10400276217010000000f9a04b6ea0da}
DEBUG - executing flush
DEBUG - Inserting entity: [eserve.WordEN#00400276217010000000f9a04b6ea0da]
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - insert into WORDS_EN (no_part_word_match, title, id) values (?, ?, ?)
Hibernate: insert into WORDS_EN (no_part_word_match, title, id) values (?, ?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [eserve.WordEN#00400276217010000000f9a04b6ea0da]
DEBUG - binding 'false' to parameter: 1
DEBUG - binding 'word' to parameter: 2
DEBUG - binding '00400276217010000000f9a04b6ea0da' to parameter: 3
DEBUG - Adding to batch
DEBUG - Inserting entity: [eserve.WordFormEN#10400276217010000000f9a04b6ea0da]
DEBUG - Executing batch size: 1
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG - insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)
Hibernate: insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)
DEBUG - preparing statement
DEBUG - Dehydrating entity: [eserve.WordFormEN#10400276217010000000f9a04b6ea0da]
DEBUG - binding 'some form' to parameter: 1
DEBUG - binding null to parameter: 2
DEBUG - binding '10400276217010000000f9a04b6ea0da' to parameter: 3
DEBUG - Adding to batch
DEBUG - Executing batch size: 1
DEBUG - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG - closing statement
DEBUG - Could not execute JDBC batch update [insert into WORD_FORMS_EN (form, word_key, id) values (?, ?, ?)]
And then comes the exception and the stack trace.
I have been strugling with this problem for more than a week now, I have read the documentation and searched the wiki, but wherever I look, I can't find an answer.
Thank you for your patience and in advance for your reply.
Regards,
Joel.