Reputation: 1469
I was learning Hibernate, where collections are used in hibernate. I know that bag in collection is used for mapping property of type Collection or list. And also difference betweeen bag and list is bag is unordered with duplicate allowed collection type, and in list we maintain the insertion order in collection.
1> But apart from this is there any other difference between this two?
2> I read in one book that,
bag is the lack of objects to be used as keys for the elements in the bag, which decreases performance when updating or deleting elements. When an element of the bag changes, Hibernate must update all of the elements since there is no way for Hibernate to find out which element has changed
do any one have any idea about this?
Upvotes: 5
Views: 15123
Reputation: 51
a) BAG
Just want to add One more point. There are two types of bags 1)Bag without id's and 2) Bag with Id's.
In Bag without Id's when you remove any element, entire bag got cleared and the elements are inserted again.
But in bag with Id's, the element which has been removed only gets removed, rest of the elements are not impacted.
@ElementCollection
@CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
@CollectionId(columns = { @Column(name = "account_user_id") }, generator = "sequence", type = @Type(type = "long"))
@Column(name = "account_provider")
private Collection<String> accountSet = new ArrayList<String>();
So if you are using bag, always try to use id bags unless you have good reason to use another.
b) List List are also of two types, lists with order and without order.
Lists without order is similar to bag without ids.
@ElementCollection
@CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
@Column(name = "account_provider")
private List<String> accountSet = new ArrayList<String>();
While in list with order, the data structure maintains an indexing order. So, if you remove one of the elements. rest of the elements got shifted automatically.
Hence, this type of list is used to maintain the order in which the elements are inserted into the list.
@ElementCollection
@OrderColumn(name="account_provider_order")
@CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
@Column(name = "account_provider")
private List<String> accountSet = new ArrayList<String>();
Also, note that although ordering is persisted in the seperate column of table. It doesn't appears on the object state, when you fetch. Hence, it is just used for internal operations.
Upvotes: 0
Reputation: 42
When you don't want insertion order capability of list but want to allow duplicate values then you can go for bag. Here you can't go for set because it doesn't allow duplicate values.
Upvotes: 0
Reputation: 2158
Your definition is correct. Bag works like a list without index (you don't know what is the order of elements), so it's similar to Set with duplicates.
The most important thing is to know that Hibernate can map your collections as a bag implicitly if you don't use index column in one-to-many relation. This may decrease the performance of delete/update statements and it's good to be aware of this.
Here you can find how it works internally: http://assarconsulting.blogspot.co.uk/2009/08/why-hibernate-does-delete-all-then-re.html
Upvotes: 4