双向多对一关系的增删改查

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双向多对一关系的增删改查相关的知识,希望对你有一定的参考价值。

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();
		
	}
}


本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1879737

以上是关于双向多对一关系的增删改查的主要内容,如果未能解决你的问题,请参考以下文章

双向链表的增删改查C++完整实现

Django 多对多关系的增删改查

Java SSM 项目实战 day03 功能介绍,订单的操作,订单的增删改查

Django多对多的增删改查

多对多表的增删改查与drf的分页

TP5的多对多模型,以及中间表,已经中间表的增删改查