Java Persistence: updating to different subclass entity

H

Harry

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
 
A

Amit Kasher

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
 
H

Harry

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
 
A

Amit Kasher

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top