Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Hibernate 使用JPQL删除elementcollection时出现约束错误_Hibernate_Jpa - Fatal编程技术网

Hibernate 使用JPQL删除elementcollection时出现约束错误

Hibernate 使用JPQL删除elementcollection时出现约束错误,hibernate,jpa,Hibernate,Jpa,JPQL: 错误: delete from Session where deviceId=:deviceId and username=:username 课时班: org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ERROR: update or delete on table "edge_session" violates foreign key constraint "fkh7j6o58rfwfumainodrxptobt" on tab

JPQL:

错误:

delete from Session where deviceId=:deviceId and username=:username
课时班:

org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ERROR: update or delete on table "edge_session" violates foreign key constraint "fkh7j6o58rfwfumainodrxptobt" on table "session_contactmethods"

在JPA中有两种删除对象的方法

  • 使用EntityManager。删除(…)。这将根据需要级联 取决于级联设置
  • 使用
    批量删除
    查询。这不会级联,你基本上是在说“相信我,我知道我在做什么”

  • 您选择了后者,它尝试按照您的指示操作,但由于存在连接的数据这一明显原因而失败。使用第一个选项,或者首先从受影响的会话对象中删除相关对象

    在JPA中删除对象有两种方法

  • 使用EntityManager。删除(…)。这将根据需要级联 取决于级联设置
  • 使用
    批量删除
    查询。这不会级联,你基本上是在说“相信我,我知道我在做什么”

  • 您选择了后者,它尝试按照您的指示操作,但由于存在连接的数据这一明显原因而失败。使用第一个选项,或者首先从受影响的会话对象中删除相关对象

    Session\u contactmethods听起来像一个联接表?或者它是一个表,实际上包含ContactMethod枚举/实体?枚举列表怎么能保留下来?!他们完全依赖于主人。。。会话对象。另外,这个字段不需要“targetClass”,你有泛型来定义它是的,我现在明白了,我想可能还涉及到另一个表。不管怎样,我怎样才能让删除生效呢?它是否需要额外的级联注释。。。我以为这不是必需的?会话\u contactmethods听起来像一个联接表?或者它是一个表,实际上包含ContactMethod枚举/实体?枚举列表怎么能保留下来?!他们完全依赖于主人。。。会话对象。另外,这个字段不需要“targetClass”,你有泛型来定义它是的,我现在明白了,我想可能还涉及到另一个表。不管怎样,我怎样才能让删除生效呢?它是否需要额外的级联注释。。。我以为那不是必需的?
    @Entity
    @Table(name="EDGE_SESSION")
    public class Session {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;
    
        @ElementCollection(targetClass=ContactMethod.class)
        @Enumerated(EnumType.STRING)
        private Set<ContactMethod> contactMethods;
    
    ...
    }
    
    # \d session_contactmethods
            Table "public.session_contactmethods"
         Column     |          Type          | Modifiers
    ----------------+------------------------+-----------
     session_id     | bigint                 | not null
     contactmethods | character varying(255) |
    Foreign-key constraints:
        "fkh7j6o58rfwfumainodrxptobt" FOREIGN KEY (session_id) REFERENCES edge_session(id)
    
    # select * from session_contactmethods;
     session_id | contactmethods
    ------------+----------------
              1 | EMAIL
              1 | TELEPHONE
              2 | TELEPHONE
              2 | EMAIL
    (4 rows)