-->
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: How to persist object that is associated with persisted obj?
PostPosted: Wed May 20, 2009 8:39 am 
Beginner
Beginner

Joined: Wed Nov 12, 2008 12:07 pm
Posts: 21
Hello

I have a class Client that has a ManyToMany relation with Address.
I'm trying to persist a client1 object that has an address1 and after that persist client2 that has the same address1.

So hibernate creates one row to client1, one to client2 and two rows for address1.

I want it to create one row for the address1. I'm using @ManyToMany(mappedby="address") and I'm persisting they in the same transaction.

What is wrong?


Top
 Profile  
 
 Post subject: Re: How to persist object that is associated with persisted obj?
PostPosted: Thu May 21, 2009 2:26 pm 
Beginner
Beginner

Joined: Wed Nov 12, 2008 12:07 pm
Posts: 21
Please help me on this:

The problem presented before happens because when I try to persist the same object using a list it will create many rows in the database:
Code:
       for(T entity : entities){
            entity = (T) getSession().merge(entity);
       }


This has a different behaviour of persisting it x times:
Code:
            entity = (T) getSession().merge(entity);
            entity = (T) getSession().merge(entity);
             ...

This will only produce one row.
Using the list, I saw that after executing the merge the entity does not receive the return of the merge, and that's why it persists the entity many times. But why does this happen?


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.