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 inherited object
PostPosted: Thu Feb 11, 2010 8:21 am 
Newbie

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

I have:

Code:
@Entity
@Table(name="persons")
@Inheritance(strategy=InheritanceType.JOINED)
class Person {
   long id;
   String name;
   ...
}

@Entity
@Table(name="fiancees")
class Fiancee extends Person {
   boolean isWoman;
   ...
}

@Entity
@Table(name="fiancee_pairs")
class FianceePair {
   Fiancee fianceeHe = new Fiancee();
   Fiancee fianceeShe = new Fiancee();

   @ManyToOne(optional=true)
   @JoinColumn(name="fiancee_he_id", nullable=true)
   @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE,CascadeType.PERSIST})
   public Person getFianceeHe() {
   ...

   @ManyToOne(optional=true)
   @JoinColumn(name="fiancee_he_id", nullable=true)
   @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE,CascadeType.PERSIST})
   public Person getFianceeShe() {
        ...
}


I have a form in which I set some fields of FianceePair. Firstly in my form controller I am loading objects of Person class and make corresponding object of Fiancee class, which have fields just like the Person objects (also the id). Notice that I have Person records in the database but I do not have Fiancee records in the database.
In the form I am setting isWoman field of Fiancee objects.
When I am submitting the form I can see in my onSubmit method that I have a FianceePair object with two Fiancee objects with some id (the id from exisiting Person objects). Now I would like to save the FianceePair in the way that new records of Fiancee class will be created in the database with Person records remain unchanged.

The getHibernateTemplate().merge(fp) method of my FianceeDaoHibernate ends with creating Fiancee records as well as new Person records - and these new Person records are undesirable for me.

So before the operation I have two records in my persons table: (1, 'Alice') (2, 'Peter')
After the operation I have four records in my persons table: (1, 'Alice') (2, 'Peter') (3, 'Alice') (4, 'Peter') and two records in fiancees table (3, 1) (4, 0)

Could someone point me to the right direction?
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.