hibernate 多對對(many to many)
阿新 • • 發佈:2018-12-09
package com.zking.pojo; import java.util.HashSet; import java.util.Set; public class Menu { private String mid; private String mname; private transient Set<Roles> sr1 = new HashSet<Roles>(); public Menu() { super(); } public Set<Roles> getSr1() { return sr1; } public void setSr1(Set<Roles> sr1) { this.sr1 = sr1; } 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; } } package com.zking.pojo; import java.util.HashSet; import java.util.Set; public class Roles { private String rid; private String rname; private transient Set<Users> su = new HashSet<Users>(); private transient Set<Menu> mi = new HashSet<Menu>(); public Roles() { super(); } public Roles(String rid, String rname) { super(); this.rid = rid; this.rname = rname; } public Set<Users> getSu() { return su; } public void setSu(Set<Users> su) { this.su = su; } public Set<Menu> getMi() { return mi; } public void setMi(Set<Menu> mi) { this.mi = mi; } 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; } } package com.zking.pojo; import java.util.HashSet; import java.util.Set; public class Users { private String uid; private String uname; private Set<Roles> sr = new HashSet<Roles>(); public Users() { super(); } public Users(String uid, String uname) { super(); this.uid = uid; this.uname = uname; } public Set<Roles> getSr() { return sr; } public void setSr(Set<Roles> sr) { 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; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2018-9-11 17:33:00 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.zking.pojo.Menu" table="MENU"> <id name="mid" type="java.lang.String"> <column name="MID" /> <generator class="guid" /> </id> <property name="mname" type="java.lang.String"> <column name="MNAME" /> </property> <set name="sr1" table="RolesMenu" cascade="save-update" > <key> <column name="mid" /> </key> <many-to-many class="com.zking.pojo.Roles" column="rid"></many-to-many> </set> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2018-9-11 17:33:00 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.zking.pojo.Roles" table="ROLES"> <id name="rid" type="java.lang.String"> <column name="RID" /> <generator class="guid" /> </id> <property name="rname" type="java.lang.String"> <column name="RNAME" /> </property> <set name="su" table="UsersRoles" cascade="save-update"> <key column="rid"></key> <many-to-many column="uid" class="com.zking.pojo.Users"></many-to-many> </set> <set name="mi" table="RolesMenu" inverse="true" cascade="save-update"> <key column="rid"></key> <many-to-many column="mid" class="com.zking.pojo.Menu"></many-to-many> </set> </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2018-9-11 17:33:00 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.zking.pojo.Users" table="USERS"> <id name="uid" type="java.lang.String"> <column name="UID" /> <generator class="guid" /> </id> <property name="uname" type="java.lang.String"> <column name="UNAME" /> </property> <set name="sr" table="UsersRoles" inverse="true" cascade="save-update"> <key column="uid"></key> <many-to-many column="rid" class="com.zking.pojo.Roles"></many-to-many> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">sasa</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/t204?characterEncoding=utf-8</property> <property name="hibernate.connection.username">root</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="com/zking/pojo/Users.hbm.xml" /> <mapping resource="com/zking/pojo/Roles.hbm.xml" /> <mapping resource="com/zking/pojo/Menu.hbm.xml" /> </session-factory> </hibernate-configuration>
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.pojo.Menu;
import com.zking.pojo.Roles;
import com.zking.pojo.Users;
public class TempTest {
// @Test
// public void add() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Transaction transaction = session.beginTransaction();
//
// // 建立Users
// Users u1 = new Users();
// u1.setUname("admin");
//
// // 建立Roles
// Roles r1 = new Roles();
// r1.setRname("System_Admin");
//
// // 建立Roles
// Roles r2 = new Roles();
// r2.setRname("Admin");
//
// u1.getSr().add(r1);
// u1.getSr().add(r2);
//
// session.saveOrUpdate(u1);
//
// transaction.commit();
// session.close();
// sessionFactory.close();
// }
// @Test
// public void query() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Users u1 = session.get(Users.class, "d5897815-b576-11e8-ad83-54ee75bbb0cd");
// String str = JSON.toJSONString(u1);
// System.out.println(str);
// }
// @Test
// public void delete() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Users u1 = session.get(Users.class, "d5897815-b576-11e8-ad83-54ee75bbb0cd");
// Set<Roles> ri = u1.getSr();
// for (Roles roles : ri) {
// roles.getSu().remove(u1);
// }
//
// session.beginTransaction().commit();
// session.close();
// sessionFactory.close();
// }
//刪除使用者,不刪除角色
// public void deleteUserByID(long uid) {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Users u1 = session.get(Users.class, "d5897815-b576-11e8-ad83-54ee75bbb0cd");
// session.delete(u1);
// session.beginTransaction().commit();
// session.close();
// sessionFactory.close();
// }
// @Test
// public void update() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Users u1 = session.get(Users.class, "d5897815-b576-11e8-ad83-54ee75bbb0cd");
// u1.setUname("Admin");
// session.update(u1);
//
// session.beginTransaction().commit();
// session.close();
// sessionFactory.close();
//
// }
// Roles 》Menu
// @Test
// public void add1() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Transaction transaction = session.beginTransaction();
// Roles r1 = new Roles();
// r1.setRname("MAMA");
//
// Menu m1 = new Menu();
// m1.setMname("mama");
//
// Menu m2 = new Menu();
// m2.setMname("mama_admin");
//
// r1.getMi().add(m1);
// r1.getMi().add(m2);
//
// session.saveOrUpdate(r1);
//
// transaction.commit();
// session.close();
// sessionFactory.close();
// }
// @Test
// public void query1() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Roles r1 = session.get(Roles.class, "40a53234-b5af-11e8-ad83-54ee75bbb0cd");
// String str = JSON.toJSONString(r1);
// System.out.println(str);
// }
// @Test
// public void delete1() {
// Configuration configuration = new Configuration().configure();
// SessionFactory sessionFactory = configuration.buildSessionFactory();
// Session session = sessionFactory.openSession();
// Roles r1 = session.get(Roles.class, "40a53234-b5af-11e8-ad83-54ee75bbb0cd");
// Set<Menu> m1 = r1.getMi();
// for (Menu menu : m1) {
// menu.getSr1().remove(r1);
// }
// session.beginTransaction().commit();
// session.close();
// sessionFactory.close();
// }
}