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

Spring Data JPA处理一对一、一对多、多对多关系映射

程序员文章站 2022-04-23 23:49:32
...

1.一对一的关联
一对一关联可以把外键放在任何一放,假设Users和Roles是一对一的关系,代码如下
Users.java

package per.czt.pojo;

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.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_users")
public class Users {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="user_id")
	private Integer userid;
	@Column(name="user_name")
	private String username;
	@Column(name="user_age")
	private Integer userage;
	
	@OneToOne(cascade=CascadeType.PERSIST)
	@JoinColumn(name="roles_id")
	private Roles roles;
	public Users() {
		// TODO Auto-generated constructor stub
	}
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getUserage() {
		return userage;
	}
	public void setUserage(Integer userage) {
		this.userage = userage;
	}
	public Roles getRoles() {
		return roles;
	}
	public void setRoles(Roles roles) {
		this.roles = roles;
	}
	@Override
	public String toString() {
		return "Users [userid=" + userid + ", username=" + username + ", userage=" + userage + "]";
	}
}

Roles.java

package per.czt.pojo;

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.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_roles")
public class Roles {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="role_id")
	private Integer roleid;
	@Column(name="role_name")
	private String rolename;
	@OneToOne(mappedBy="roles",cascade=CascadeType.PERSIST)
	private Users users;
	public Integer getRoleid() {
		return roleid;
	}

	public void setRoleid(Integer roleid) {
		this.roleid = roleid;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public Roles() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Roles [roleid=" + roleid + ", rolename=" + rolename + ", users=" + users + "]";
	}

	
	

}

测试类

package per.czt.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import per.czt.dao.UsersDao;
import per.czt.pojo.Roles;
import per.czt.pojo.Users;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class AssociationOneToOneTest {
	
	@Autowired
	private UsersDao usersDao;
	
	@Test

	public void test1() {
	   //创建角色
		Roles roles=new Roles();
		roles.setRolename("admin");
		//创建用户
		Users users=new Users();
		users.setUserage(13);
		users.setUsername("张晓萌");
		//建立关联
		users.setRoles(roles);
		roles.setUsers(users);
		//保存对象
		this.usersDao.save(users);
		//this.usersDao.save(users);
		
		
	}
	@Test
	public void test2() {
		Users users=usersDao.findOne(22);
		System.out.println(users);
		System.out.println(users.getRoles());
	}

}

2.一对多关系
假设角色和用户是一对多的关系,一个角色对于多个用户,一个用户只能有一个角色
Roles.java

package per.czt.pojo;

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.OneToMany;

import javax.persistence.Table;

@Entity
@Table(name="t_roles4")
public class Roles {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="role_id")
	private Integer roleid;
	@Column(name="role_name")
	private String rolename;
	
	@OneToMany(mappedBy="roles")
	private Set<Users> users=new HashSet<Users>();	

	public Integer getRoleid() {
		return roleid;
	}

	public void setRoleid(Integer roleid) {
		this.roleid = roleid;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public Roles() {
		super();
		// TODO Auto-generated constructor stub
	}


	public Set<Users> getUsers() {
		return users;
	}

	public void setUsers(Set<Users> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";
	}

	
	
	

}

Users.java

package per.czt.pojo;


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.ManyToOne;

import javax.persistence.Table;

@Entity
@Table(name="t_users4")
public class Users {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="user_id")
	private Integer userid;
	@Column(name="user_name")
	private String username;
	@Column(name="user_age")
	private Integer userage;
	
	@ManyToOne(cascade=CascadeType.PERSIST)//开启级联操作
	@JoinColumn(name="roles_id")
	private Roles roles;
	
	
	public Users() {
		// TODO Auto-generated constructor stub
	}
	public Integer getUserid() {
		return userid;
	}
	public void setUserid(Integer userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getUserage() {
		return userage;
	}
	public void setUserage(Integer userage) {
		this.userage = userage;
	}
	
	public Roles getRoles() {
		return roles;
	}
	public void setRoles(Roles roles) {
		this.roles = roles;
	}
	@Override
	public String toString() {
		return "Users [userid=" + userid + ", username=" + username + ", userage=" + userage + "]";
	}
}

测试类
OneToManyTest.java

