package com.ddwei.test; import org.hibernate.Session; import org.hibernate.Transaction; import com.ddwei.entity.Employee2; import com.ddwei.entity.Project; import com.ddwei.util.HibernateUtil; /** * * @author weijingli * */ public class Test { /** * 关联关系交由Project来管理,需要在Employee2.hbm.xml中设置反转; * 为了删除项目时,级联删除员工,所以需要在Project.hbm.xml中设置级联 记得cfg.xml 配置hbm.xml * @param args */ public static void main(String[] args) { Employee2 emp1 = new Employee2(1,"苏妲己"); Employee2 emp2 = new Employee2(2,"白素贞"); Project pro1 = new Project(1, "苏州银行"); Project pro2 = new Project(2, "杭州银行"); pro1.getEmployees().add(emp1); pro2.getEmployees().add(emp1); pro2.getEmployees().add(emp2); Session session1 = HibernateUtil.getSession(); Transaction trans1 = session1.beginTransaction(); session1.save(pro1); session1.save(pro2); trans1.commit(); HibernateUtil.closeSession(session1); } }
原文地址:https://www.cnblogs.com/1446358788-qq/p/9391614.html
时间: 2024-09-30 03:31:14