首页 > 代码库 > hibernate 多对多

hibernate 多对多

hibernate 核心配置文件

 

<?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>
        <!-- DataSource配置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        
        <!-- 指定连接数据库所使用的SQL方言-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- 控制台是否输出SQL语句 -->
        <property name="show_sql">true</property>
        <!-- 式化Hibernate的SQL输出语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 指定程序是否在数据库自动创建表 -->
        <property name="hbm2ddl.auto">update</property>
        <!-- 指定映射文件的路径 -->
        <mapping resource="zr/com/pojo/Role.hbm.xml"/>
        <mapping resource="zr/com/pojo/User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>



        
        
        
<?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="zr.com.pojo.User" table="user">
        <!-- 主键 -->
        <id name="id" column="user_id">
            <!-- 主键生成策略 -->
            <generator class="native"/>
        </id>
        <property name="name" column="user_name"/>
        <!-- 多对多 
            name:集合的属性
            table:第三张表
        -->
        <set name="roles" table="user_role">
            <!-- 配置当前映射文件在第三张表外键名称 -->
            <key column="user_id"/>
            <!-- 
                column:角色在第三张表外键的名称
             -->
            <many-to-many column="role_id" class="zr.com.pojo.Role"></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="zr.com.pojo.Role" table="role">
        <!-- 主键 -->
        <id name="id" column="role_id">
            <!-- 主键生成策略 -->
            <generator class="native"/>
        </id>
        <property name="roleName" column="role_name"/>
        <!-- 
            多对多
            name:集合的属性
            table:第三张表
         -->
         <set name="users" table="user_role">
             <!-- 配置当前映射文件在第三张表外键名称 -->
             <key column="role_id"/>
             <!-- 
                column:角色在第三张表外键的名称
             -->
             <many-to-many class="zr.com.pojo.User" column="user_id"/>
         </set>
    </class>
    
</hibernate-mapping>
package zr.com.pojo;

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

public class Role {
    
    private int id;
    private String roleName;
    
    private Set<User> users = new HashSet<>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Role() {
        super();
    }

    public Role(int id, String roleName, Set<User> users) {
        super();
        this.id = id;
        this.roleName = roleName;
        this.users = users;
    }

    @Override
    public String toString() {
        return "Role [id=" + id + ", roleName=" + roleName + ", users=" + users
                + "]";
    }
    
    
    
    
    
}
package zr.com.pojo;

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

public class User {
    
    private int id;
    private String name;

    private Set<Role> roles = new HashSet<Role>();

    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<Role> getRoles() {
        return roles;
    }

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

    public User() {
        super();
    }

    public User(int id, String name, Set<Role> roles) {
        super();
        this.id = id;
        this.name = name;
        this.roles = roles;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", roles=" + roles + "]";
    }  
    
    
}
package zr.com.test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;



public class TestClass {
    
    public static void main(String[] args) {
        
        // 创建Configuration 对象
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        // 创建ServiceRegistry 对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        // 创建SessionFactory 对象
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // 创建Session 对象
        Session session = sessionFactory.openSession();
        
        // 开启事务
        session.beginTransaction();
        // 创建实体类
        
        
        
        
        
        // 提交事务
        session.getTransaction().commit();
        
    
    }
}

 

hibernate 多对多