首页 > 其他分享 >Hibernate多对多双向关联(Annotation配置)

Hibernate多对多双向关联(Annotation配置)

时间:2023-05-08 12:32:06浏览次数:40  
标签:Hibernate javax name Annotation public 双向 import id persistence




Hibernate多对多双向关联(Annotation配置)_Test


Role.java


package  com.many2many.bean;

import  java.util.Set;

import  javax.persistence.CascadeType;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.ManyToMany;
import  javax.persistence.Table;

@Entity
@Table(name = " roles " ,catalog = " Hibernate_Many2Many " )
public   class  Role {
    
     private   int  id;
     private  String name;
     private  Set < User >  users;
    @Id
    @Column(name = " id " )
    @GeneratedValue(strategy = GenerationType.AUTO)
     public   int  getId() {
         return  id;
    }
     public   void  setId( int  id) {
         this .id  =  id;
    }
    @Column(name = " name " )
     public  String getName() {
         return  name;
    }
     public   void  setName(String name) {
         this .name  =  name;
    }
    @ManyToMany(mappedBy = " roles " ,cascade = CascadeType.ALL)
     public  Set < User >  getUsers() {
         return  users;
    }
     public   void  setUsers(Set < User >  users) {
         this .users  =  users;
    }
    
}
User.java 
package  com.many2many.bean;

import  java.util.Set;

import  javax.persistence.CascadeType;
import  javax.persistence.Column;
import  javax.persistence.Entity;
import  javax.persistence.GeneratedValue;
import  javax.persistence.GenerationType;
import  javax.persistence.Id;
import  javax.persistence.JoinColumn;
import  javax.persistence.JoinTable;
import  javax.persistence.ManyToMany;
import  javax.persistence.Table;

@Entity
@Table(name = " users " ,catalog = " Hibernate_Many2Many " )
public   class  User {
    
     private   int  id;
     private  String name;
     private  Set < Role >  roles;
    @Id
    @Column(name = " id " )
    @GeneratedValue(strategy = GenerationType.AUTO)
     public   int  getId() {
         return  id;
    }
     public   void  setId( int  id) {
         this .id  =  id;
    }
    @Column(name = " name " )
     public  String getName() {
         return  name;
    }
     public   void  setName(String name) {
         this .name  =  name;
    }
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = " user_role " ,
            joinColumns = @JoinColumn(name = " user_id " ),
            inverseJoinColumns = @JoinColumn(name = " role_id " )
    )
     public  Set < Role >  getRoles() {
         return  roles;
    }
     public   void  setRoles(Set < Role >  roles) {
         this .roles  =  roles;
    }
    
}
导出的sql 
create   table  Hibernate_Many2Many.roles (
        id  integer   not   null  auto_increment,
        name  varchar ( 255 ),
         primary   key  (id)
    )

     create   table  Hibernate_Many2Many.users (
        id  integer   not   null  auto_increment,
        name  varchar ( 255 ),
         primary   key  (id)
    )

     create   table  user_role (
         user_id   integer   not   null ,
        role_id  integer   not   null ,
         primary   key  ( user_id , role_id)
    )

     alter   table  user_role 
         add   index  FK143BF46ADA6B394F (role_id), 
         add   constraint  FK143BF46ADA6B394F 
         foreign   key  (role_id) 
         references  Hibernate_Many2Many.roles (id)

     alter   table  user_role 
         add   index  FK143BF46A7F95FD2F ( user_id ), 
         add   constraint  FK143BF46A7F95FD2F 
         foreign   key  ( user_id ) 
         references  Hibernate_Many2Many.users (id)

