Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.x
Mapping documents:
<class name="User" table="user">
<id name="id" column="user_id">
<generator class="IdGenerator">
<param name="sequence_name">main_sequence</param>
</generator>
</id>
<set name="responses" cascade="save-update" inverse="true" access="field">
<key column="user_id" not-null="true"/>
<one-to-many class="Response"/>
</set>
</class>
<class name="Response" table="response">
<composite-id name="id" class="Response$Identifier">
<key-property name="userId" type="long" column="user_id"/>
<key-property name="questionId" type="long" column="question_id"/>
<key-property name="answerId" type="long" column="answer_id"/>
</composite-id>
<property name="response" column="response_text" access="field"/>
<many-to-one name="question" column="question_id" class="Question" access="field" cascade="none" insert="false" update="false" />
<many-to-one name="answer" column="answer_id" class="Answer" access="field" cascade="none" insert="false" update="false"/>
<many-to-one name="user" column="user_id" class="User" access="field" cascade="none" insert="false" update="false"/>
</class>
Hi,
I'm having trouble working with a composite-id. The problem is that if I attempt to save a user object that contains a number of responses, the cascade to responses does not work. It doesn't know that the user object has been saved, so it can't create the responses successfully with a null user id.
The documentation indicates that one has to set the fields on a composite id in the application code. However, it doesn't seem possible to handle all the relevant cases.
A new user object. If in my DAO I recognize that it's a new user, i can iterate through responses, set the new user id and save them. However, if it's not a new user, I don't know how to figure out the persistance. Some responses that are in the db may need to be deleted, some added/updated . I can't execute the delete first, because then i've lost the new data as well. Even if i could detach the responses from the session, if i attempt to add any with the same key, it'll give me an error about different objects with the same key being added to the session.
I would really like hibernate to take care of this association, but unless I'm missing something, it can not. Can anyone point me in the right direction?
Just a note, adding a generic pkey to the response table is not an option.
thanks