-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate: Updates all the items of a set - Cascade 'create'
PostPosted: Fri Jul 30, 2010 6:37 am 
Newbie

Joined: Sat May 01, 2010 12:31 am
Posts: 2
Hi All,

I am using following data model (changed to make it simpler).

Table: STUDENT, TEACHER, BOOKS_ASSOCIATION, BOOKS_DETAILS.

Student and Teacher have a column whose forign key is BOOKS_ASSC_ID, hbm mapping as following

Code:
           <many-to-one name="bookAssociation" class="com.example.BookAssociation" not-null="false" fetch="join" cascade="all" >
              <column name="BOOK_ASSC_ID" />
           </many-to-one>


Books Association Mapping is as following:

Code:
    <hibernate-mapping>   
       <class name="com.example.BookAssociation" table="BOOK_ASSC">
          <id name="id" type="long" column="BOOK_ASSC_ID">
             <generator class="sequence">
                <param name="sequence">BOOK_ASSC_SEQ</param>
             </generator>
          </id>
   
          <set table="BOOKS_DETAILS" name="booksDetails" cascade="save-update" lazy="false" inverse="true">
             <key column="BOOK_ASSC_ID" />
             <one-to-many class="com.example.BOOKS_DETAILS" />
          </set>
       </class>   
    </hibernate-mapping>


and the BOOK_DETAILS table has a coulmn refering to BOOKS_ASSOCIATION table.

**The general idea is, the main entity(student,teacher, or anybody else) can have many books. Thus they are having ref to an association key which in turns refer to book details table.**

Why there is no direct one to many mapping between main entity (student, teacher, anybody else) is because, books can be issued to any of the main entity. Thus, in order to maintain the forign key reference in BOOK_DETAILS table, this is done.


Now, the problem: Suppose, a Student has issued five books already. Now if I add one more book to the booksDetails set and update the Student object, hibernate runs one insert statement (which is fine), but also runs 6 update statements considering that I have not updated anything in the rest 5 books. I dont want to update the previous items in the association set.

Is there any way to do so? Please also note that I can live with, never updating the BOOK_DETAILS from BOOKS_ASSC or parent entity (STUDENT, TEACHER, etc). I just want two operatonsL: 1) retrieve and 2) add into the bookDetails set.

I tried using cascade="create", but this is not supported. Can anybody please suggest the solution to the problem?


Top
 Profile  
 
 Post subject: Re: Hibernate: Updates all the items of a set - Cascade 'create'
PostPosted: Thu Dec 16, 2010 3:12 am 
Newbie

Joined: Thu Dec 16, 2010 3:07 am
Posts: 5
Hi, Sandeep! I really appreciate you for having come up with something like this! However I really do not know as to what the purpose is that you are trying to do here! Or in simple terms, I should say, I do not know what you are trying to do here. Perhaps, you could explain to me as to what you are intending to do with the whole Student, Teacher and Books thing. May be if I understand that, I could understand this better!

_________________
Stereolithography


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.