We're updating the issue view to help you get more done. 

Issue with Inheritance mapping and one to many relationship for child

Description

Assume we have PARENT and INHERITANCE_TAB tables as given below.

PARENT
------------ ID : primary key
NAME : varchar2

and

INHERITANCE_TAB
---------------------------- ID : primary key
VALUE1 : varchar2
VALUE2 : varchar2
PARENT_ID : foreign key for PARENT table
DISC : number - discriminator column
ORDER_COL – number order column

And we have classes as given below:

@Entity
@Table(name = "PARENT")
public class Parent {

@Id
@GeneratedValue
@Column(name = "ID")
Integer id;

@Column(name = "NAME")
String name;

@OneToMany( fetch= FetchType.EAGER)
@JoinColumn(name = "PARENT_ID")
@OrderColumn(name = "ORDER_C")
//@Where(clause="DISC=1")
List<AbstractChild1> child1s = new LinkedList<>();

@OneToMany( fetch= FetchType.EAGER)
@JoinColumn(name = "PARENT_ID")
@OrderColumn(name = "ORDER_C")
//@Where(clause="DISC=2")
List<AbstractChild2> child2s= new LinkedList<>();
}

@Entity
@Table(name = " INHERITANCE_TAB")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Access(AccessType.FIELD)
@DiscriminatorColumn(name = "DISC")
public class ParentHierarchy {

@Id
@GeneratedValue
@Column(name = "ID")
Integer id;
}

@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("1")
public class AbstractChild1 extends ParentHierarchy {

@Column(name = "VALUE1")
String value;
}

@Entity
@Access(AccessType.FIELD)
@DiscriminatorValue("2")
public class AbstractChild2 extends ParentHierarchy {

@Column(name = "VALUE2")
String value;
}

So as you can see data from ParentHierarchy, AbstractChild1, AbstractChild2 should be collectively saved in single table INHERITANCE_TAB table. Note that; in Parent class, @OrderColumn is same for both one to many mappings. When you persist above model; it will persist correctly. Problem occurs while fetching data. Hibernate complains WrongClassException. It tries to insert data that belongs to AbstractChild2 in AbstractChild1 object.

The cause of this error is hibernate generate following query to fetch whole object eagerly (I modified query a bit for readability sake like aliasing etc).

SELECT p.ID,
p.NAME ,
ch.PARENT_ID,
ch.ID as CHILD_ID,
ch.ORDER_COL ,
ch.ID,
ch.VALUE1
FROM A_PARENT p
LEFT OUTER JOIN INHERITANCE_TAB ch
ON p.ID = ch.PARENT_ID
WHERE p.ID=?

SELECT ch.PARENT_ID,
ch.ID,
ch.ORDER_COL,
ch.ID,
ch.VALUE
FROM INHERITANCE_TAB ch
WHERE ch.PARENT_ID=?

First query tries to fetch data for Parent, ParentHierarchy, AbstractChild1 classes and second query tries to fetch data for AbstractChild2 class. As we can see both of queries don’t have discriminator column in where clause and hence result of this query consist of data for both child classes and hence hibernate confuses and throws exception.

Environment

Oracle 11g, hibernate-3.6.0.jar, hibernate-jpa-2.0-api-1.0.0.Final.jar

Status

Assignee

Unassigned

Reporter

MohammadA

Labels

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Affects versions

2.1-ProposedFinalDraft

Priority

Major