Java Persistence: updating to different subclass entity

Discussion in 'Java' started by Harry, Feb 5, 2007.

  1. Harry

    Harry Guest

    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
     
    Harry, Feb 5, 2007
    #1
    1. Advertising

  2. Harry

    Amit Kasher Guest

    On Feb 5, 8:39 am, "Harry" <> 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
     
    Amit Kasher, Feb 5, 2007
    #2
    1. Advertising

  3. Harry

    Harry Guest

    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" <> wrote:
    > On Feb 5, 8:39 am, "Harry" <> 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
     
    Harry, Feb 7, 2007
    #3
  4. Harry

    Amit Kasher Guest

    On Feb 7, 9:10 am, "Harry" <> 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" <> wrote:
    >
    > > On Feb 5, 8:39 am, "Harry" <> 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
     
    Amit Kasher, Feb 8, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. javaguy44
    Replies:
    10
    Views:
    985
    Michael Berg
    May 18, 2004
  2. jstorta
    Replies:
    3
    Views:
    447
    jstorta
    Feb 20, 2006
  3. Kenneth P. Turvey

    Java Persistence API and persistence.xml

    Kenneth P. Turvey, Mar 15, 2008, in forum: Java
    Replies:
    2
    Views:
    17,338
    Kenneth P. Turvey
    Mar 16, 2008
  4. markla
    Replies:
    1
    Views:
    552
    Steven Cheng
    Oct 6, 2008
  5. S.Volkov
    Replies:
    2
    Views:
    221
    S.Volkov
    Mar 12, 2006
Loading...

Share This Page