Hi,
I want to map a One-To-Many Association between the Entity Question and the Entity Options. One question can have many options and one option can only belong to one Question. The primary key of Options consists of the Option_id and the Question_id, where the latter is a foreign
key to Question.
I wrote the following two mappings for Options and Question, which generated valid DDL using the Hibernate-SchemaUpdate Tool.
<hibernate-mapping>
<class name="de.ryll.hibernate.Options"
table="Options">
<composite-id name="id"
class="de.ryll.hibernate.OptionIdentifyer">
<key-property name="option_id" column="option_id"
type="integer"/>
<key-many-to-one name="question_id" class="de.ryll.hibernate.Question" column="question_id"/>
</composite-id>
<property name="option_text" column="option_text" type="string" not-null="true"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="de.ryll.hibernate.Question"
table="Question">
<id name="question_id" column="question_id" type="integer">
<generator class="increment"/>
</id>
<property name="question_text" column="question_text" type="string" not-null="true"/>
<set name="options">
<key column = "question_id"/>
<one-to-many class="de.ryll.hibernate.Options"/>
</set>
</class>
</hibernate-mapping>
When I want to save a question along with its options like this:
session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Question q = new Question();
q.setQuestion_text("Who shall be the president?");
Set options = new HashSet();
options.add((Options) new Options(new OptionIdentifyer(new Integer(1), q), "Bush"));
options.add((Options) new Options(new OptionIdentifyer(new Integer(2), q), "Kerry"));
q.setOptions((Set)options);
session.save((Question) obj);
tx.commit();
HibernateUtil.closeSession();
...I get the following Exceptions:
21:49:28,529 INFO SettingsFactory:106 - Use JDBC3 getGeneratedKeys(): false
21:49:28,529 INFO SettingsFactory:109 - Optimize cache for minimal puts: false
21:49:28,529 INFO SettingsFactory:118 - Query language substitutions: {}
21:49:28,529 INFO SettingsFactory:129 - cache provider: net.sf.hibernate.cache.EhCacheProvider
21:49:28,559 INFO Configuration:1116 - instantiating and configuring caches
21:49:29,170 INFO SessionFactoryImpl:118 - building session factory
21:49:29,771 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2375)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at de.ryll.hibernate.test.HibTool.createExampleData(HibTool.java:219)
at de.ryll.hibernate.test.HibTool.main(HibTool.java:91)
21:49:30,002 ERROR SessionImpl:2379 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:65)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2375)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at de.ryll.hibernate.test.HibTool.createExampleData(HibTool.java:219)
at de.ryll.hibernate.test.HibTool.main(HibTool.java:91)
*************************
What is my mistake?? ARE THE HBM-MAPPINGS CORRECT??? I have not implemented equals() and
hashCode() in the class OptionsIdentifyer - may this be the problem?? If yes, how would you
implement these methods...
It would be very great if anybody helps me a little bit...THANK YOU.
|