Hibernate关系映射总结(三)

 上一章对一对多单向关联进行了小结,关联映射在一方User里进行了如下配置:

          < set  name ="seats"  table ="seat"  cascade ="all" >
             
< key  column ="user_id"   />
             
< one-to-many  class ="com.mp.persistence.model.Seat"   />
         
</ set >

多对一单向关联也差不多,改在多方Seat的hbm.xml中加入如下关联映射代码就可以,同时需要把Seat.java里的user_id属性改为User对象,相应的把User中的Set<seat>属性去掉,数据库不用做修改。

         < many-to-one  name ="user"  column ="user_id"
                                                    class
="com.mp.persistence.model.User"  cascade ="all"   />

这章要总结多对多基于连接表双向关联,还是用第一章的例子,多个User对应多个Seat,一个人可以有多个座位,一个座位也可以有多个人。

Hibernate关系映射总结(三)_第1张图片

通过cmd进入mysql,执行以下语句修改表seat,增加一连接表user_seat.

alter   table  seat  drop   column   user_id  ;

create   table  user_seat (
    
user_id   char ( 32 ) ,
    seat_id 
char ( 32 )
);

User.java:


package  com.mp.persistence.model;

import  java.util.Set;

public   class  User
{

    
private String        id;
    
private String        name;
    
private Set<Seat>    seats;

    
public Set<Seat> getSeats()
    
{
        
return seats;
    }


    
public void setSeats(Set<Seat> seats)
    
{
        
this.seats = seats;
    }


    
public String getId()
    
{
        
return id;
    }


    
public void setId(String id)
    
{
        
this.id = id;
    }


    
public String getName()
    
{
        
return name;
    }


    
public void setName(String name)
    
{
        
this.name = name;
    }


}

Seat.java:


package  com.mp.persistence.model;

import  java.util.Set;

public   class  Seat
{

    
private String        id;
    
private String        place;
    
private Set<User>    users;

    
public Set<User> getUsers()
    
{
        
return users;
    }


    
public void setUsers(Set<User> users)
    
{
        
this.users = users;
    }


    
public String getId()
    
{
        
return id;
    }


    
public void setId(String id)
    
{
        
this.id = id;
    }


    
public String getPlace()
    
{
        
return place;
    }


    
public void setPlace(String place)
    
{
        
this.place = place;
    }

}

User.hbm.xml:

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping  PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
    
< class  name ="com.mp.persistence.model.User"  table ="user" >
        
< id  name ="id"  column ="id"  type ="java.lang.String" >
            
< generator  class ="uuid"   />
        
</ id >
        
< property  name ="name"  column ="name"  type ="java.lang.String"   />
        
        
< set  name ="seats"  table ="user_seat"  cascade ="save-update" >
            
< key  column ="user_id"   />
            
< many-to-many  class ="com.mp.persistence.model.Seat"  column ="seat_id"   />
        
</ set >
        
    
</ class >
</ hibernate-mapping >

set里的name与User.java里的seat对象属性对应,table为连接表,key column为关联表里关联user表的字段,many-to-many里的class为关联类,column 为连接表里关联类的关联字段。

Seat.hbm.xml:

<? xml version="1.0" encoding="utf-8" ?>
<! DOCTYPE hibernate-mapping  PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
    
< class  name ="com.mp.persistence.model.Seat"  table ="seat" >
        
< id  name ="id"  column ="id"  type ="java.lang.String" >
            
< generator  class ="uuid"   />
        
</ id >
        
< property  name ="place"  column ="place"  type ="java.lang.String"   />
        
        
< set  name ="users"  table ="user_seat"  cascade ="save-update"  inverse ="true"   >
            
< key  column ="seat_id"   />
            
< many-to-many  class ="com.mp.persistence.model.User"  column ="user_id"   />
        
</ set >
        
    
</ class >
</ hibernate-mapping >

测试代码:

package  com.mp.persistence.model;

import  java.util.HashSet;
import  java.util.Set;

import  org.hibernate.Session;
import  org.hibernate.SessionFactory;
import  org.hibernate.Transaction;
import  org.hibernate.cfg.Configuration;

/**
 * 类功能描述:测试hibernate映射关系
 *
 * 
@author <a href="mailto:[email protected]">likun </a>
 * 
@version $Id: codetemplates.xml,v 1.1 2007/12/07 02:35:38 likun Exp  $
 * Create:  2008-1-3 下午01:00:21
 
*/

public   class  TestHibernate
{

    
public static void main(String[] args)
    
{
        
// 获得hibernate.cfg.xml配置信息    
        Configuration config = new Configuration().configure();
        
// 根据 config 建立 SessionFactory       
        
// SessionFactory 将用于建立 Session      
        SessionFactory sessionFactory = config.buildSessionFactory();
        
        Seat seat 
= new Seat();
        seat.setPlace(
"三排一列");
        seat.setUsers(
new HashSet());
        Seat seat2 
= new Seat();
        seat2.setPlace(
"三排二列");
        seat2.setUsers(
new HashSet());
        
        User user 
= new User();
        user.setName(
"lucy");
        user.setSeats(
new HashSet());
        User user2 
= new User();
        user2.setName(
"lily");
        user2.setSeats(
new HashSet());
        
        
        user.getSeats().add(seat);
        user.getSeats().add(seat2);
        seat.getUsers().add(user);
        seat2.getUsers().add(user);
        
        user2.getSeats().add(seat);
        user2.getSeats().add(seat2);
        seat.getUsers().add(user2);
        seat2.getUsers().add(user2);
        
        
// 开启Session,相当于开启JDBC的Connection       
        Session session = sessionFactory.openSession();
        Transaction tx 
= session.beginTransaction();
        
// 保存实体类至数据库中     
        session.save(user);
        session.save(user2);
        tx.commit();
        session.close();
        sessionFactory.close();
        System.out.println(
"操作成功!");
    }

}

右键Run as /Java Application ,控制台显示信息:

Hibernate: insert into user (name ,  id) values (? ,  ?)
Hibernate: insert into seat (place
,  id) values (? ,  ?)
Hibernate: insert into user (name
,  id) values (? ,  ?)
Hibernate: insert into seat (place
,  id) values (? ,  ?)
Hibernate: insert into user_seat (user_id
,  seat_id) values (? ,  ?)
Hibernate: insert into user_seat (user_id
,  seat_id) values (? ,  ?)
Hibernate: insert into user_seat (user_id
,  seat_id) values (? ,  ?)
Hibernate: insert into user_seat (user_id
,  seat_id) values (? ,  ?)
操作成功!

切换到cmd界面,查询结果如下:Hibernate关系映射总结(三)_第2张图片

你可能感兴趣的:(Hibernate关系映射总结(三))