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]