Hibernate允许你只说".id“。假设我有:
@Entity public class Customer {
@Id private Integer customerId;
@Basic private String customerName;
// getters and setters
}我想通过ID获取客户的名称:
SELECT cust.customerName FROM Customer cust WHERE cust.id = :customerId请注意,我将"id“而不是"customerId”作为快捷方式。这是有效的JPQL,还是只在Hibernate中有效?
发布于 2010-06-22 03:50:03
JPA规范没有定义这样的快捷方式,对状态字段(包括Id属性)的访问是按名称进行的:
4.3抽象模式类型和查询域
(...)
非正式地,实体的抽象模式类型可以被描述如下:
(...)
换句话说,这是Hibernate特有的(参见14.5. Referring to identifier property小节),所以如果您想编写JPQL,请不要依赖它。
例如,使用EclipseLink会得到以下结果(MyEntity没有id字段):
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Error compiling the query [select e from MyEntity e where e.id = :id], line 1, column 33: unknown state or association field [id] of class [com.acme.MyEntity].https://stackoverflow.com/questions/3086487
复制相似问题