Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Java Persistence: updating to different subclass entity

Thread view: 
Harry - 05 Feb 2007 06:39 GMT
Dear,

I have three entities, having a class hierarchy like this:

               Person
                   / \
       Teacher    Student

I have implemented these three entities using "Separate Table per
Subclass" approach (ie. strategy=InheritanceType.JOINED), and all
works well including inserting, updating and removing.

Now, I have made an entity instance for Teacher, and I want to change
it from Teacher to Student. Given that I guess the PK cannot be
changed, as it's the key for joining other tables. How should I do
that?

1. should I directly update using merge(), or
2. should I put the information to a new Student object, and use
persist(), or
3. should I remove the instance from Teacher first

Thanks,
Harry
Amit Kasher - 05 Feb 2007 12:04 GMT
> Dear,
>
[quoted text clipped - 20 lines]
> Thanks,
> Harry

Hi,
I'm not 100% sure of that, but as far as I understand this is not
possible (using standard methods). The reason is that you also cannot
change the runtime type of a Java object (casting only changes the
compile-type). I would try and workaround this not by hacking it, but
by, for example, creating a standard new Student object and link this
student to the teacher from which it was created (another field in
Student: @something Teacher createdFromTeacher).

Amit
Harry - 07 Feb 2007 07:10 GMT
Amit,

I agree with your point that Java itself cannot change the runtime
type of object.

So, what you mean is to create a reference indicating the history of
the record, and need to link to the old instance for all old
references.

Harry

> > Dear,
>
[quoted text clipped - 31 lines]
>
> Amit
Amit Kasher - 08 Feb 2007 12:42 GMT
> Amit,
>
[quoted text clipped - 42 lines]
>
> > Amit

Yes this is what I meant. I actually encountered this issue in the
past and this is the solution I came up with. I had the entities
Client and User (A client can sometime decide to register to the app,
and then he "becomes" a User. The underlying implementation created a
new User object which references the Client object this way: @OneToOne
private final Client creatingClient).

If you (or anyone) come up with a better solution, I'd be happy to be
updated. I'm monitoring this post.

Amit


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.