-->
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.  [ 3 posts ] 
Author Message
 Post subject: Avoid collection initialization when new children is added
PostPosted: Mon Jun 12, 2006 7:41 pm 
Newbie

Joined: Mon Jun 12, 2006 7:25 pm
Posts: 7
Hi,

My problem looks something like this:

- There is a bank account (parent) and the account has a history of transactions (children). Here is what my parent class (Account) mapping looks like:

<set name="newTransactions" inverse="true" lazy="true" cascade="save-update">
<key column="ACCOUNT_ID"/>
<one-to-many class="Transactions" />
</set>

In my child class (Transaction) mapping, here is what I have:

<many-to-one name="account"
class="Account"
column="ACCOUNT_ID"
not-null="true"/>

- If I create a new transaction, I have to go through the account object's business method. I create a new transaction object, set the parent to "Account" and add this transaction to the "Set" in the "Account". I also modify some properties in the "Account" object.

- I persist the parent object ("Account") and because of the "save-update" cascading, the new transaction is also inserted to the database.

Everything works fine. Except, when I add the new transaction to the "Set" in the Account, Hibernate initializes the collection and loads all past transactions. However, all I want to do is insert a new transaction. I don't want to retrieve past transactions.

Is it possible to define a cascading relationship from a parent to children and prevent Hibernate initialize the collection when I create new children instances? I want to define the parent-child relationship only between new children and the parent and I don't care about the existing children in the db.

I appreciate any help.

Thanks,
Mehmet


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 9:25 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
1. You could have a look at this:

http://www.hibernate.org/hib_docs/v3/re ... entinverse


2. If you just want to add new instances, you could also try this:

After the object is fetched do the following:

bankaccount.setNewTransactions(new HashSet()); // replace whatever hibernate has set with a new instance of the collection.

bankaccount.getNewTransactions().add(newTransaction);

or simply add your new transaction in a new Set and set it by calling
bankaccount.setNewTransactions.

Keep lazy=true for the 'newTransactions', which you already have.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 1:25 pm 
Newbie

Joined: Mon Jun 12, 2006 7:25 pm
Posts: 7
Thanks, that link was useful and I'll replace set with a bag.

Thanks,
Mehmet


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