`

JPA Path Expression, operator IN and Collection properties

阅读更多

If we want to select the Orders whose price is greate than 100, we can use this JPQL query:

 

select o from Order o where o.price > 100

Here "o.price" is used to refer to the property "price" of JPA entity "Order". This expression is called "Path Expression". The path expression is an identfier variable "o" followed by the navigation operator "." and then a property or an associated property.

 

Problem is that the path expression cannot evaluate/apply to a collection. For example, if entity "Order" has a memeber of "Collection<Item> items", which maps to the items in the order, then "o.items.price" is illegal. This is because navigation to items results in a Collection, not a single Item.

 

To handle this situation, an identifier variable may be declared in the FROM clause to range over the elements of the "items" collection:

SELECT i.name
FROM Order o, IN(o.items) i
where i.price > 100

 The above query would return the item names whose price is greater than 100. It does that by searching the "items" collection of the Order entity.

 

The argument to the "IN" operator must be a collection-valued path expression., which specifies a navigation to a collection-valued association property of an entity or embedded class. The identifier "i" in the above example designates a member in the property "Collection<Item> items".

 

The above query can be better written as a JOIN statement, which is easier to understand:

SELECT i.name
FROM Order o JOIN o.items i
WHERE i.price > 100

 

For an association or collection property of java.util.Map, the identification variable (eg, "i") refers to an element of the map value. Functions KEY, VALUE and ENTRY may be used to refer to the map key, value and entry, repectively. As this example shows:

SELECT VALUE(o)
FROM Customer AS c JOIN c.orders AS o
WHERE KEY(o) >= 2 

 Here the path expression KEY(o) and VALUE(o) are the map key and map value of the orders map of the customer, respectively. The keyword VALUE is optional here, thus VALUE(o) and o are equivalent in this query.

 

Warning: queries not tested and modified from the reference just in order to illustrate the concepts.

 

Reference:

"Java Persistence with JPA" - by Mr. Daoqi Yang

 

分享到:
评论

相关推荐

    Pro JPA 2 in Java EE 8: An In-Depth Guide to Java Persistence APIs.pdf

    Learn to use the Java Persistence API (JPA) and other related APIs as found in the Java EE 8 platform from the perspective of one of the specification creators. A one-of-a-kind resource, this in-depth...

    jpa例子jpajpa

    **Java Persistence API (JPA)** 是Java平台上的一个标准,用于管理关系数据库中的对象-关系映射(ORM)。它提供了一种方式,让开发者可以用面向对象的编程模型来操作数据库,而无需直接编写SQL语句。JPA允许你在...

    Spring和openJPA集成

    Spring 和 OpenJPA 集成是企业级Java开发中常见的技术组合,主要用于构建数据持久化层。Spring 是一个强大的轻量级应用框架,而 OpenJPA 是一个开源的 Java Persistence API (JPA) 实现,它允许开发者将对象关系映射...

    Spring Data JPA 简化 JPA 开发

    Spring Data JPA 是一个由 Spring 框架提供的强大库,它极大地简化了基于 Java Persistence API (JPA) 的数据库访问。JPA 是 Java 平台上的标准 ORM(对象关系映射)规范,允许开发者使用面向对象的方式处理数据库...

    JPA教程JPA教程JPA教程

    Java Persistence API(JPA)是Java平台上的一个标准,用于管理关系数据库中的数据。它提供了一种面向对象的方式来处理数据库操作,使得开发人员可以使用Java类和对象来操作数据库记录,而无需直接编写SQL语句。JPA...

    jpa的学习---jpa demo工程

    在Java项目中,JPA的配置通常通过`pom.xml`文件引入依赖,如Spring Data JPA,然后在`application.properties`或`application.yml`文件中设置数据源、实体扫描路径、JPA供应商(如Hibernate)等相关属性。...

    Springboot中使用Druid+JPA

    2. 配置JPA:在`application.properties`或`application.yml`中设置JPA的属性,如实体扫描路径、数据库连接信息等。 3. 定义实体类:创建表示数据库表的实体类,使用@Entity注解,并用@Id标记主键字段。 4. 创建...

    JPA源文件/jpa学习

    **JPA(Java Persistence API)**是Java平台上的一个标准,用于管理关系数据库中的数据,它简化了在Java应用程序中存储、检索和管理对象的工作。JPA是Java EE和Java SE环境中的一种ORM(Object-Relational Mapping)...

    spring4 mvc + jpa demo

    &lt;properties&gt; &lt;project.build.sourceEncoding&gt;UTF-8 &lt;jdk.version&gt;1.7 &lt;spring.version&gt;4.0.1.RELEASE &lt;spring-data-jpa.version&gt;1.6.2.RELEASE&lt;/spring-data-jpa.version&gt; ...

    Pro JPA2 精通JPA2

    《Pro JPA2:精通Java™ Persistence API》是一本由Mike Keith和Merrick Schincariol撰写的关于Java持久化API(JPA)的权威指南。本书深入探讨了JPA2,即Java Persistence API的第二版,是Java EE 6标准的一部分。...

    JPA教程,包括TOPLink JPA,Hibernate JPA,Open Jpa,jpa批注

    **Java Persistence API (JPA)** 是Java平台上的一个标准,用于管理关系数据库中的数据。它为Java开发者提供了一种对象关系映射(ORM)机制,将业务对象与数据库表进行映射,使得开发者可以使用面向对象的方式来操作...

    Spring Data JPA中文文档[1.4.3]_springdatajpa_erlang_waitxpf_

    **Spring Data JPA** 是一个基于 **Java** 的开源框架,它是 **Spring Framework** 的一个模块,主要用于简化 **Java Persistence API (JPA)** 的使用。JPA 是 Java 平台上的一个标准,用于管理和持久化应用程序的...

    JPA中文解释,JPA的API

    Java Persistence API(JPA)是Java平台上的一个标准,用于管理关系数据库中的对象持久化。它简化了在Java应用程序中存储、检索和管理数据的过程,是Enterprise JavaBeans(EJB)的一部分,也是Spring框架中的一个...

    JPA环境搭建及JPA实例与JPA主键生成策略

    **JPA环境搭建** Java Persistence API(JPA)是Java平台上的一个标准,用于管理和持久化Java对象到数据库。在开始使用JPA之前,我们需要搭建一个基本的开发环境。以下是一些关键步骤: 1. **引入依赖**:如果你...

    JPA 标注 JPA标签手册

    Java Persistence API (JPA) 是Java企业版5(Java EE 5)的一部分,与Enterprise JavaBeans(EJB)3.0规范一起,极大地简化了Java持久化。它提供了一种对象关系映射方法,允许您以标准、可移植的方式声明性地定义...

    springboot -data-jpa

    它集成了大量常用的第三方库配置,如 JDBC、MongoDB、JPA、RabbitMQ、Quartz 等,使得开发者可以快速地创建一个独立运行的、生产级别的基于 Spring 的应用。 Spring Data JPA 是 Spring 基于 Java Persistence API ...

    JPA Demo 简单的了解下jpa

    **JPA(Java Persistence API)简介** Java Persistence API(JPA)是Java平台上的一个标准,用于管理和持久化Java对象到关系数据库。它是Java EE和Java SE环境中处理对象关系映射(ORM)的一种规范,旨在简化...

Global site tag (gtag.js) - Google Analytics