package per.czt.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import per.czt.dao.UsersDao;
import per.czt.pojo.Roles;
import per.czt.pojo.Users;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class OneToManyTest {
	
	@Autowired
	private UsersDao usersDao;
	@Test
	public void test1() {
		//创建Roles对象
	
		Roles roles=new Roles();
		roles.setRolename("administrator");
		//创建Users对象
		
		Users users1=new Users();
		users1.setUsername("小孙");
		users1.setUserage(14);
		//建立关联
		
		roles.getUsers().add(users1);
		users1.setRoles(roles);
		//保存
		usersDao.save(users1);
	}
	
	@Test
	public void test2() {
		Users users=usersDao.findOne(5);
		System.out.println(users);
		System.out.println(users.getRoles());
	}
}

3.多对多关系
假设角色和菜单之间是多对多的关系,则会存在一个中间表存储角色id和菜单id,在这里从那一方建立这个表关联都可以,假设从角色表建立关联
Roles.java

package per.czt.pojo;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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.OneToMany;

import javax.persistence.Table;

@Entity
@Table(name="t_roles5")
public class Roles {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="role_id")
	private Integer roleid;
	@Column(name="role_name")
	private String rolename;
	
	@ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)//级联  , 延迟加载
	@JoinTable(name="t_roles_menus",[email protected](name="role_id"),[email protected](name="menu_id"))
	private Set<Menus> menus=new HashSet<Menus>();
	
	

	public Integer getRoleid() {
		return roleid;
	}

	public void setRoleid(Integer roleid) {
		this.roleid = roleid;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public Set<Menus> getMenus() {
		return menus;
	}

	public void setMenus(Set<Menus> menus) {
		this.menus = menus;
	}

	public Roles() {
		super();
		// TODO Auto-generated constructor stub
	}


	

	@Override
	public String toString() {
		return "Roles [roleid=" + roleid + ", rolename=" + rolename + "]";
	}
}

Menus.java

package per.czt.pojo;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import javax.persistence.Table;

@Entity
@Table(name="t_menus")
public class Menus {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="menus_id")
	private Integer menusid;
	@Column(name="menus_name")
	private String menusname;
	@Column(name="menus_url")
	private String menusurl;
	@Column(name="fatherid")
	private Integer fatherid;
	
	@ManyToMany(mappedBy="menus",fetch=FetchType.EAGER)// 延迟加载
	private Set<Roles> roles=new HashSet<Roles>();
	
	public Integer getMenusid() {
		return menusid;
	}
	public void setMenusid(Integer menusid) {
		this.menusid = menusid;
	}
	public String getMenusname() {
		return menusname;
	}
	public void setMenusname(String menusname) {
		this.menusname = menusname;
	}
	public String getMenusurl() {
		return menusurl;
	}
	public void setMenusurl(String menusurl) {
		this.menusurl = menusurl;
	}
	public Integer getFatherid() {
		return fatherid;
	}
	public void setFatherid(Integer fatherid) {
		this.fatherid = fatherid;
	}
	public Set<Roles> getRoles() {
		return roles;
	}
	public void setRoles(Set<Roles> roles) {
		this.roles = roles;
	}
	@Override
	public String toString() {
		return "Menus [menusid=" + menusid + ", menusname=" + menusname + ", menusurl=" + menusurl + ", fatherid="
				+ fatherid + ", roles=" + roles + "]";
	}
	public Menus() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}

ManyToManyTest.java

package per.czt.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import per.czt.dao.RolesDao;
import per.czt.pojo.Menus;
import per.czt.pojo.Roles;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
public class ManyToManyTest {
	
	@Autowired
	private RolesDao rolesDao;
	
	@Test
	public void test() {
		//创建角色
		Roles roles=new Roles();
		roles.setRolename("超级管理员");
		
		//创建menus
		Menus menus=new Menus();
		menus.setFatherid(1);
		menus.setMenusname("xx管理平台");
		menus.setMenusurl(null);
		
		//创建menus1
		Menus menus1=new Menus();
		menus1.setFatherid(-1);
		menus1.setMenusname("用户管理平台");
		menus1.setMenusurl(null);
		
		//把roles分别与menus1和menus建立关联
		roles.getMenus().add(menus1);
		roles.getMenus().add(menus);
		menus1.getRoles().add(roles);
		menus1.getRoles().add(roles);
		//保存
		rolesDao.save(roles);
	}
	
	@Test
	public void test2() {
		Roles roles=rolesDao.findOne(1);
		System.out.println(roles);
	
		System.out.println(roles.getMenus().size());
		for(Menus menus:roles.getMenus()) {
			System.out.println(menus);
		}
	}
	
	
}