-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Trouble with composit-id and save
PostPosted: Thu May 18, 2006 6:36 pm 
Newbie

Joined: Wed Apr 05, 2006 5:58 pm
Posts: 13
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 7:05 pm 
Newbie

Joined: Thu May 18, 2006 10:00 am
Posts: 12
Try key-many-to-one instead of key-property:

Code:
<class name="Response" table="response">
<composite-id name="id" class="Response$Identifier">
<key-many-to-one name="user" class="User" column="user_id"/>
...
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 18, 2006 9:40 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
<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="all-delete-orphan" inverse="true">
      <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"/>

   <many-to-one name="question" column="question_id" class="Question" insert="false" update="false" />
   <many-to-one name="answer" column="answer_id" class="Answer" insert="false" update="false"/>
   <many-to-one name="user" column="user_id" class="User" insert="false" update="false"/>

</class>


Since I dont want to have access="field", below code uses setters/getters. You can replace them appropriately.

Code:
public User createUserWithThreeResponses(){
   Session session = getSession();

   // To create a new user and attach 3 responses
   User user = new User();
   createResponse( user, "My First Response" );
   createResponse( user, "My Second Response" );
   createResponse( user, "My Third Response" );

   // this should also response objects to respective table
   // apart from saving user to user table
   session.saveOrUpdate( user );
}

private void createResponse( User user, String response ){
   Response respObject = new Response();
   respObject.setUserId( some_value );
   respObject.setQuestionId( some_value );
   respObject.setAnswerId( some_value );
   respObject.setResponse( "value_response" );
   respObject.setUser( user );
   user.getResponses().add( respObject );
}



If the user & response already existed and you want add new response's, delete some then check the below code. Also I suggest you override equals() and hashCode() in Response$Identifier class to make this work.

Code:
   Session session = getSession();

   // first load the user object into session
   User user = (User) session.get( User.class, userPrimKey );

   // I want to delete response2 and add response3
   Response response2 = new Response();
   response2.setUserId( some_value );
   response2.setQuestionId( some_value );
   response2.setAnswerId( some_value );
   if ( user.getResponses().contains( response2 ) ) {
      user.getResponses().remove( response2 );
      response2.setUser( null );
   }

   // now I want to add response4
   Response response4 = new Response();
   response4.setUserId( some_value );
   response4.setQuestionId( some_value );
   response4.setAnswerId( some_value );
   response4.setResponse( "My Fourth Response" );
   if ( !user.getResponses().contains( response3 ) ) {
      user.getRespones().add( response4 );
      response4.setUser( user );
   }

   // this will update user table
   //           insert one response
   //           delete one response
   session.saveOrUpdate();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 19, 2006 10:22 am 
Newbie

Joined: Wed Apr 05, 2006 5:58 pm
Posts: 13
I apprecidate the help. I've got it working now, but it's a lot more manual work than i was hoping for. Since knowing what to delete is somewhat hard (the user is only gonna submit what they want, not all the changes) I had to write a lot of code to figure out what to remove, what to add and what to update.

I was really hoping i could clear out all the responses, and add a new set of responses and hibernate would at least know to delete all the old stuff, and add this new stuff, and even better would be to delete the stuff that isn't valid anymore and make the updates/inserts.

Oh well, at least I have something working.

thanks again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.