Hibernate 多对多的增删改查。

                                              inverse

Hibernate 多对多的增删改查你得先了解inverse功能作用。

百度文档:

Inverse是hibernate双向关系中的基本概念。inverse的真正作用就是指定由哪一方来维护之间的关联关系。当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之间的关联关系。

Hibernate仅仅按照主控方对象的状态的变化来同步更新数据库。按照原来的映射文 件,people.getAddresses().add(address),即主控方对象的状态发生了改变,因此数据库会跟着对象状态的变化来同步更新 数据库;而address.setPeople(people),即被控方对象的状态发生了改变,它是不能触发对象和数据库的同步更新的。

 

下面直接上代码(简单的五表权限):

实体类:

Menu 实体类:

package com.zking.entity;

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

public class Menu {
	private String mid=String.valueOf(UUID.randomUUID());;
	private String mname;
	private transient Set sr=new HashSet<>();
	
	
	public Menu() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Menu(String mid, String mname, Set sr) {
		super();
		this.mid = mid;
		this.mname = mname;
		this.sr = sr;
	}

	public String getMid() {
		return mid;
	}
	public void setMid(String mid) {
		this.mid = mid;
	}
	public String getMname() {
		return mname;
	}
	public void setMname(String mname) {
		this.mname = mname;
	}
	public Set getSr() {
		return sr;
	}
	public void setSr(Set sr) {
		this.sr = sr;
	}
	
	

}

Role实体类:

package com.zking.entity;

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

public class Role {
	private String rid=String.valueOf(UUID.randomUUID());;
	private String rname;
	private transient Set su=new HashSet<>();
	private Set sm=new HashSet<>();
	public Role() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Role(String rid, String rname, Set su, Set sm) {
		super();
		this.rid = rid;
		this.rname = rname;
		this.su = su;
		this.sm = sm;
	}
	public String getRid() {
		return rid;
	}
	public void setRid(String rid) {
		this.rid = rid;
	}
	public String getRname() {
		return rname;
	}
	public void setRname(String rname) {
		this.rname = rname;
	}
	public Set getSu() {
		return su;
	}
	public void setSu(Set su) {
		this.su = su;
	}
	public Set getSm() {
		return sm;
	}
	public void setSm(Set sm) {
		this.sm = sm;
	}
	
	
	

}

Users实体类:

package com.zking.entity;

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

public class Users {
	private String uid=String.valueOf(UUID.randomUUID());
	private String uname;
	private Set sr=new HashSet<>();
	public Users() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Users(String uid, String uname, Set sr) {
		super();
		this.uid = uid;
		this.uname = uname;
		this.sr = sr;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public Set getSr() {
		return sr;
	}
	public void setSr(Set sr) {
		this.sr = sr;
	}
	
	

}

实体类HBM.XML的文档





    
        
            
            
        
        
            
        
      
        
        
        
    





	
		
			
			
		
		
			
		
		
			
			
		
	





	
		
			
			
		
		
			
		
		
			
			
		

		
			
			
		


	

CFG.XML文档:





	
		
			
			
		
		
			
		
		
			
			
		
	

测试类:

package com.zking.test;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.zking.entity.Menu;
import com.zking.entity.Role;
import com.zking.entity.Users;

public class MyTest {

	 @Test
	 public void add() {
	 Configuration configuration = new Configuration().configure();
	 SessionFactory sessionFactory = configuration.buildSessionFactory();
	 Session session = sessionFactory.openSession();
	 Transaction transaction = session.beginTransaction();
	 //用户
	 Users u1=new Users();
	 u1.setUname("admin");
	
	 Users u2=new Users();
	 u2.setUname("System");
	 //角色
	 Role r1=new Role();
	 r1.setRname("Admin");
	
	 Role r2=new Role();
	 r2.setRname("System_Admin");
	
	 //菜单
	 Menu m1=new Menu();
	 m1.setMname("角色管理");
	
	 Menu m2=new Menu();
	 m2.setMname("查看信息");
	
	 //互设
	 u1.getSr().add(r1);
	 u2.getSr().add(r2);
	
	 r1.getSu().add(u1);
	 r2.getSu().add(u2);
	
	 r1.getSm().add(m1);
	 r2.getSm().add(m2);
	//保存
	 session.save(u1);
	 session.save(u2);
	//提交
	 transaction.commit();
	 session.close();
	 sessionFactory.close();
	
	 }
	
	 @Test
	 public void query() {
	 Configuration configuration = new Configuration().configure();
	 SessionFactory sessionFactory = configuration.buildSessionFactory();
	 Session session = sessionFactory.openSession();
	 //h获得当前用户
	 Users u=session.get(Users.class, "265bdd39-5b11-4deb-9099-2b04a714ebe2");
	//转为JSON格式
	 String str=JSON.toJSONString(u);
	 //输出Str
	 System.out.println(str);
	
	 }


	@Test
	public void delete() {
		Configuration configuration = new Configuration().configure();
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		// 得到用户
		Users u = session.get(Users.class, "265bdd39-5b11-4deb-9099-2b04a714ebe2");
		// 得到当前用户所有权限
		Set sr = u.getSr();
		// 循环SET集合
		for (Role r : sr) {
			// 判断需要删除的权限
			if (r.getRname().equals("Admin")) {
				// 从权限表中获得用户再删除当前用户
				r.getSu().remove(u);
			}
		}
		session.update(u);
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}

代码什么的都在上面!

有什么不足的地方还请各位多多指教!!!

你可能感兴趣的:(Hibernate 多对多的增删改查。)