我是否可以通过名称“id”引用 JPQL 中任何实体的主键,而不管实体的 ID 属性名称如何?
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 中有效?
Hibernate allows you to just say ".id". Let's say I have:
@Entity public class Customer {
@Id private Integer customerId;
@Basic private String customerName;
// getters and setters
}
And I want to get the name of a customer by ID:
SELECT cust.customerName FROM Customer cust WHERE cust.id = :customerId
Notice I put "id" rather than "customerId", as a shortcut. Is this valid JPQL, or is it only valid in Hibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JPA 规范没有定义这样的快捷方式,对状态字段(包括
Id
属性)的访问是通过名称:换句话说,这是 Hibernate 特定的(请参阅 14.5. 引用标识符属性),因此如果您想编写 JPQL,请不要依赖于此。
例如,这就是您使用 EclipseLink 得到的结果(
MyEntity
没有id
字段):The JPA specification doesn't define such a shortcut, access to state fields (including an
Id
property) is by name:In other words, this is Hibernate specific (see section 14.5. Referring to identifier property) so don't rely on this if you want to write JPQL.
This is what you'd get with EclipseLink for example (
MyEntity
doesn't have anid
field):