-->
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.  [ 2 posts ] 
Author Message
 Post subject: Is mixing of inheritance strategies possible?
PostPosted: Wed Mar 11, 2009 1:24 pm 
Newbie

Joined: Wed Mar 11, 2009 12:59 pm
Posts: 1
Hello,

I have a problem with inheritance strategies, namely, I would like to mix two types across one class hierarchy.

The class hierarchy is as follows:
Code:
Participant
+-----Person
         +------Visitor

Between Participant and Person, I would like to use the JOINED strategy, and between Person and Visitor, the SINGLE_TABLE strategy.

I have set up the entities with following annotations:


Participant class:
Code:
@Entity
@Table( name  = "participant" )
@Inheritance(strategy = InheritanceType.JOINED)
public class Participant implements Serializable
{
   protected Integer participantId;
        ...   
   @Id
   @Column( name = "participant_id", unique = true, nullable = false )
   @GeneratedValue( strategy = GenerationType.IDENTITY )
   public Integer getParticipantId()
   {
      return this.participantId;
   }

   public void setParticipantId(Integer participantId)
   {
      this.participantId = participantId;
   }
...
}


Person class:
Code:
@Entity
@Table( name  = "person" )
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="person_type", discriminatorType=DiscriminatorType.STRING)
public class Person extends Participant implements Serializable
{
...
}


Visitor class:
Code:
@Entity
@Table(name="person")
@DiscriminatorValue(value="V")
public class Visitor extends Person implements Serializable
{
...
}


The database is set up correctly using hbm2ddl param, but when I'm trying to persist something, I get the following exception:
Code:
javax.el.ELException: javax.ejb.EJBException: nested exception is: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert: [entities.Visitor]
...


And the SQL behind:
Code:
Hibernate:
    insert
    into
        participant
        (participant_comments, participant_login, participant_password, participant_phone, participant_status, participant_website)
    values
        (?, ?, ?, ?, ?, ?)
Hibernate:
    select
        currval('participant_participant_id_seq')
Hibernate:
    insert
    into
        person
        (person_birth_date, person_first_name, person_gender, person_last_name, person_middle_name, person_mobile, person_occupation, person_title, participant_id)
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
    insert
    into
        person
        (participant_id)
    values
        (?)
ERROR [org.hibernate.util.JDBCExceptionReporter] ERROR: null value in column "person_first_name" violates not-null constraint


As I can see, Hibernate tries to insert the Visitor instance to the person table twice: 1st time with all params, and 2nd time only with participant_id. It seems, that it is trying to use the JOINED strategy on this table and not the SINGLE_TABLE one.
The question is: why, and it is actually possible to override the inheritance strategy in the middle of the class hierarchy (in this case in the Person class, which is subclass of Participant and superclass of Visitor). [/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 4:17 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Afaik hibernate states that you should select inheritance strategy one time for particular hierarchy and follow it all the time. The only exception is that particular subclass may be joined. I already posted an example about that.

However, there is definitely no general inheritance strategy overriding rule.


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

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.