使用元素集合时如何在JPA中执行批量删除? [英] How to do bulk delete in JPA when using Element Collections?

查看:248
本文介绍了使用元素集合时如何在JPA中执行批量删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Person 对象包含使用 @ElementCollection存储的数据时,我无法解决如何使用JPA批量删除 Person 对象

  @Entity 
@Table(name =at_person )
public class Person implements Comparable< Person> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id)
private long id = 0;

@Column(name =name,nullable = true,length = 128)
private String name =;

@ElementCollection
@Column(name =email)
@CollectionTable(name =person_email,joinColumns = @JoinColumn(name =person_id))
私人设置< String> email = new HashSet< String>();
}

目前我正在做的是这样的,键约束错误:

 查询查询= em.createQuery(DELETE FROM Person); 




导致:java.sql.SQLException:完整性约束冲突:
外键不行动; FKCEC6E942485388AB表:PERSON_EMAIL


如果它可以是纯粹的JPA注释而不是Hibernate注释,那将是一种奖励!

解决方案

我会让你解释JPA 2.0规范的一部分,它提到批量删除操作没有级联:


4.10批量更新和删除操作



... ...

...

删除操作仅适用于指定类的
实体和
其子类。 它不会级联到
相关实体

事实是,Hibernate赢得了'将级联删除到集合表中。这已在 HHH-5529 中报告,建议的方法是:


您也可以(a)自己清理集合表或(b)在模式中使用级联外键。换句话说,(a)使用本机SQL或(b)在数据库级别使用级联删除约束 - 并且您必须添加它(b

手动,我不认为你可以使用 @ElementCollection 注释(与 @OnDelete href =http://opensource.atlassian.com/projects/hibernate/browse/HHH-4301 =noreferrer> HHH-4301 IMO)。


I am having trouble working out how to do a bulk delete of a Person object using JPA, when the Person objects contain data stored using an @ElementCollection. Any ideas on how to do this would be much appreciated.

@Entity
@Table(name="at_person")
public class Person implements Comparable<Person> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id")
    private long id = 0;

    @Column(name="name", nullable = true, length = 128)
    private String name = "";

    @ElementCollection
    @Column(name = "email")
    @CollectionTable(name = "person_email", joinColumns = @JoinColumn(name = "person_id"))
    private Set<String> email = new HashSet<String>();
}

What I am doing at the moment is this, and it fails with a foreign key constraint error:

Query query=em.createQuery("DELETE FROM Person");

Caused by: java.sql.SQLException: integrity constraint violation: foreign key no action; FKCEC6E942485388AB table: PERSON_EMAIL

If it can be a pure JPA annotation rather than a Hibernate annotation that would be a bonus!

解决方案

I'll let you interpret the part of the JPA 2.0 specification that mentions that a bulk delete operation is not cascaded:

4.10 Bulk Update and Delete Operations

...

A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities.

And the fact is that Hibernate won't cascade a delete to a collection table either. This has been reported in HHH-5529 and the suggested approaches are:

You could also (a) clean up the collection table yourself or (b) use cascading foreign keys in the schema.

In other words, (a) use native SQL or (b) use a cascade delete constraint at the database level - and you'll have to add it manually, I don't think you can use @OnDelete with the @ElementCollection annotation (same story as HHH-4301 IMO).

这篇关于使用元素集合时如何在JPA中执行批量删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