Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Persistence: updating to different subclass entity

Reply
Thread Tools

Java Persistence: updating to different subclass entity

 
 
Harry
Guest
Posts: n/a
 
      02-05-2007
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

 
Reply With Quote
 
 
 
 
Amit Kasher
Guest
Posts: n/a
 
      02-05-2007
On Feb 5, 8:39 am, "Harry" <(E-Mail Removed)> wrote:
> 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


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

 
Reply With Quote
 
 
 
 
Harry
Guest
Posts: n/a
 
      02-07-2007
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

On Feb 5, 8:04 pm, "Amit Kasher" <(E-Mail Removed)> wrote:
> On Feb 5, 8:39 am, "Harry" <(E-Mail Removed)> wrote:
>
>
>
> > 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

>
> 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



 
Reply With Quote
 
Amit Kasher
Guest
Posts: n/a
 
      02-08-2007
On Feb 7, 9:10 am, "Harry" <(E-Mail Removed)> wrote:
> 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
>
> On Feb 5, 8:04 pm, "Amit Kasher" <(E-Mail Removed)> wrote:
>
> > On Feb 5, 8:39 am, "Harry" <(E-Mail Removed)> wrote:

>
> > > 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

>
> > 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclass of subclass Fab C++ 0 08-09-2012 09:54 AM
subclass a class in the namespace of the that subclass Trans Ruby 8 10-23-2008 07:24 AM
How to relate a SQL based entity with an Object based entity in Entity Framework markla ASP .Net 1 10-06-2008 09:42 AM
String subclass method returns subclass - bug or feature? S.Volkov Ruby 2 03-12-2006 06:46 PM
subclass has a variable that is subclass of same superclass jstorta Java 3 02-20-2006 08:42 PM



Advertisments