我是否可以通过名称“id”引用 JPQL 中任何实体的主键,而不管实体的 ID 属性名称如何?

发布于 2024-09-06 03:37:00 字数 385 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛起拖把扫天下 2024-09-13 03:37:00

JPA 规范没有定义这样的快捷方式,对状态字段(包括 Id 属性)的访问是通过名称:

4.3 抽象架构类型和查询域

(...)

通俗地说,抽象模式类型
实体的特征可以表示为
如下:

  • 对于每个持久字段
    或获取访问器方法(对于
    实体的持久属性)
    类,有一个字段
    (“状态字段”)其抽象模式
    类型与字段的类型相对应
    或访问器的结果类型
    方法。
  • 对于每一个坚持
    关系字段或获取访问器
    方法(对于持久关系
    实体类的属性),有
    是一个字段(“关联字段”),其
    type 是抽象模式类型
    相关实体(或者,如果
    关系是一对多或
    多对多,此类的集合)。

(...)

换句话说,这是 Hibernate 特定的(请参阅 14.5. 引用标识符属性),因此如果您想编写 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].

The JPA specification doesn't define such a shortcut, access to state fields (including an Id property) is by name:

4.3 Abstract Schema Types and Query Domains

(...)

Informally, the abstract schema type
of an entity can be characterized as
follows:

  • For every persistent field
    or get accessor method (for a
    persistent property) of the entity
    class, there is a field
    (“state-field”) whose abstract schema
    type corresponds to that of the field
    or the result type of the accessor
    method.
  • For every persistent
    relationship field or get accessor
    method (for a persistent relationship
    property) of the entity class, there
    is a field (“association-field”) whose
    type is the abstract schema type of
    the related entity (or, if the
    relationship is a one-to-many or
    many-to-many, a collection of such).

(...)

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 an id field):

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].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文