Hibernate包vs idbag [英] Hibernate bag vs idbag

查看:114
本文介绍了Hibernate包vs idbag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一个包和idbag之间的区别,我理解它们之间的区别,但是我想明白的是,如果使用bag而不是idbag,或者反之,情况可能会出现问题。有人可以用一个例子来解释这一点。

解决方案

Bag 是一个无序集合,而且,与 idbag 不同,它不使用额外的列来存储每个元素索引。



有没有这样的事情,一个可以工作,而一个 idbag 不会。唯一的区别是他们的效率:


  1. 手袋是最有效的逆向收集器,但它们在单向一对多关联方面表现不佳:




手袋是最差的情况,因为它们允许重复的元素值
,并且因为它们没有索引列,不能定义主键。
Hibernate无法区分重复的行。
Hibernate通过彻底删除单个
DELETE来解决这个问题,并在其更改时重新创建集合。这可能是
低效。





  1. 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.

Bag is an unordered collection, and, unlike idbag, it doesn't use an extra column for storing each element index.

There's no such thing as a use case where a bag would work and an idbag would not. The only difference is their efficiency:

  1. Bags are the most efficient inverse collection but they perform poorly for unidirectional one-to-many associations:

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.

  1. idbag are a legacy hibernate mapping and they were used to provide a more efficient unidirectional associations alternative

You can define an idbag semantics using the JPA @OrderColumn annotation:

@OneToMany(mappedBy = "department")
@OrderColumn(name = "index_id")
private List<Employee> employees;

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屋!

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