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