Hibernate---注解一对多

Posted Angelye

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate---注解一对多相关的知识,希望对你有一定的参考价值。

一、核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/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">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <!-- 
        <mapping resource="com/chinasofti/entity/User.hbm.xml" />
        <mapping resource="com/chinasofti/entity/Role.hbm.xml" />
        -->
        <mapping class="com.chinasofti.entity.User"/>
        <mapping class="com.chinasofti.entity.Role"/>
    </session-factory>
</hibernate-configuration>

二、实体类

  User

package com.chinasofti.entity;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="T_USER2")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer user_id;
    private String user_name;
    private String user_password;
    
    @OneToMany(mappedBy="user",cascade=CascadeType.REMOVE)
    private Set<Role> roles = new HashSet<Role>(); 

    public Set<Role> getRoles() {
        return roles;
    }
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
    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_password() {
        return user_password;
    }
    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }
    @Override
    public String toString() {
        return "User [user_id=" + user_id + ", user_name=" + user_name
                + ", user_password=" + user_password + "]";
    }
}

  Role

package com.chinasofti.entity;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name="T_ROLE2")
public class Role {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer role_id;
    private String role_name;
    private String role_memo;
    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    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_memo() {
        return role_memo;
    }

    public void setRole_memo(String role_memo) {
        this.role_memo = role_memo;
    }

    @Override
    public String toString() {
        return "Role [role_id=" + role_id + ", role_name=" + role_name
                + ", role_memo=" + role_memo + "]";
    }

}

三、封装类

package com.chinasofti.entity;

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

public class HibernateUtil {
    static Configuration CONFIGURATION = null;
    static SessionFactory SESSION_FACTORY = null;
    static Session SESSION = null;
    static{
        CONFIGURATION = new Configuration();
        CONFIGURATION.configure();
        SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
        SESSION    = SESSION_FACTORY.openSession();
    }
    public static SessionFactory getSessionFactory(){
        return SESSION_FACTORY;
    }
    public static Session openSession(){
        return SESSION;
    }
}

四、测试类

package com.chinasofti.entity;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class TestAnnotation {
    
    @Test
    public void removeRoleForUser(){
        Session session = HibernateUtil.openSession();
        Transaction transaction = session.beginTransaction();
        //先找到左冷禅
        //User zuolengchan = session.get(User.class, 1);
        //再找到角色-掌门
        Role zhangmen = session.get(Role.class, 1);
        session.delete(zhangmen);
        //session.save(zuolengchan);
        //session.update(zuolengchan);
        transaction.commit();
        session.close();
    }
    
    @Test
    public void testAnnotationOneToMany(){
        Session session = HibernateUtil.openSession();
        Transaction transaction = session.beginTransaction();
        User user = new User();
        user.setUser_name("左冷禅");
        user.setUser_password("guniang");
        
        //角色
        Role role1 = new Role();
        role1.setRole_name("掌门");
        role1.setRole_memo("zhangmen");
        
        //角色
        Role role2 = new Role();
        role2.setRole_name("教主");
        role2.setRole_memo("jiaozhu");
        role1.setUser(user);
        role2.setUser(user);
        //给左冷禅添加角色
        user.getRoles().add(role1);
        user.getRoles().add(role2);
        
        //保存左冷禅-级联角色
        session.save(user);
        transaction.commit();
        session.close();
    }
}

 

 

补充

以上是关于Hibernate---注解一对多的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 一对多注解 实例

HIbernate 一对多双向注解

Hibernate 一对一对多多对多注解cascade属性的总结

Hibernate 一对一对多多对多注解属性的总结

2.2Hibernate用注解方式实现一对多多对多关系

2.2Hibernate用注解方式实现一对多多对多关系