![]() |
Java Persistence: updating to different subclass entity
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 |
Re: Java Persistence: updating to different subclass entity
On Feb 5, 8:39 am, "Harry" <harrywo...@gmail.com> 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 |
Re: Java Persistence: updating to different subclass entity
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" <amitkas...@gmail.com> wrote: > On Feb 5, 8:39 am, "Harry" <harrywo...@gmail.com> 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 |
Re: Java Persistence: updating to different subclass entity
On Feb 7, 9:10 am, "Harry" <harrywo...@gmail.com> 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" <amitkas...@gmail.com> wrote: > > > On Feb 5, 8:39 am, "Harry" <harrywo...@gmail.com> 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 |
| All times are GMT. The time now is 01:44 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.