Greetings! I'm stuck with the following problem - i have a MappedSuperClass entity with InheritanceType.JOINED, and some entities, extending that superclass. And when i try to query this entity with criteria api, i'm getting strange result set, which contains only one of sub entities.
The code as follows:
Base parent class:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent
{
@Id
private long id;
/** getters and setters */
}
Mapped superclass, which contains shared attribute:
Code:
@MappedSuperclass
public class Attributes extends Parent
{
@Column
private String name;
/** getters and setters */
}
First subentity:
Code:
@Entity
@Table(name = "classA")
public class SubClassA extends Attributes
{
@Column
private String lastName;
/** getters and setters */
}
Second subentity:
Code:
@Entity
@Table(name = "classB")
public class SubClassB extends Attributes
{
@Column
private String secondName;
/** getters and setters */
}
And the criteria:
Code:
Criteria criteria = session.createCriteria(Parent.class);
Criterion restrictions = Restrictions.ilike("name", "n%");
Junction conditionGroup = Restrictions.conjunction();
conditionGroup.add(restrictions);
criteria.add(conditionGroup);
As a result, i've got the following sql:
Code:
select
this_.id as id1_0_0_,
this_1_.name as name1_1_0_,
this_1_.lastName as lastName2_1_0_,
this_2_.name as name1_2_0_,
this_2_.secondName as secondNa2_2_0_,
case
when this_1_.id is not null then 1
when this_2_.id is not null then 2
when this_.id is not null then 0
end as clazz_0_
from Parent this_
left outer join classA this_1_ on this_.id=this_1_.id
left outer join classB this_2_ on this_.id=this_2_.id
where
(lower(this_1_.name) like ?)
As i can see, the selection is nearly i've expected, except the "where" clause, which includes only this_1_.name, which is alias for classA, but not classB. Both tables (classA and classB) are filled with data, but, the result set contains data only from classA table.
Other observation shows, that, if i change InheritanceType to TABLE_PER_CLASS, the result would be correct, because it uses union:
Code:
select
this_.id as id1_0_0_,
this_.name as name1_1_0_,
this_.lastName as lastName2_1_0_,
this_.name as name1_2_0_,
this_.secondName as secondNa2_2_0_,
this_.clazz_ as clazz_0_
from
(
select
id,
cast(null as varchar(100)) as name,
cast(null as varchar(100)) as lastName,
cast(null as varchar(100)) as secondName,
0 as clazz_
from Parent
union all
select
id,
name,
lastName,
cast(null as varchar(100)) as secondName,
1 as clazz_
from classA
union all select
id,
name,
cast(null as varchar(100)) as lastName,
secondName,
2 as clazz_ from classB
) this_ where (lower(this_.name) like ?)
So, to make a long story short, the there is a question - is there a way to make restriction queries for InheritanceType.JOINED, which would cover ALL subclasses? Something like:
Code:
where
( (lower(this_1_.name) like ?) or
(lower(this_2_.name) like ?)
)
Thanks in advance for your replies!