-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: inheritance - saving the object od another class
PostPosted: Fri May 21, 2010 11:43 am 
Newbie

Joined: Tue Sep 18, 2007 3:30 pm
Posts: 17
Hi,

I am not sure if it is an imperfection of my architecture or it is some other problem.

I have a class Person:

Code:
@Entity
@Table(name="persons")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Person extends BaseObject { ... }


I have another class Parent:

Code:
@Entity
@Table(name="parents")
public class Parent extends Person { ... }


And finally I have a class Fiancee:

Code:
@Entity
@Table(name="fiancees")
public class Fiancee extends Person {
   public Fiancee(Person person) {
      this.setId(person.getId());      //???
      ...
   }
}


As you can see I am using "Joined subclasses" inheritance strategy.

Of course object of class Parent is not an object of class Fiancee and vice-versa. When I am creating an object of class Parent and saving it - there is a new record in my persons and parents tables.

But what if I would like my Person to be a Parent and a Fiancee? Imagine the situation: I have a Parent 'Mary'. This Person is a Parent in one form but in the other form it should be a Fiancee. So I would like to have in my database a record in persons, parents and fiancees tables with a common joining key.

Unfortunately when I load an existing Parent object and make a Fiancee object from it:

Code:
new Fiancee(parent)


and try to save this object - I get "Key (fiancee_id)=(1) does not exist in "fiancees" table".

I understand that. But when I remove the line "this.setId(person.getId());" from the Fiancee constructor I can save the object but a new Person is created in the database as well as a new Fiancee - and I do not want the new Person object created.

Could someone give me some pointers? I have read the Hibernate docs about inheritance few times but I still can not find the answer...
Best Regards,
Gandalf


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.