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.  [ 8 posts ] 
Author Message
 Post subject: Is copy functionality supported by Hibernate?
PostPosted: Wed Jan 19, 2005 7:39 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
Is copy functionality supported by Hibernate?

Suppose I have User, Role tables and the relation between them is many-to-many. I want to copy a User with the method

copyUser(Long userId);

This method is supposed to create a new User record with all the attributes of the passed userId. That should also copy the associations in the other tables also. ie the new user should get all the roles that the source user from which new user has been created.

Hope I am clear..Is it directly supported by Hibernate?

Thanks,
SSSS.




Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 3:04 pm 
Newbie

Joined: Thu Aug 05, 2004 11:00 pm
Posts: 7
Location: New York, NY
I don't know that it is supported directly by Hibernate (although I don't know that it isn't either). The way I tackled this problem was to override Object.clone() in my entities, and then create new entities from the existing ones just by calling myEntity.clone(). Very quick and easy, and it worked like a charm.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 19, 2005 7:44 pm 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
This is called the prototype pattern and is not really the persistence framework's responsibility.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 24, 2005 2:28 am 
Beginner
Beginner

Joined: Thu Dec 02, 2004 4:49 am
Posts: 32
don't know that it is supported directly by Hibernate (although I don't know that it isn't either). The way I tackled this problem was to override Object.clone() in my entities, and then create new entities from the existing ones just by calling myEntity.clone(). Very quick and easy, and it worked like a charm.

Can you plz give some sample on how to clone??

Thanks,
SSSS.


Top
 Profile  
 
 Post subject: Can anyone provide the sample code for this clone()?
PostPosted: Mon Jun 13, 2005 3:12 pm 
Beginner
Beginner

Joined: Tue Jun 22, 2004 3:16 pm
Posts: 35
Can anyone provide the sample code for this clone()?


Top
 Profile  
 
 Post subject: Re: Is copy functionality supported by Hibernate?
PostPosted: Tue Jun 28, 2005 8:31 am 
Beginner
Beginner

Joined: Tue Jun 21, 2005 8:38 am
Posts: 37
SSSS wrote:
Is copy functionality supported by Hibernate?

Suppose I have User, Role tables and the relation between them is many-to-many. I want to copy a User with the method

copyUser(Long userId);

This method is supposed to create a new User record with all the attributes of the passed userId. That should also copy the associations in the other tables also. ie the new user should get all the roles that the source user from which new user has been created.

Hope I am clear..Is it directly supported by Hibernate?

Thanks,
SSSS.


"beanlib" should solve this cloning problem for you: http://hansonchar.blogspot.com/2005/06/ ... rnate.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 8:51 am 
Regular
Regular

Joined: Thu Apr 29, 2004 5:08 pm
Posts: 56
Location: Montreal, Quebec, Canada
It seems alot of person here wants this feature out of hibernate. I guess you guys want hibernate to clone an object by respecting cascade semantic defined in the mapping file of that entity?

Personally, for a parent-child relationship, I found that I want clone to copy only the parent object, sometimes I want it to copy all children also.

Copying of objects for me is cleary related to your business, not persistence. I personally find clone() a bad solution since you cannot pass it parameters.

I usually create a separate class with duplicateXXXX methods that answer my need and take all the parameters I want.

Some purists may find this solution not eleguant, but as soon as you coded those duplicate methods and they work, you realize you stop wasting time trying to be clean, you are more flexible than clone, and you finally work on your real important business ;)

_________________
- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 10:02 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
In my case (and I have not fully learned everything I need to know about hibernate), I'm looking at the need to serialize my domain objects for transmission over RMI (through a session facade). This has a few implications:

- I cannot use lazy loading where I expect to transmit retrieved entities to the client
- I would prefer to avoid N+1 select situations, which is a very real problem when you turn off lazy loading (HQL queries have to be loaded with the expected data in mind to take advantage of table joins --> 'from Person p' becomes 'from Person p left join fetch p.addresses', and then create a new Set based on the returned list to remove duplicates)
- Different situations call for different parts of a retrieved object graph (i.e., I might only need the Person object, and not any of its associated addresses). Without lazy loading, this becomes much more difficult.

The best solution I can see is to allow lazy loading and then use something to create a detached transient copy of the retrieved results at some arbitrary depth. This would allow the performance benefits of lazy loading while still allowing for serializability. beanlib (below) looks promising, but at the same time, this is yet another dependency for my code. What would be great is if hibernate could support some sort of configurable copy utility -- perhaps with an associated section in the entity mapping for different... er... copy "plans".

What drives me mad about this is that I'm spending a lot of time figuring out these issues that are really communication and architecture-related, rather than focusing on the business model itself.

All that said, though, I'm still new to all of this, and there's probably a better solution out there that I just haven't run into yet.[/i]


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