在 JPQL 请求中使用新语句
Using new statement in JPQL request
我在 JPQL 查询中使用 new
,如下所示
select
ob.property1,
NEW package1.CustomObject(item, dimension, material, product)
from mainTable ob
LEFT JOIN ....
应用程序在 ob.property1
后抱怨昏迷说这是一个意外的标记。似乎您不能在 select 子句中使用带有 new
的多列。你能帮帮我吗
根据this,您有以下选择:
- 将值包装在类型安全的 Java 对象中,该对象将 return 编辑为查询结果。
select new package1.CustomObject(item, dimension, material, product)
from MainTable ...
The projection class must be fully qualified in the entity query, and it must define a matching constructor. The class here need not be mapped. It can be a DTO class. If it does represent an entity, the resulting instances are returned in the NEW state (not managed!).
HQL 支持额外的 "dynamic instantiation" 功能。
- 查询可以为标量结果指定 return
List
而不是 Object[]
。
select new list(item, dimension, material, product)
from MainTable ...
The results from this query will be a List<List>
as opposed to a List<Object[]>
.
- 将标量包装成
Map
select new map(
item as iName,
dimension as iDimension,
material as iMaterial,
product as iProduct)
from MainTable ...
The results from this query will be a List<Map<String, Object>>
as opposed to a List<Object[]>
. The keys of the map are defined by the aliases given to the select expressions. If the user doesn’t assign aliases, the key will be the index of each particular result set column (e.g. 0, 1, 2, etc).
我在 JPQL 查询中使用 new
,如下所示
select
ob.property1,
NEW package1.CustomObject(item, dimension, material, product)
from mainTable ob
LEFT JOIN ....
应用程序在 ob.property1
后抱怨昏迷说这是一个意外的标记。似乎您不能在 select 子句中使用带有 new
的多列。你能帮帮我吗
根据this,您有以下选择:
- 将值包装在类型安全的 Java 对象中,该对象将 return 编辑为查询结果。
select new package1.CustomObject(item, dimension, material, product)
from MainTable ...
The projection class must be fully qualified in the entity query, and it must define a matching constructor. The class here need not be mapped. It can be a DTO class. If it does represent an entity, the resulting instances are returned in the NEW state (not managed!).
HQL 支持额外的 "dynamic instantiation" 功能。
- 查询可以为标量结果指定 return
List
而不是Object[]
。
select new list(item, dimension, material, product)
from MainTable ...
The results from this query will be a
List<List>
as opposed to aList<Object[]>
.
- 将标量包装成
Map
select new map(
item as iName,
dimension as iDimension,
material as iMaterial,
product as iProduct)
from MainTable ...
The results from this query will be a
List<Map<String, Object>>
as opposed to aList<Object[]>
. The keys of the map are defined by the aliases given to the select expressions. If the user doesn’t assign aliases, the key will be the index of each particular result set column (e.g. 0, 1, 2, etc).