hibernate 双向一多对关联 删除一端时级联删除多端

//部门 public class Department { private Long id; private String name; private String description; private Set<Employee> employees; //一端执有多端的一个集合引用 getter(); setter(); } //员工 public class Employee { private Long id; private String username; private Date joinTime; private Department dept; //持有关联实体类的一个引用 getter(); setter(); }  

//部门表的映射文件 <hibernate-mapping> <class name="com.liuc.domain.Department" table="dept"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> <property name="description"/> <set name="employees" inverse="true" cascade="all-delete-orphan" > <key column="dept_id"/> <one-to-many class="com.liuc.domain.Employee" /> </set> </class> </hibernate-mapping> 员工表的映射文件 <hibernate-mapping> <class name="com.liuc.domain.Employee" table="emp"> <id name="id" column="id"> <generator class="native"/> </id> <property name="username"/> <property name="joinTime" column="join_time"/> <!-- 映射员工到部门的多对一 --> <many-to-one name="dept" column="dept_id" not-null="true" foreign-key="fk_emp_dept"/> </class> </hibernate-mapping> 

 

生成的表结构 create table dept ( id bigint not null auto_increment, name varchar(255), description varchar(255), primary key (id) ); create table emp ( id bigint not null auto_increment, username varchar(255), join_time datetime, dept_id bigint not null, primary key (id) ); 

 

测试jUnit3 如果是jUnit4 可以@Test public class MappingTest { private static SessionFactory sessionFactory; @BeforeClass public static void init(){ sessionFactory = HibernateUtil.getSessionFactory(); } @AfterClass public static void destroy(){ sessionFactory = null; } @Test public void testSave(){ Session session = sessionFactory.openSession(); session.beginTransaction(); Department dept = new Department(); dept.setName("研发部"); dept.setDescription("研发公司的产品"); session.save(dept); Employee emp = new Employee(); emp.setUsername("张三"); emp.setJoinTime(new Date()); emp.setDept(dept); //设置它们关联关系 session.save(emp); session.getTransaction().commit(); session.close(); } SQL语句 Hibernate: insert into dept (name, description) values (?, ?) Hibernate: insert into emp (username, join_time, dept_id) values (?, ?, ?)


级联删除,删除一个部门级职删除一个部门下的所有员工 @Test public void testDelete(){ Session session = sessionFactory.openSession(); session.beginTransaction(); Department dep =(Department)session.get(Department.class, Long.valueOf(2)); session.delete(dep); session.getTransaction().commit(); session.close(); } SQL语句 Hibernate: select department0_.id as id0_0_, department0_.name as name0_0_, department0_.description as descript3_0_0_ from dept department0_ where department0_.id=? Hibernate: select employees0_.dept_id as dept4_1_, employees0_.id as id1_, employees0_.id as id1_0_, employees0_.username as username1_0_, employees0_.join_time as join3_1_0_, employees0_.dept_id as dept4_1_0_ from emp employees0_ where employees0_.dept_id=? Hibernate: delete from emp where id=? Hibernate: delete from dept where id=?  

你可能感兴趣的:(Hibernate,session,JUnit,Class,generator,getter)