Hibernate - @ElementCollection - 奇怪的删除/插入行为 [英] Hibernate - @ElementCollection - Strange delete/insert behavior

查看:322
本文介绍了Hibernate - @ElementCollection - 奇怪的删除/插入行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
public class Person {

@ElementCollection
@CollectionTable(name =PERSON_LOCATIONS,joinColumns = @JoinColumn( name =PERSON_ID))
私人列表<位置>位置;

[...]

}

@Embeddable
公共类位置{

[ ..]

}

考虑到以下类结构,当我尝试要将新位置添加到人员位置列表中,它总是会导致以下SQL查询:

 从人员删除其中PERSON_ID =:idOfPerson 



  A lota'插入到PERSON_LOCATIONS表中

Hibernate(3.5.x / JPA 2)删除给定Person的所有关联记录,并重新插入所有以前的记录,再加上新记录。



我有这样的想法:equals / hashcode方法位置可以解决问题,但它不会改变任何内容。



任何提示都会被赞赏!

解决方案

这个问题在页面中解释了关于J的 ElementCollection PA wikibook:


CollectionTable中的主键



JPA 2.0规范不
提供了一种方法来定义 Id
嵌入式中。然而,要删除或
更新
ElementCollection 映射的元素,通常需要一些唯一的
键。否则,每次更新JPA提供者
时,
都需要从
CollectionTable 中删除​​实体中的所有内容
,然后插入值。因此,
JPA提供程序很可能会假设
,所有$ b $嵌入式中的b字段是唯一的,
与外键
JoinColunm (一个或多个))。然而,这可能是
低效率,或者如果
很大或很复杂,则不可行。
blockquote>

这正是(粗体部分)这里发生了什么(Hibernate不会为集合表生成主键,并且无法检测到什么元素的集合已更改,并会从表格中删除旧内容以插入新内容)。 然而, if 定义一个 @OrderColumn (指定一个用于维护列表的持久顺序的列 - 这很有意义,因为您使用的是 List ),Hibernate将创建一个主键(由顺序列和连接列组成),并且能够更新集合表而不删除整个内容。



类似于此(如果您想使用默认列名称):

  @Entity 
public class Person {
...
@ElementCollection
@CollectionTable(name =PERSON_LOCATIONS,joinColumns = @JoinColumn(name =PERSON_ID))
@OrderColumn
private列表与LT;地点>位置;
...
}



参考文献




  • JPA 2.0规范


    • 第11.1.12节ElementCollection注释
    • 第11.1.39节OrderColumn注解


  • JPA Wikibook


    @Entity
    public class Person {
    
        @ElementCollection
        @CollectionTable(name = "PERSON_LOCATIONS", joinColumns = @JoinColumn(name = "PERSON_ID"))
        private List<Location> locations;
    
        [...]
    
    }
    
    @Embeddable
    public class Location {
    
        [...]
    
    }
    

    Given the following class structure, when I try to add a new location to the list of Person's Locations, it always results in the following SQL queries:

    DELETE FROM PERSON_LOCATIONS WHERE PERSON_ID = :idOfPerson
    

    And

    A lotsa' inserts into the PERSON_LOCATIONS table
    

    Hibernate (3.5.x / JPA 2) deletes all associated records for the given Person and re-inserts all previous records, plus the new one.

    I had the idea that the equals/hashcode method on Location would solve the problem, but it didn't change anything.

    Any hints are appreciated!

    解决方案

    The problem is somehow explained in the page about ElementCollection of the JPA wikibook:

    Primary keys in CollectionTable

    The JPA 2.0 specification does not provide a way to define the Id in the Embeddable. However, to delete or update a element of the ElementCollection mapping, some unique key is normally required. Otherwise, on every update the JPA provider would need to delete everything from the CollectionTable for the Entity, and then insert the values back. So, the JPA provider will most likely assume that the combination of all of the fields in the Embeddable are unique, in combination with the foreign key (JoinColunm(s)). This however could be inefficient, or just not feasible if the Embeddable is big, or complex.

    And this is exactly (the part in bold) what happens here (Hibernate doesn't generate a primary key for the collection table and has no way to detect what element of the collection changed and will delete the old content from the table to insert the new content).

    However, if you define an @OrderColumn (to specify a column used to maintain the persistent order of a list - which would make sense since you're using a List), Hibernate will create a primary key (made of the order column and the join column) and will be able to update the collection table without deleting the whole content.

    Something like this (if you want to use the default column name):

    @Entity
    public class Person {
        ...
        @ElementCollection
        @CollectionTable(name = "PERSON_LOCATIONS", joinColumns = @JoinColumn(name = "PERSON_ID"))
        @OrderColumn
        private List<Location> locations;
        ...
    }
    

    References

    这篇关于Hibernate - @ElementCollection - 奇怪的删除/插入行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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