测试代码 
@Test
     public   void  insert(){
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = session.beginTransaction();
         try  {
            transaction.begin();
            User user = new  User();
            user.setName( " 张三 " );
            
            Role role = new  Role();
            role.setName( " 管理员 " );
            Set < Role >  roles = new  HashSet < Role > ();
            roles.add(role);
            user.setRoles(roles);
            
            session.persist(user);
            
            transaction.commit();
        }  catch  (HibernateException e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }
    
    
    @Test
     public   void  select(){
        Session session = HibernateSessionFactory.getSession();
        User user = (User) session.get(User. class ,  1 );
        System.out.println(user.getName());
        Set < Role >  roles = user.getRoles();
         for  (Iterator < Role >  iterator  =  roles.iterator(); iterator.hasNext();) {
            Role role  =  (Role) iterator.next();
            System.out.println(role.getName());
        }
    }

标签:Hibernate,javax,name,Annotation,public,双向,import,id,persistence
From: https://blog.51cto.com/u_16065168/6253466

相关文章

  • Hibernate多对多双向关联(xml配置)
    Role.javapackagecom.many2many.bean;importjava.util.Set;publicclassRole{privateintid;privateStringname;privateSet<User>users;publicintgetId(){returnid;}public......
  • 双向LLC变换器模型,采用半桥结构,电路拓扑为LLC结构,注意不是CLLC!!!
    双向LLC变换器模型,采用半桥结构,电路拓扑为LLC结构,注意不是CLLC!!!输出电压闭环控制,闭环控制采用变频控制正反向运行效果如图所示对称半桥结构和不对称半桥结构都有联系请做备注模型运行环境包括matlab/simulink/plecs/等~ID:7440676714497107......
  • 非隔离双向DC/DC变换器 buck-boost变换器仿真
    非隔离双向DC/DC变换器buck-boost变换器仿真输入侧为直流电压源,输出侧接蓄电池模型采用电压外环电流内环的双闭环控制方式正向运行时电压源给电池恒流恒压充电,反向运行时电池放电维持直流侧电压稳定matlab/simulink仿真模型~ID:1639675030822110......
  • Hibernate4之二级缓存配置
    缓存:缓存是什么,解决什么问题? 位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为缓存Cache。缓存目的:让数据更接近于应用程序,协调速度不匹配,使访问速度更快。 缓存的范围分为3类: 1.事务范围(单Session即一级缓存)   事务范围......
  • spring和hibernate配置文件整合
    为了操作方便,我们经常在spring整合hibernate的WEB项目中省去了用hibernate.cfg.xml的麻烦,将hibernate的信息直接配置在Spring配置文件中下面的都是针对Spring整合Hibernate(注解方式的hibernate)来说的hibernate.cfg.xml和applicationContext.xml原始配置......
  • hibernate二级缓存(包括注解方式)
    二级缓存配置(spring+hibernate)说明:本人不建议使用查询缓存,因为查询缓存要求完全相同的查询sql语句才会起作用,所说的查询缓存是针对第二次查询时sql语句与第一次sql语句完全相同那么就可以从缓存中取数据而不去数据库中取数据了,在不启用查询缓存的情况下......
  • struts2+Spring+Hibernate+Jquery (json)
          Struts2文件的上传和下载http://wing929.javaeye.com/blog/216118基于struts+spring+hibernate+jquery的jmesa的分页实现样例http://wing929.javaeye.com/blog/216292http://hi.baidu.com/angel_lishiwen/blog/item/cf266627fcb59327d4074288.html stru......
  • 缓存----Ibatis /Hibernate
    iBatis缓存的使用方法及解释:以iBatis2.3为例,做以下配置即可实现对某些查询进行缓存1、<settingslazyLoadingEnabled="false" cacheModelsEnabled="true" enhancementEnabled="true"/>   注释:       lazyLoadingEnabled延迟加载数据;cacheModelsEna......
  • STM32单片机引脚要职能配置为输入或者输出模式,并不能像51一样准双向,那么如何进行但总
    如题随便找个端口举例对应的程序为 难道需要写之后立即初始化为输入?然后赶紧读?然后再赶紧初始化为输出?再往外写?是的,还真他妈就是这么傻逼的操作 ......
  • LSTM和双向LSTM介绍
    (一)RNN的长期依赖问题循环神经网络RNN在训练的过程中会有长期依赖的问题,这是由于RNN模型在训练时会遇到梯度消失(大部分情况)或者梯度爆炸(很少,但对优化过程影响很大)的问题。对于梯度爆炸是很好解决的,可以使用梯度修剪(GradientClipping),即当梯度向量大于某个阈值,缩放梯度向量......