Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为CollectionFements创建自定义查询_Java_Hibernate_Orm_Jpa - Fatal编程技术网

Java 如何为CollectionFements创建自定义查询

Java 如何为CollectionFements创建自定义查询,java,hibernate,orm,jpa,Java,Hibernate,Orm,Jpa,我在创建自定义查询时遇到问题 这是什么样的实体: @Entity public class ApplicationProcess { @CollectionOfElements private Set<Template> defaultTemplates; //more fields } ApplicationProcess\u defaultTemplates未映射[更新ApplicationProcess\u defaultTemplates t se

我在创建自定义查询时遇到问题

这是什么样的实体:

@Entity
public class ApplicationProcess {

    @CollectionOfElements
    private Set<Template> defaultTemplates;
    //more fields
}
ApplicationProcess\u defaultTemplates未映射[更新ApplicationProcess\u defaultTemplates t set t.used=true,其中t.ApplicationProcess.id=:apId]

我已经试过了

 int v = entityManager.createQuery("update Template t set t.used = true " + "WHERE t.applicationProcess.id=:apId").setParameter("apId", ap.getId())
   .executeUpdate();
同样的错误:

模板未映射[更新模板t set t.used=true,其中t.applicationProcess.id=:apId]

有什么想法吗

更新

我通过创建本机查询修复了它

int v = entityManager.createNativeQuery("update ApplicationProcess_defaultTemplates t set t.used=true where t.ApplicationProcess_id=:apId").setParameter("apId", ap.getId()).executeUpdate();

您想在hql中执行的操作是不可能的:因为嵌入对象不是实体,所以它们不能出现在hql查询的from子句中。要更新该对象,您需要它位于from子句中

所以你有两种可能:

  • 或者使用sql查询(注意了解sql更新对当前会话实体的影响(缺少)
  • 将模板类重构为实体

    • 我认为你做不到。Hibernate只允许对实体进行更新,不允许嵌入或映射超类。要访问模板,您需要加入,但HQL更新中不允许加入。以下是来自的相关限制:

      没有连接(隐式或显式), 可以在批量HQL查询中指定。 子查询可用于 where子句,其中子查询 它们本身可能包含连接


      您必须使用本机SQL或检索并更改java代码中的字段值。

      Embeddedables无法直接查询(批量或非批量),它们不是独立的对象,因为它们没有
      Id
      (并且将backref设置为所属类将无法实现)

      通过
      EntityManager
      更新所属对象,或者将您的
      模板
      设置为实体

       int v = entityManager.createQuery("update Template t set t.used = true " + "WHERE t.applicationProcess.id=:apId").setParameter("apId", ap.getId())
         .executeUpdate();
      
      int v = entityManager.createNativeQuery("update ApplicationProcess_defaultTemplates t set t.used=true where t.ApplicationProcess_id=:apId").setParameter("apId", ap.getId()).executeUpdate();