【Hibernate】---【注解】一对多
阅读原文时间:2023年07月10日阅读:1

一、核心配置文件





com.mysql.jdbc.Driver
root
jdbc:mysql://localhost:3306/test
root

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

二、实体类

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

}

补充