有什么问题
session.createCriteria(Composed.class, "main")
.createAlias("main.id.branch", "b1")
.add(Restrictions.eq("b1.owner", user))
.list();
?相应的HQL工作正常
String hql = "select main from Composed as main"
+ "left join main.id.branch as b1 where b1.owner = ?";
session.createQuery(hql)
.setInteger(0, user.id().intValue())
.list();
根据条件, Hibernate不会创建任何联接并使用where b1x1_.owner_id=?
,但是在任何地方都没有b1x1_
,所以它失败了"无法准备声明"。
这些课程相当简单
@Entity class Composed {
@Id ComposedId id; // also tried @EmbeddedId
... irrelevant stuff
}
@Embeddable class ComposedId {
@ManyToOne(optional=false) Branch branch;
... irrelevant stuff
}
@Entity class Branch {
@Id Integer id;
@ManyToOne(optional=false) User owner;
... irrelevant stuff
}
@Entity class User {
@Id Integer id;
... irrelevant stuff
}
答案 0 :(得分:1)
我没有尝试,但可能会创建两个左连接别名帮助您。我的意思是:
session.createCriteria(Composed.class, "main")
.createAlias("main.id", "id1", JoinType.LEFT_OUTER_JOIN)
.createAlias("id1.branch", "b1", JoinType.LEFT_OUTER_JOIN)
.add(Restrictions.eq("b1.owner", user))
希望它有所帮助!
答案 1 :(得分:0)
由于Branch
是映射的Entity
,因此您需要加入子标准而不是别名。
session.createCriteria(Composed.class, "main")
.createCriteria("main.id.branch", "b1")
.add(Restrictions.eq("b1.owner", user))
.list();