使用Hibernate值类型集合产生的并发问题和解决方法
Hibernate的值类型集合用起来很方便,但在最近的使用中发现它有并发问题。如下:
有一个持久化类A需要包括n个有序的long型数。类A需要保存到数据库中。
类A如下:
public class Foo{ // some properties List longNumbers=new ArrayList(); // some propertes' getter and setter public List getLongNumbers(){ return this.longNumbers; } public void setLongNumber(List longNubmers){ this.longNumbers=longNumbers; }}
<list name="longNumbers" table="itil_foolongnumbers" lazy="false" inverse="false" cascade="all-delete-orphan" > <key column="ifooid" > </key> <index column="iindex" /> <element column="ilongnumber" type="java.lang.Long" not-null="false" unique="false" /> </list>
public void appdendLongNumber(Long fooId,Long num){ Transaction tr=session.beginTransaction(); Foo f=session.get(fooId,Foo.class); f.getLongNumbers().add(num); tr.commit(); ...}