Hibernate Map映射的index-many-to-many案例

使用Map映射,index部分可以使用组件,也可以使用持久话类,本例使用<index-many-to-many>来实现使用持久话类为map映射定义index

数据库结构:

 

CREATE   TABLE  `team4` (
  `id` 
varchar ( 50 NOT   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `member4` (
  `id` 
varchar ( 50 NOT   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  `age` 
varchar ( 50 default   NULL ,
  `team` 
varchar ( 50 default   NULL ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;

CREATE   TABLE  `position` (
  `id` 
varchar ( 50 NOT   NULL ,
  `role` 
varchar ( 50 NOT   NULL ,
  `scene` 
varchar ( 50 NOT   NULL ,
  `bestchoice` 
varchar ( 50 NOT   NULL ) ENGINE = InnoDB  DEFAULT  CHARSET = gb2312;

 
持久话类:

package  mapindexmanytomany;

import  java.util.HashMap;
import  java.util.Map;

public   class  Team  {
   
private String id;
   
private String name;
   
private Map members=new HashMap();
public String getId() {
    
return id;
}

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



public Map getMembers() {
    
return members;
}

public void setMembers(Map members) {
    
this.members = members;
}

public String getName() {
    
return name;
}

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

}



package  mapindexmanytomany;

import  java.util.HashMap;
import  java.util.Map;
import  java.util.Set;

public   class  Member  {
   
private String id;
   
private String name;
   
private Position postition;
   
private Team team;
   
private String age;
public String getAge() {
    
return age;
}

public void setAge(String age) {
    
this.age = age;
}


public String getName() {
    
return name;
}

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



public Position getPostition() {
    
return postition;
}

public void setPostition(Position postition) {
    
this.postition = postition;
}

public String getId() {
    
return id;
}

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

public Team getTeam() {
    
return team;
}

public void setTeam(Team team) {
    
this.team = team;
}





}



package  mapindexmanytomany;

public   class  Position  {
    
private String id;
  
private String role;
  
private String scene;
  
private Member bestChoice;
public Member getBestChoice() {
    
return bestChoice;
}

public void setBestChoice(Member bestChoice) {
    
this.bestChoice = bestChoice;
}

public String getRole() {
    
return role;
}

public void setRole(String role) {
    
this.role = role;
}

public String getScene() {
    
return scene;
}

public void setScene(String scene) {
    
this.scene = scene;
}

public String getId() {
    
return id;
}

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

}


映射文件:
其中  <index-many-to-many class="Position" column="id"></index-many-to-many>定义了,数据表Position的主键id值使用Member表的主键数值

 

<? 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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="mapindexmanytomany" >
    
< class  name ="Team"  table ="team4" >
       
< id  name ="id"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="name"  column ="name" />
       
< map  name ="members"  inverse ="false"  cascade ="all" >
          
< key  column ="team" ></ key >
          
< index-many-to-many  class ="Position"  column ="id" ></ index-many-to-many >
          
< one-to-many  class ="Member" />
       
</ map >
    
</ class >
</ hibernate-mapping >


<? 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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="mapindexmanytomany" >
   
    
      
< class  name ="Member"  table ="member4" >
       
< id  name ="id"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="name"  column ="name" />
       
< property  name ="age"  column ="age" />
       
< many-to-one  name ="team"   ></ many-to-one >
    
</ class >
</ hibernate-mapping >

<? 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"
>
<!--  
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping  package ="mapindexmanytomany" >
   
    
      
< class  name ="Position"  table ="position" >
       
< id  name ="id"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="role"  column ="role" />
       
< property  name ="scene"  column ="scene" />
       
< many-to-one  name ="bestChoice"  column ="bestchoice"  cascade ="save-update" ></ many-to-one >
    
</ class >
</ hibernate-mapping >


测试代码:

 

public   static   void  main(String[] args)  {
        Configuration cfg
=new Configuration();
        cfg.configure();
        SessionFactory sf
=cfg.buildSessionFactory();
        Session session
=sf.openSession();
        Transaction t
=session.beginTransaction();

        Team team1
=new Team();
        team1.setName(
"足球队-01");
    
        
        Member m1
=new Member();
        m1.setName(
"张三");
        m1.setAge(
"11");

        
        Member m2
=new Member();
        m2.setName(
"张四");
        m2.setAge(
"22");
        
        Position p1
=new Position();
        p1.setRole(
"守门员");
        p1.setScene(
"上半场");
        p1.setBestChoice(m1);
        
        Position p2
=new Position();
        p2.setRole(
"中后卫");
        p2.setScene(
"下半场");
        p2.setBestChoice(m2);
        
        team1.getMembers().put(p1, m1);
        team1.getMembers().put(p2, m2);

        

        session.save(p1);
        session.save(p2);
        session.save(team1);

        t.commit();
        System.out.println(
"success");

    }

运行结果:
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into team4 (name, id) values (?, ?)
Hibernate: update member4 set team=?, id=? where id=?
Hibernate: update member4 set team=?, id=? where id=?
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值