我正在尝试通过子查询从B实体(有效负载)获取属性。但是,如果设置别名,则会出现以下错误:
org.hibernate.QueryException:不关联:有效负载
如果我删除别名,错误将变为:
org.hibernate.HibernateException:未知实体:null
重要信息:
类A是主要查询的类,并且类A与类C具有一对一关系。类C与类B具有一对多关系。
@Entity
@Table(name = "A")
public class A{
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "C_ID")
private C c;
}
@Entity
@Table(name = "C")
public class C{
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "entityC")
@Cascade({CascadeType.ALL})
List<B> bs;
}
@Entity
@Table(name = "B")
public class B{
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "C_ID")
private C entityC;
}
查询如下:
Criteria query = sessionFactory.getCurrentSession().createCriteria(A.class,"a")
final DetachedCriteria dc = DetachedCriteria.forClass(B.class, "b");
dc.add(Restrictions.isNotNull("b.payload"));
dc.addOrder(Order.asc("b.id"));
dc.add(Restrictions.eqProperty("b.entityC", "c"));
dc.add(Restrictions.sqlRestriction("LIMIT 1"));
dc.setProjection(Projections.property("b.entityC.id"));
query.add(Subqueries.propertyIn("c.id", dc));
...
query.createAlias("c", "c");
此查询的目的是安装一个ADTO,该ADTO具有B的有效负载的属性,如下例所示:
query.setProjection(Projections.projectionList()
.add(Projections.groupProperty("b.payload").as("payload"))
...
query.setResultTransformer(new AliasToBeanResultTransformer(ADTO.class));