-->
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: Only generate @Id when id is null?
PostPosted: Mon Feb 16, 2009 3:50 pm 
Newbie

Joined: Thu Dec 11, 2008 7:10 pm
Posts: 4
Hello,

I'm using JPA w/Hibernate. I have a User entity and multiple subclass entities like Customer, Admin, etc. I need the ability to convert from a Customer to an Admin. The way I've tried to do this is create a new Admin instance, copy all properties over including the Id of the Customer, delete the Customer entity and persist the Admin.

With a little tweaking (using getReference() before the remove and flush() after the remove) I have it all working fine.

The problem, however, is that the new entity I am saving has a newly generated Id. I need it to have the Id of the entity I just deleted so that many foreign key references remain valid.

The behavior I think I'm looking for is something like: if id=null, generate as normal. Otherwise use the Id that's been set.

Ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 3:08 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I dont think there is a straight forward way to do this. Plz see:
http://forum.hibernate.org/viewtopic.ph ... 2b04766891

But I could think of a solution. I am assuming that you are using Table per class hierarchy mapping. What you can do is map the same table to another POJO which will include all the fields of User, Customer and Admin and the discriminator field. Now use this entity to update the discriminator field to convert your Customer to Admin.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 4:40 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I would also do something similar to littypreethkr's solution: create a native sql query where you update the discriminator column.

_________________
-----------------
Need advanced help? http://www.viada.eu


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.