hibernate报错异常总结
1、A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance
Hibernate中one to many设置cascade="all"的话,那么在进行联机操作的时候,如果把一端设置成inverse="false",当进行删除的时候,删除one表记录,many一方不会删除,而是把关联字段设置成null,这样会出现孤立的many方数据!解决办法就是设置 cascade="all-delete-orphan"。然而这种简单的设置,会出现上述问题。
?
当<set name="children" inverse="true" cascade="all">时,通过parent.getChildren().remove(child1);只是使child1游离成为一个“孤儿”,并不能将child1持久化到数据库表中的记录也删除。但如果cascade="all-delete-orphan"(orphan为“孤儿”),则会将child1持久化到数据库表中的记录也删除掉。
// 酒店:hotel?? 标志物:building
// 这二者之间是一对多的关系,一个酒店对应多个标志物。
// 酒店的配置文件中设置了对标志物的 cascade="all-delete-orphan"
执行如下代码:
? TbHotelDAO hotelDAO =new TbHotelDAO();??
??TbHotel hotel = hotelDAO.findById(44);
??hotel.setHotelName("12345");
??
??TbBuilding building2 = new TbBuilding();
??building2.setTbHotel(hotel);
??building2.setDistance("234米");
??building2.setBuildingName("阿斯顿");
??
??Set tbBuildings = new HashSet();
??tbBuildings.add(building2);
??hotel.setTbBuildings(tbBuildings);?
??
??hotelDAO.update(hotel);
报出如下异常:
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:。。。。。。。。。。。
解决办法:
将代码
? Set tbBuildings = new HashSet();
??tbBuildings.add(building2);
??hotel.setTbBuildings(tbBuildings);?
修改为:
? Set tbBuildings = hotel.getTbBuildings();
??tbBuildings.clear();
??tbBuildings.add(building2);
?
设置位置:
<set name="hotelbookpaxdetails" cascade="all-delete-orphan" inverse="true" order-by="paxid"> <key> <column name="bookingno" length="20" not-null="true" /> </key> <one-to-many /> </set>
?
?
先清空Set中以前的数据,再保存新数据!
?
2、catalog
我用myEclipse开发,基本上POJO和hbm.xml的配置文件都是自动生成的,
自动生成好的配置文件里面有一个 <catalog>这个属性,默认是数据库名
我怎么看到好多资料上面建议把这个属性去掉,说可能出现问题,究竟这个属性有什么用??
下面的catalog="onlinetest"指定了数据库为onlinetest,弄得我一直在纳闷前面的hibernate.cfg.xml里的connection.url指定的databaseName怎么就不见效果了呢。看来学习上还要认真才是!
<hibernate-mapping>
??? <class name="onlinetest.Hibernate.Teacher" table="teacher" catalog="onlinetest">
??????? <id name="id" type="integer">
??????????? <column name="id" />
??????????? <generator />
??????? </id>
??????? <property name="name" type="string">
??????????? <column name="name" length="20" />
??????? </property>
??????? <property name="password" type="string">
??????????? <column name="password" length="50" not-null="true" />
??????? </property>
??? </class>
</hibernate-mapping>
?
3、?Illegal attempt to associate a collection with two open sessions;?
@Transactional(propagation = Propagation.REQUIRED)public void delete(Long id) throws SystemException {Hotel hotel = findById(id);this.getBaseDao().delete(hotel);log.info(" delete ok.");}?。。?
?
?
?