hibernate many2many (单向)示例

Role类

package com.cnblogs.hibernate_first;

public class Role {

    private int id;

    private String name;

    public Role() {
        super();
    }

    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;
    }

}

User类

package com.cnblogs.hibernate_first;

import java.util.Set;

public class User {

    private int id;
    
    private String name;
    
    private Set roles;

    public User() {
        super();
    }

    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;
    }

    public Set getRoles() {
        return roles;
    }

    public void setRoles(Set roles) {
        this.roles = roles;
    }
    
    

    
    

}
View Code

user.hbm.xml

<?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 package="com.cnblogs.hibernate_first">
    <class name="User" table="t_user">
        <id name="id" column="t_id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="t_name" />
        <set name="roles" table="t_user_role">
            <key column="user_id"></key>
            <many-to-many class="com.cnblogs.hibernate_first.Role" column="role_id"></many-to-many>
        
        </set>        
    </class>
</hibernate-mapping>
View Code

role.hbm.xml

<?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 package="com.cnblogs.hibernate_first">
    <class name="Role" table="t_role">
        <id name="id" column="t_id">
            <generator class="native"></generator>
        </id>
        <property name="name" column="t_name" />    
            
    </class>
</hibernate-mapping>
View Code

test 类

package com.cnblogs.hibernate_first;

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

import javax.persistence.EntityTransaction;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import junit.framework.TestCase;

public class many2manyTest extends TestCase {

    public void testSave1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();

            session.beginTransaction();
            Role role1 = new Role();
            role1.setName("数据录入人员");
            session.save(role1);
            Role role2 = new Role();
            role2.setName("商务主管");
            session.save(role2);
            Role role3 = new Role();
            role3.setName("商务经理");
            session.save(role3);
            Role role4 = new Role();
            role4.setName("项目会计");
            session.save(role4);

            User user1 = new User();
            user1.setName("张三");
            Set roles = new HashSet<>();
            roles.add(role1);
            roles.add(role2);
            user1.setRoles(roles);
            session.save(user1);
            // 错误示例
            // User user2 = new User();
            // user2.setName("李四");
            // user2.getRoles().add(role1);
            // 如果这么写的话,这里会报空指针异常。因为getRoles()方法获得不到set
            // user2.getRoles().add(role2);
            // user2.getRoles().add(role3);
            // session.save(user2);

            session.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }

    public void testLoad2() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            User user1 = session.load(User.class, 1);
            System.out.println("user1 的姓名是 :" + user1.getName() + "他的职责是 :" + user1.getRoles().toString());

        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

    }

}
View Code

 

转载于:https://www.cnblogs.com/Juli/p/8334286.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值