【发布时间】:2016-03-10 08:48:29
【问题描述】:
怎么了
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
}
更新
【问题讨论】:
-
不确定,但您是否尝试过分步创建别名?先说 main.id=x1 然后 x1.branch= x2 等等。?
-
@Forkmohit 现在,我做到了。恕我直言,main.id 是嵌入的,因此不需要别名,但我已经尝试过了。我也尝试为 main.id only 和 whatelse 创建别名。
-
你能试试
Restrictions.eq("b1.owner.id", user.id().intValue())吗?您能否也分享整个生成的 SQL 和错误消息。 -
你是不是缺少
Branch类中Use owner;[sic] 字段的映射? -
你使用的是什么休眠版本?使用问题中的注释类,我什至无法创建 SessionFactory。我收到以下异常:INFO:HHH000400:使用方言:org.hibernate.dialect.HSQLDialect org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1499) 的线程“main”java.lang.NullPointerException .hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)