Person类:
package com.bjsxt.hibernate; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Person { private Integer id; private String name; private Group1 group1; @ManyToOne(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH}) public Group1 getGroup1() { return group1; } public void setGroup1(Group1 group1) { this.group1 = group1; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Group1类(数据库中group是关键字,也可以做一个映射):
package com.bjsxt.hibernate; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class Group1 { private Integer id; private String name; private List<Person> persons; @OneToMany(mappedBy="group1",fetch=FetchType.EAGER,cascade={CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE}) public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试类:
package com.bjsxt.hibernate; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; public class TestPerson_group { private static SessionFactory sessionFactory; @Test public void testAddPerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Person person1=new Person(); person1.setName("person1"); Person person2=new Person(); person2.setName("person2"); Group1 group1=new Group1(); group1.setName("group1"); List<Person> persons=new ArrayList<Person>(); persons.add(person1); persons.add(person2); person1.setGroup1(group1); person2.setGroup1(group1); group1.setPersons(persons); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); session.persist(group1); session.getTransaction().commit(); } @Test public void deleteGroup1(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Group1 group1=(Group1) session.get(Group1.class, 1); List<Person> persons=group1.getPersons(); Iterator<Person> iterator=persons.iterator(); if(iterator.hasNext()){ Person person=iterator.next(); System.out.println(person.getName()); person.setGroup1(null); } session.update(group1); /* * 一定要先update一下group1, * 将group1的每一个user的group设置为null,同步到数据库,否则group1对应于person的关联关系没有打破*/ group1.setPersons(null); session.delete(group1); session.getTransaction().commit(); System.out.println(group1.getName()); } @Test public void deleteUser(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); /*Person person=(Person) session.get(Person.class, 3); person.setGroup1(null); session.delete(person);*/ /* * 或者直接写HQL删除*/ session.createQuery("delete from Person p where p.id=4").executeUpdate(); session.getTransaction().commit(); } @Test public void updatePerson(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Person person=(Person) session.get(Person.class, 1); person.setGroup1(null); session.update(person); session.getTransaction().commit(); } @Test public void updateGroup1(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.getCurrentSession(); session.beginTransaction(); Group1 group1=(Group1) session.get(Group1.class, 1); group1.setPersons(null); session.update(group1); session.getTransaction().commit(); } }
时间: 2024-10-03 21:44:14