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

系统 1786 0

数据库结构

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

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论