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

转载地址:http://www.blogjava.net/sxyx2008/archive/2010/10/18/335490.html


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值