标签:hash new nat 改善 bsp cas hiberna comm cti
dept.hbm.xml:关键点标蓝色这部分,inverse表示放弃维护外键关系,cascade就不用说了
<hibernate-mapping package="com.cissst.it.dept.entity"> <class name="Dept" table="DEPT1"> <id name="deptno" column="DEPTNO"> <generator class="native"></generator> </id> <property name="dname" column="DNAME"></property> <set name="emps" inverse="true" cascade="save-update,delete"> <key column="deptno"></key> <one-to-many class="com.cissst.it.emp.entity.Emp" /> </set> </class> </hibernate-mapping>
emp.hbm.xml
<hibernate-mapping package="com.cissst.it.emp.entity"> <class name="Emp" table="EMP1"> <id name="empno" column="EMPNO"> <generator class="native"></generator> </id> <property name="ename" column="ENAME"></property> <property name="job" column="JOB"></property> <many-to-one name="dept" class="com.cissst.it.dept.entity.Dept"/> </class> </hibernate-mapping>
测试类:
public static void main(String[] args) { //级联保存 Session session = SessionUtil.openSession(); Transaction tx = session.getTransaction(); tx.begin(); Dept dept = new Dept(); dept.setDname("develop"); Emp emp1 = new Emp(); emp1.setEname("PECOOL"); emp1.setJob("CODE"); Emp emp2 = new Emp(); emp2.setEname("PHOEBE"); emp2.setJob("PHOTO"); HashSet<Emp> set = new HashSet<Emp>(); set.add(emp1); set.add(emp2); dept.setEmps(set); session.save(dept); tx.commit(); session.close(); }
只需要保存dept对象,dept中的emp对象也会得到保存,一般1方放弃维护外键关系有助于改善性能,发现sql中明显少了update语句。
标签:hash new nat 改善 bsp cas hiberna comm cti
原文地址:https://www.cnblogs.com/pecool/p/9690996.html