Hibernate Map映射的composite-element案例

 在这个案例的hbm文件中,只有一个持久话类Team,Member作为class引入,map的关系被映射到teammember表中,

 

数据库结构:

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

CREATE   TABLE  `teammembers` (
  `teamid` 
varchar ( 50 NOT   NULL ,
  `name` 
varchar ( 50 default   NULL ,
  `age` 
varchar ( 50 default   NULL ,
  `teamrole` 
varchar ( 50 default   NULL ,
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;


持久类:

 

package  compositeelement;

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

public   class  Member  {
   
private String id;
   
private String name;
   
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 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  compositeelement;

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

}


映射文件:

 

<? 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 ="compositeelement" >
    
< import  class ="Member" />
    
< class  name ="Team"  table ="team5" >
       
< id  name ="id"  column ="id" >
         
< generator  class ="uuid.hex" ></ generator >
       
</ id >
       
< property  name ="name"  column ="name" />
       
< map  name ="members"  table ="teammembers"   inverse ="false"   cascade ="save-update" >
          
< key  column ="teamid" />
          
< index  column ="teamrole"  type ="string" ></ index >
          
< composite-element  class ="Member" >
             
< parent  name ="team" />
             
< property  name ="name" />
             
< property  name ="age" ></ property >
          
</ composite-element >
       
</ map >
    
</ 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");
        
        
        
        team1.getMembers().put(
"守门员", m1);
        team1.getMembers().put(
"中后卫", m2);


        session.save(team1);
        
        

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

    }

运行结果:
Hibernate: insert into team5 (name, id) values (?, ?)
Hibernate: insert into teammembers (teamid, teamrole, name, age) values (?, ?, ?, ?)
Hibernate: insert into teammembers (teamid, teamrole, name, age) values (?, ?, ?, ?)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值