欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate框架jpa双向多对多配置,操作

程序员文章站 2022-03-02 15:27:12
...
package bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="sys_user")
public class User {
	@Id
	@Column(name="user_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long user_id;//'用户id',
	@Column(name="user_code")
	private String user_code;// '用户账号',
	@Column(name="user_name")
	private String user_name;// '用户名称',
	
	//外键
	// 配置多对多的关系
		// 有Role的集合
		@ManyToMany(targetEntity=Role.class,cascade=CascadeType.PERSIST)
		// 维护外键
		/*name: 中间表的名称
		joinColumns: 自己在中间表的配置
		inverseJoinColumns:	对方在中间表的配置
		*/
		@JoinTable(name="sys_user_role",
							joinColumns= {
									// name:自己在中间表的外键字段名
									// referencedColumnName: 指向自己的主键字段名
									@JoinColumn(name="user_id",referencedColumnName="user_id")
							},
							inverseJoinColumns= {
									/*name:对方在中间表的外键字段名
									referencedColumnName:指向对方的主键字段名*/
									@JoinColumn(name="role_id",referencedColumnName="role_id")
									
							})
	private Set<Role> role=new HashSet<Role>();
	public Long getUser_id() {
		return user_id;
	}
	public void setUser_id(Long user_id) {
		this.user_id = user_id;
	}
	public String getUser_code() {
		return user_code;
	}
	public void setUser_code(String user_code) {
		this.user_code = user_code;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public Set<Role> getRole() {
		return role;
	}
	public void setRole(Set<Role> role) {
		this.role = role;
	} 
}
package bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
//类上:
//@Entity // 当前类是持久化类
//@Table(name="表名") //当前持久化类和哪个表做映射
//
//属性:
//@Id //当前的属性是oid属性
//@Column(name="主键名")// 和表的哪个字段做映射
//@GeneratedValue(strategy=GenerationType.IDENTITY) //指定oid的增长策略
//
//
// @Column(name="字段名") // 其它属性和表中的字段映射
@Entity
@Table(name="sys_role")
public class Role {
	@Id
	@Column(name="role_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long role_id;
	@Column(name="role_name")
	private String role_name;//'角色名称',
	@Column(name="role_memo")
	private String role_memo;// '备注',
	
	//外键
	// 配置多对多
		// 有user的集合
		/*targetEntity:对象的字节码文件对象
		mappedBy:自己在对方中的属性名
		*/
	@ManyToMany(targetEntity=User.class,mappedBy="role")
	private Set<User> users=new HashSet<User>();
	
	public Long getRole_id() {
		return role_id;
	}
	public void setRole_id(Long role_id) {
		this.role_id = role_id;
	}
	public String getRole_name() {
		return role_name;
	}
	public void setRole_name(String role_name) {
		this.role_name = role_name;
	}
	public String getRole_memo() {
		return role_memo;
	}
	public void setRole_memo(String role_memo) {
		this.role_memo = role_memo;
	}
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
}

JPA操作

package test;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.ManyToMany;

import org.junit.Test;

import bean.Role;
import bean.User;
import utils.JPAUtils;

public class demo4 {
//JPA双向多对多操作
	@Test//普通保存
	public void t1() {
		EntityManager en = JPAUtils.getEntityManager();
		EntityTransaction tr = en.getTransaction();
		tr.begin();
		//保存两个用户
		User u1=new User();
		u1.setUser_name("小明");
		User u2=new User();
		u2.setUser_name("大柱");
		//三个职业
		Role r1=new Role();
		r1.setRole_name("学生");
		Role r2=new Role();
		r2.setRole_name("医生");
		Role r3=new Role();
		r3.setRole_name("老师");
		
		//用户关联职业
		u1.getRole().add(r1);
		u1.getRole().add(r2);
		
		u2.getRole().add(r1);
		u2.getRole().add(r3);
		//职业关联用户
		r1.getUsers().add(u1);
		r1.getUsers().add(u2);
		r2.getUsers().add(u1);
		r2.getUsers().add(u2);
		//保存所有数据
		en.persist(u1);
		en.persist(u2);
		en.persist(r1);
		en.persist(r2);
		en.persist(r3);
	
		tr.commit();
		en.close();
	}
	//级联保存
	//在保存的一方加上
	//@ManyToMany(cascade=CascadeType.PERSIST)
	@Test
	public void t2(){
		EntityManager en = JPAUtils.getEntityManager();
		EntityTransaction tr = en.getTransaction();
		tr.begin();
		//保存两个用户
		User u1=new User();
		u1.setUser_name("小明");
		User u2=new User();
		u2.setUser_name("大柱");
		//三个职业
		Role r1=new Role();
		r1.setRole_name("学生");
		Role r2=new Role();
		r2.setRole_name("医生");
		Role r3=new Role();
		r3.setRole_name("老师");
		
		//用户关联职业
		u1.getRole().add(r1);
		u1.getRole().add(r2);
		
		u2.getRole().add(r1);
		u2.getRole().add(r3);
		//职业关联用户
		r1.getUsers().add(u1);
		r1.getUsers().add(u2);
		r2.getUsers().add(u1);
		r2.getUsers().add(u2);
		//保存用户关联的数据
		en.persist(u1);
		en.persist(u2);
		
		tr.commit();
		en.close();
	}
	//修改单条数据
	@Test
	public void t3() {
		EntityManager en = JPAUtils.getEntityManager();
		EntityTransaction tr = en.getTransaction();
		tr.begin();
		//获取用户
		User user = en.find(User.class, 1L);
		//获取要删除的职业
		Role role = en.find(Role.class, 3L);
		//获取要修改的职业
		Role role3 = en.find(Role.class, 1L);
		//删除职业
		user.getRole().remove(role);
		//添加职业
		user.getRole().add(role3);

		tr.commit();
		en.close();
	}
}