zoukankan      html  css  js  c++  java
  • 多对多关联(双向)

     package com.jonsion.hibernate;
    import java.util.Set;
    public class Role {
    private int id;
    private String name;
    private Set users;
    public int getId() {
    return id;
    }
    public Set getUsers() {
    return users;
    }
    public void setUsers(Set users) {
    this.users = users;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    package com.jonsion.hibernate;
    import java.util.Set;
    public class User {
    private int id;
    private String name;
    private Set roles;
    public Set getRoles() {
    return roles;
    }
    public void setRoles(Set roles) {
    this.roles = roles;
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    }
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping >
    <class name="com.jonsion.hibernate.Role">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name"/>
    <set name="users" table="t_user_role">
    <key column="roleid"/>
    <many-to-many class="com.jonsion.hibernate.User" column="userid"/>
    </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">
    <hibernate-mapping >
    <class name="com.jonsion.hibernate.User">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name"/>
    <set name="roles" table="t_user_role">
    <key column="userid"/>
    <many-to-many class="com.jonsion.hibernate.Role" column="roleid"/>
    </set>
    </class>
    </hibernate-mapping>
    package com.jonsion.hibernate;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import junit.framework.TestCase;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    public class Many2ManyTest extends TestCase{
    public void testSave1(){
    Session session=null;
    Transaction tx=null;
    try{
    session=HibernateUtils.getSession();
    tx=session.beginTransaction();
    Role r1=new Role();
    r1.setName("大堂经理");
    session.save(r1);
    Role r2=new Role();
    r2.setName("出纳");
    session.save(r2);
    Role r3=new Role();
    r3.setName("业务员");
    session.save(r3);
    User user1=new User();
    user1.setName("阿希");
    Set r1role=new HashSet();
    r1role.add(r1);
    r1role.add(r2);
    user1.setRoles(r1role);
    User user2=new User();
    user2.setName("阿雅");
    Set r2role=new HashSet();
    r2role.add(r2);
    r2role.add(r3);
    user2.setRoles(r2role);
    User user3=new User();
    user3.setName("阿杜");
    Set r3role=new HashSet();
    r3role.add(r1);
    r3role.add(r2);
    r3role.add(r3);
    user3.setRoles(r3role);
    session.save(user1);
    session.save(user2);
    session.save(user3);
    tx.commit();
    }catch(Exception ex){
    ex.printStackTrace();
    tx.rollback();
    }finally{
    HibernateUtils.closeSession(session);
    }
    }
    public void testSave2(){
    Session session=null;
    Transaction tx=null;
    try{
    session=HibernateUtils.getSession();
    tx=session.beginTransaction();
    User user1=new User();
    user1.setName("阿希");
    session.save(user1);
    User user2=new User();
    user2.setName("阿雅");
    session.save(user2);
    User user3=new User();
    user3.setName("阿杜");
    session.save(user3);
    Role r1=new Role();
    r1.setName("大堂经理");
    Set users1=new HashSet();
    users1.add(user1);
    users1.add(user2);
    r1.setUsers(users1);
    session.save(r1);
    Role r2=new Role();
    r2.setName("出纳");
    Set users2=new HashSet();
    users2.add(user2);
    users2.add(user3);
    r2.setUsers(users2);
    session.save(r2);
    Role r3=new Role();
    r3.setName("业务员");
    Set users3=new HashSet();
    users3.add(user1);
    users3.add(user2);
    users3.add(user3);
    r3.setUsers(users3);
    session.save(r3);
    tx.commit();
    }catch(Exception ex){
    ex.printStackTrace();
    tx.rollback();
    }finally{
    HibernateUtils.closeSession(session);
    }
    }
    public void testLoad1(){
    Session session=null;
    Transaction tx=null;
    try{
    session=HibernateUtils.getSession();
    tx=session.beginTransaction();
    User user=(User)session.load(User.class, 3);
    System.out.println(user.getName());
    for(Iterator it=user.getRoles().iterator();it.hasNext();){
    Role role=(Role)it.next();
    System.out.println(role.getName());
    }
    tx.commit();
    }catch(Exception ex){
    ex.printStackTrace();
    tx.rollback();
    }finally{
    HibernateUtils.closeSession(session);
    }
    }
    public void testLoad2(){
    Session session=null;
    Transaction tx=null;
    try{
    session=HibernateUtils.getSession();
    tx=session.beginTransaction();
    Role role=(Role)session.load(Role.class, 3);
    System.out.println(role.getName());
    for(Iterator it=role.getUsers().iterator();it.hasNext();){
    User user=(User)it.next();
    System.out.println(user.getName());
    }
    tx.commit();
    }catch(Exception ex){
    ex.printStackTrace();
    tx.rollback();
    }finally{
    HibernateUtils.closeSession(session);
    }
    }
    }
  • 相关阅读:
    hibernate 4 需要导入的jar包
    中国象棋马走日(要求打印每一种走法) — 递归
    最长公共子串
    国际象棋马走日(骑士周游)
    八皇后
    约瑟夫问题
    hashtable的运用实例
    用较小的代价去除字符串中的空格
    C++ string数据类型的实现
    strcpy、strncpy与memcpy的区别与使用方法
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1958543.html
Copyright © 2011-2022 走看看