Hibernate包vs idbag [英] Hibernate bag vs idbag
问题描述
我看了一个包和idbag之间的区别,我理解它们之间的区别,但是我想明白的是,如果使用bag而不是idbag,或者反之,情况可能会出现问题。有人可以用一个例子来解释这一点。
Bag 是一个无序集合,而且,与 idbag
不同,它不使用额外的列来存储每个元素索引。
有没有这样的事情,一个包
可以工作,而一个 idbag
不会。唯一的区别是他们的效率:
- 手袋是最有效的逆向收集器,但它们在单向一对多关联方面表现不佳:
手袋是最差的情况,因为它们允许重复的元素值
,并且因为它们没有索引列,不能定义主键。
Hibernate无法区分重复的行。
Hibernate通过彻底删除单个
DELETE来解决这个问题,并在其更改时重新创建集合。这可能是
低效。
-
idbag
是传统的hibernate映射,它们用于提供更高效的单向关联替代方法
您可以定义使用JPA的idbag语义 @OrderColumn 注释:
@OneToMany(mappedBy =department)
@OrderColumn(name =index_id)
private列表与LT;员工>雇员;
这样,您可以同时使用 I have looked at the difference between a bag and idbag and I understand the difference between them but what I am trying to understand is would there be a scenario where things could go wrong if bag is used instead of idbag or vice versa. Can some one explain this with an example. There's no such thing as a use case where a
Bags are the worst case since they permit duplicate element values
and, as they have no index column, no primary key can be defined.
Hibernate has no way of distinguishing between duplicate rows.
Hibernate resolves this problem by completely removing in a single
DELETE and recreating the collection whenever it changes. This can be
inefficient.
You can define an idbag semantics using the JPA @OrderColumn annotation: This way you can use both Lists and Sets for ordering collections. So, try to stick to bidirectional associations as they are the most efficient ones and they mimic the database relations better. If you want a certain element index strategy, use the @OrderColumn association. 这篇关于Hibernate包vs idbag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Bag
is an unordered collection, and, unlike idbag
, it doesn't use an extra column for storing each element index.bag
would work and an idbag
would not. The only difference is their efficiency:
idbag
are a legacy hibernate mapping and they were used to provide a more efficient unidirectional associations alternative@OneToMany(mappedBy = "department")
@OrderColumn(name = "index_id")
private List<Employee> employees;