Hibernate 包与 idbag
Hibernate bag vs idbag
我研究了 bag 和 idbag 之间的区别,我了解它们之间的区别,但我想了解的是,如果使用 bag 而不是 idbag 或 vice,是否会出现出现问题的情况反之亦然。谁能举个例子解释一下。
Bag
是无序的 collection,并且与 idbag
不同,它不使用额外的列来存储每个元素索引。
不存在 bag
有效而 idbag
无效的用例。唯一的区别是它们的效率:
- Bags are the most efficient inverse collection 但它们在单向 one-to-many 关联方面表现不佳:
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.
idbag
是遗留的休眠映射,它们用于提供 more efficient unidirectional associations alternative
您可以使用 JPA @OrderColumn 注释定义 idbag 语义:
@OneToMany(mappedBy = "department")
@OrderColumn(name = "index_id")
private List<Employee> employees;
这样您就可以同时使用 Lists and Sets 来订购 collections。
因此,请尝试坚持双向关联,因为它们是最有效的关联,并且可以更好地模拟数据库关系。如果你想要某种元素索引策略,使用@OrderColumn关联。
我研究了 bag 和 idbag 之间的区别,我了解它们之间的区别,但我想了解的是,如果使用 bag 而不是 idbag 或 vice,是否会出现出现问题的情况反之亦然。谁能举个例子解释一下。
Bag
是无序的 collection,并且与 idbag
不同,它不使用额外的列来存储每个元素索引。
不存在 bag
有效而 idbag
无效的用例。唯一的区别是它们的效率:
- Bags are the most efficient inverse collection 但它们在单向 one-to-many 关联方面表现不佳:
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.
idbag
是遗留的休眠映射,它们用于提供 more efficient unidirectional associations alternative
您可以使用 JPA @OrderColumn 注释定义 idbag 语义:
@OneToMany(mappedBy = "department")
@OrderColumn(name = "index_id")
private List<Employee> employees;
这样您就可以同时使用 Lists and Sets 来订购 collections。
因此,请尝试坚持双向关联,因为它们是最有效的关联,并且可以更好地模拟数据库关系。如果你想要某种元素索引策略,使用@OrderColumn关联。