两种方式配置Hibernate复合主键(修正)

数据库结构

create table component(name varchar(50) not null,
                       sex varchar(50) not null,
                       description varchar(50),
                       primary key(name,sex));

 

主键类,一定要实现Serializable接口,并改写equals和hascode方法

 

package  component;

import  java.io.Serializable;

import  org.apache.commons.lang.builder.EqualsBuilder;
import  org.apache.commons.lang.builder.HashCodeBuilder;

public   class  ComponentPK  implements  Serializable  {
  
private String name;
  
private String sex;

public String getName() {
    
return name;
}

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

public String getSex() {
    
return sex;
}

public void setSex(String sex) {
    
this.sex = sex;
}

public boolean equals(Object obj) {
    
if(obj == this{
        
return true;
    }

    
    
if(!(obj instanceof Component)) {
        
return false;
    }

    
    ComponentPK componentpk 
= (ComponentPK) obj;
    
return new EqualsBuilder()
          .append(
this.name, componentpk.getName())
          .append(
this.sex, componentpk.getSex())
          .isEquals();
    
  }

  
  
public int hashCode() {
    
return new HashCodeBuilder()
          .append(
this.name)
          .append(
this.sex)
          .toHashCode();
  }


}

 

 

package  component;

public   class  Component  {
  
private String description;
  
private ComponentPK componentpk;
public ComponentPK getComponentpk() {
    
return componentpk;
}

public void setComponentpk(ComponentPK componentpk) {
    
this.componentpk = componentpk;
}

public String getDescription() {
    
return description;
}

public void setDescription(String description) {
    
this.description = description;
}

}

 

HBM文件

 

<? 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 ="component" >  

  
< class  name ="Component"  table ="component" >  
    
< composite-id  name ="componentpk"
              class
="ComponentPK"
              unsaved-value
="any" >
        
< key-property  name ="name"  
                column
="name"  
                type
="java.lang.String" />
        
< key-property  name ="sex"  
                column
="sex"  
                type
="java.lang.String" />
    
</ composite-id >
    
    
< property  name ="description"  column ="description"  type ="java.lang.String" />
  
  
</ 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();
        
        ComponentPK pk
=new ComponentPK();
        pk.setName(
"1");
        pk.setSex(
"3");
        
        Component component
=new Component();
        component.setComponentpk(pk);
        component.setDescription(
"12");
        session.save(component);
        t.commit();
        System.out.println(
"success");

    }

 

 

如果组成复合主键的某一个属性是其他持久话类的话,则需要使用<key many-to-one>

增加表User:

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

增加持久化类User及相应的Mapping文件

 

package  component;

public   class  User  {
   
private String id;
   
private String pass;
public String getId() {
    
return id;
}

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

public String getPass() {
    
return pass;
}

public void setPass(String pass) {
    
this.pass = pass;
}

}


<?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
= " component "

  <class name
= " User "  table = " user "
   
    <id name
= " id "  column = " id " >
      <generator class
= " assigned " ></generator>
    </id>
    <property name
= " pass "  column = " pass "  type = " java.lang.String " />
  
  </class> 

</hibernate-mapping>


修改ComponentPK类

 

package  component;

import  java.io.Serializable;

import  org.apache.commons.lang.builder.EqualsBuilder;
import  org.apache.commons.lang.builder.HashCodeBuilder;

public   class  ComponentPK  implements  Serializable  {
  
private String name;
  
private String sex;
  
private User user;
public User getUser() {
    
return user;
}

public void setUser(User user) {
    
this.user = user;
}

public String getName() {
    
return name;
}

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

public String getSex() {
    
return sex;
}

public void setSex(String sex) {
    
this.sex = sex;
}

public boolean equals(Object obj) {
    
if(obj == this{
        
return true;
    }

    
    
if(!(obj instanceof Component)) {
        
return false;
    }

    
    ComponentPK componentpk 
= (ComponentPK) obj;
    
return new EqualsBuilder()
          .append(
this.name, componentpk.getName())
          .append(
this.sex, componentpk.getSex())
          .isEquals();
    
  }

  
  
public int hashCode() {
    
return new HashCodeBuilder()
          .append(
this.name)
          .append(
this.sex)
          .toHashCode();
  }


}


修改Component.hbm.xml
< hibernate-mapping  package ="component" >  

  
< class  name ="Component"  table ="component" >  
    
< composite-id  name ="componentpk"
              class
="ComponentPK"
              unsaved-value
="any" >
        
< key-property  name ="name"  
                column
="name"  
                type
="java.lang.String" />
        
< key-property  name ="sex"  
                column
="sex"  
                type
="java.lang.String" />
        
< key-many-to-one  name ="user"  class ="User"  column ="userid"  access ="field"  foreign-key ="id" ></ key-many-to-one >
    
</ composite-id >
    
    
< property  name ="description"  column ="description"  type ="java.lang.String" />
  
  
</ 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();
        
        User user
=(User)session.get(User.class"11111");
    
        
        ComponentPK pk
=new ComponentPK();
        pk.setName(
"1");
        pk.setSex(
"3");
        pk.setUser(user);
        
        Component component
=new Component();
        component.setComponentpk(pk);
    
        component.setDescription(
"12");
        session.save(component);
        t.commit();
        System.out.println(
"success");

    }


可以看到数据库结果如下:
User(id为111111)

idpass
11111 !234

Component的联合主键(name,sex,userid),其中userid已经引用了user表的主键

namesexdescriptionuserid
1 3 1211111

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值