用hibernate怎么实现一个表中多个字段的联合唯一

<? 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 ="onlyfun.caterpillar.User" table ="user" >

       
< composite-id >

           
< key-property name ="name"

                          column
="name"

                          type
="java.lang.String" />

           
< key-property name ="sex"

                          column
="sex"

                          type
="java.lang.String" />

       
</ composite-id >

   
</ class >

</ hibernate-mapping >




并且定义的User类要覆盖equals和hashCode方法
Java code
    
    
    
    
public boolean equals(Object obj) { if (obj == this ) { return true ; } if ( ! (obj instanceof User)) { return false ; } User user = (User) obj; return new EqualsBuilder() .append( this .name, user.getName()) .append( this .sex, user.getSex()) .isEquals(); } public int hashCode() { return new HashCodeBuilder() .append( this .name) .append( this .sex) .toHashCode(); }

你可能感兴趣的:(Hibernate,object,user,table,equals,encoding)