Java InheritanceType.JOINED的条件api限制
我遇到了以下问题-我有一个InheritanceType.JOINED的MappedSuperClass实体,以及一些扩展该超类的实体。当我试图用CriteriaAPI查询这个实体时,我得到了一个奇怪的结果集,它只包含一个子实体 守则如下: 基本父类:Java InheritanceType.JOINED的条件api限制,java,sql,hibernate,hibernate-criteria,Java,Sql,Hibernate,Hibernate Criteria,我遇到了以下问题-我有一个InheritanceType.JOINED的MappedSuperClass实体,以及一些扩展该超类的实体。当我试图用CriteriaAPI查询这个实体时,我得到了一个奇怪的结果集,它只包含一个子实体 守则如下: 基本父类: @Entity @Inheritance(strategy = InheritanceType.JOINED) public class Parent { @Id private long id; /** getters
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Parent
{
@Id
private long id;
/** getters and setters */
}
映射的超类,其中包含共享属性:
@MappedSuperclass
public class Attributes extends Parent
{
@Column
private String name;
/** getters and setters */
}
第一个子实体:
@Entity
@Table(name = "classA")
public class SubClassA extends Attributes
{
@Column
private String lastName;
/** getters and setters */
}
@Entity
@Table(name = "classB")
public class SubClassB extends Attributes
{
@Column
private String secondName;
/** getters and setters */
}
第二子实体:
@Entity
@Table(name = "classA")
public class SubClassA extends Attributes
{
@Column
private String lastName;
/** getters and setters */
}
@Entity
@Table(name = "classB")
public class SubClassB extends Attributes
{
@Column
private String secondName;
/** getters and setters */
}
以及准则:
Criteria criteria = session.createCriteria(Parent.class);
Criterion restrictions = Restrictions.ilike("name", "n%");
Junction conditionGroup = Restrictions.conjunction();
conditionGroup.add(restrictions);
criteria.add(conditionGroup);
因此,我得到了以下sql:
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 ?)
正如我所看到的,除了“where”子句,它只包括这个_1_.name,它是classA的别名,而不是classB的别名,选择几乎是我所期望的。两个表(classA和classB)都用数据填充,但是,结果集只包含classA表中的数据
其他观察结果表明,如果我将InheritanceType更改为TABLE_PER_类,结果将是正确的,因为它使用union:
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 ?)
因此,长话短说,有一个问题-有没有办法对InheritanceType.JOINED进行限制查询,它将覆盖所有子类?比如:
where
( (lower(this_1_.name) like ?) or
(lower(this_2_.name) like ?)
)
提前感谢您的回复 你解决你的问题了吗?@svlada,没有,我不得不为那个任务更改映射。