1、配置实体类
package cn.pojo; import java.util.HashSet; import java.util.Set; public class User { /** * 用户表 */ private Integer user_id; private String user_name; private String user_pwd; private Set<Role> setRole = new HashSet<Role>(); public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public String getUser_pwd() { return user_pwd; } public void setUser_pwd(String user_pwd) { this.user_pwd = user_pwd; } public Set<Role> getSetRole() { return setRole; } public void setSetRole(Set<Role> setRole) { this.setRole = setRole; } }
用户实体类
package cn.pojo; import java.util.HashSet; import java.util.Set; public class Role { //角色表 private Integer role_id; private String role_name; private String role_nome; private Set<User> setuser = new HashSet<User>(); public Set<User> getSetuser() { return setuser; } public void setSetuser(Set<User> setuser) { this.setuser = setuser; } public Integer getRole_id() { return role_id; } public void setRole_id(Integer 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_nome() { return role_nome; } public void setRole_nome(String role_nome) { this.role_nome = role_nome; } }
角色实体类
2、配置映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.pojo.User" table="user"> <id name="user_id" column="user_id"> <generator class="native"></generator> </id> <!-- 3、配置其他属性 --> <property name="user_name"></property> <property name="user_pwd" ></property> <!-- 第三张表 --> <set name="setRole" table="user_role" cascade="save-update,delete"> <key column="userid"></key> <many-to-many class="cn.pojo.Role" column="roleid"></many-to-many> </set> </class> </hibernate-mapping>
用户配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="cn.pojo.Role" table="role"> <id name="role_id" column="role_id"> <generator class="native"></generator> </id> <!-- 3、配置其他属性 --> <property name="role_name"></property> <property name="role_nome" ></property> <set name="setuser" table="user_role" cascade="save-update,delete"> <key column="roleid"></key> <many-to-many class="cn.pojo.User" column="userid"></many-to-many> </set> </class> </hibernate-mapping>
角色配置文件
3、配置核心文件
4、加载核心文件
5、测试
package cn.junit; import static org.junit.Assert.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.junit.Test; import com.unitl.HibernateUtils; import cn.pojo.Role; import cn.pojo.User; public class Text1 { // 添加操作 @Test public void test() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); //创建用户 User user1 = new User(); user1.setUser_name("张三"); user1.setUser_pwd("123"); User user2 = new User(); user2.setUser_name("李四"); user2.setUser_pwd("456"); //创建角色 Role r1 = new Role(); r1.setRole_name("经理"); r1.setRole_nome("管理"); Role r2 = new Role(); r2.setRole_name("秘书"); r2.setRole_nome("管理"); Role r3 = new Role(); r3.setRole_name("保安"); r3.setRole_nome("管理"); // 用户拥有角色 user1.getSetRole().add(r1); user1.getSetRole().add(r2); user2.getSetRole().add(r2); user2.getSetRole().add(r3); // 保存用户 session.save(user1); session.save(user2); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } } /** *一般不删除 */ @Test public void test2() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); User user = session.get(User.class, 1); session.delete(user); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } } /** * 维护表 * 让某个用户拥有某个角色 */ @Test public void test3() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); // 查询id User user = session.get(User.class, 4); Role role = session.get(Role.class, 4); // 把用户保存到set集合里 user.getSetRole().add(role); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } } /** * 维护表 * 让某个用户没有某个角色 */ @Test public void test4() { SessionFactory factory = null; Session session = null; Transaction ts = null; try { factory = HibernateUtils.getSessionFactory(); session = factory.openSession(); ts = session.beginTransaction(); // 查询id User user = session.get(User.class, 4); Role role = session.get(Role.class, 4); // 把用户保存到set集合里 user.getSetRole().remove(role); ts.commit(); } catch (Exception e) { ts.rollback(); }finally{ session.close(); factory.close(); } } }
时间: 2024-10-13 07:30:56