persist()方法和save()方法对detached态对象的支持
来源:互联网 发布:pure codec for mac 编辑:程序博客网 时间:2024/06/06 00:44
persist()方法不支持detached对象
同一事务中的操作:
1.Many对象和One对象操作在同一事务中时,
One 对象为持久化对象,
Many对象可正常通过persist()存储入库
Session session1 = sf.getCurrentSession();
Transaction tran1 = session1.beginTransaction();
DvdTable1 dvd = new DvdTable1();
dvd.setName("rope71");
dvd.setDiskvolume(27);
dvd.setDatetime(new Date());
dvd.setDescription("no");
dvd.setPhoto("no");
DvdType tType = (DvdType) session1.get(DvdType.class, 15);
QualityType qType =(QualityType) session1.get(QualityType.class, 15);
dvd.setDvdType(tType);
dvd.setQualityType(qType);
session1.persist(dvd);
tran1.commit();
mysql> select * from DvdTable1;
+----+---------------------+-------------+------------+-------------+-------+------------+----------------+
| id | datetime | description | diskvolume | name | photo | dvdType_id | qualityType_id |
| 13 | 2010-06-11 21:43:28 | no | 27 | rope71 | no | 15 |
- 不管是detached形式一(session close造成),还是detached形式二(new对象 ,setId造成),
- 不管是在同一事务中(显然同一事务,只支持形式二),还是不同事务中(不同事务支持形式一和二)
同一事务中的操作:
1.Many对象和One对象操作在同一事务中时,
One 对象为持久化对象,
Many对象可正常通过persist()存储入库
mysql> select * from DvdTable1;
+----+---------------------+-------------+------------+-------------+-------+------------+----------------+
| id | datetime
| 13 | 2010-06-11 21:43:28 | no