gpt4 book ai didi

sql - 在 JPQL 请求中使用新语句

转载 作者:行者123 更新时间:2023-12-04 10:47:28 25 4
gpt4 key购买 nike

我用 new在 JPQL 查询中,如下所示

select 
ob.property1,
NEW package1.CustomObject(item, dimension, material, product)
from mainTable ob
LEFT JOIN ....

应用程序提示 ob.property1 后昏迷说这是一个意外的 token 。似乎您不能在 new 中使用多列在 select 子句中。你能帮助我吗

最佳答案

根据 this您有以下选择:

  • 将值包装在类型安全的 Java 对象中,该对象将作为查询结果返回。
  • 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 支持额外的“动态实例化”功能。
  • 查询可以指定返回 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).

    关于sql - 在 JPQL 请求中使用新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59633796/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com