一、核心配置文件
com.mysql.jdbc.Driver root jdbc:mysql://localhost:3306/test root true true update org.hibernate.dialect.MySQLDialect thread
二、实体类
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 Setroles = new HashSet (); public Set getRoles() { return roles; } public void setRoles(Set 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(); }}
补充