开发者

In Hibernate, why Set is the recommended way to represent many-valued associations

开发者 https://www.devze.com 2023-01-10 01:03 出处:网络
Taken from here: http://docs.jboss.org/hibernate/stable/core/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode

Taken from here: http://docs.jboss.org/hibernate/stable/core/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode

I tend to use List since Criteria returns List, so it makes my code cleaner since I don't have to do conversion.

I do something like so..

@OneToMany(cascade= {CascadeType.PERSIST, CascadeType.REMOVE}, mappedBy="parent")
@Column(name="PARENT_ID")
public List<Menu> getChildMenus() {
    return childMenus;
}

If I had use Set there, somewhere in my DAO I will have to开发者_StackOverflow convert results returned by Criteria to Set first.

I wonder what the repercussion could be by using List they way I am doing.


Set is used as the child table has a primary key such that a child can only be once in a parent. If you use a list there can be duplicate children in the list and this cannot be saved to the database.

0

精彩评论

暂无评论...
验证码 换一张
取 消