博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Hibernate】---【注解】一对多
阅读量:5079 次
发布时间:2019-06-12

本文共 5453 字,大约阅读时间需要 18 分钟。

一、核心配置文件

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

 

 

补充

转载于:https://www.cnblogs.com/angelye/p/7464883.html

你可能感兴趣的文章
软件工程团队作业3
查看>>
python标准库——queue模块 的queue类(单向队列)
查看>>
火狐、谷歌、IE关于document.body.scrollTop和document.documentElement.scrollTop 以及值为0的问题...
查看>>
深入理解JVM读书笔记--字节码执行引擎
查看>>
vue-搜索功能-实时监听搜索框的输入,N毫秒请求一次数据
查看>>
批处理 windows 服务的安装与卸载
查看>>
React文档翻译 (快速入门)
查看>>
nodejs fs路径
查看>>
动态规划算法之最大子段和
查看>>
linux c:关联变量的双for循环
查看>>
深入浅出理解zend framework(三)
查看>>
python语句----->if语句,while语句,for循环
查看>>
javascript之数组操作
查看>>
LinkedList源码分析
查看>>
TF-IDF原理
查看>>
用JS制作博客页面背景随滚动渐变的效果
查看>>
JavaScript的迭代函数与迭代函数的实现
查看>>
一步步教你学会browserify
查看>>
Jmeter入门实例
查看>>
亲近用户—回归本质
查看>>