hibernate的主键和复合主键

来源:互联网 发布:大学生表白知乎 编辑:程序博客网 时间:2024/05/22 06:46

一般的数据库都是有主键或者复合主键的,在hibernate中也明确要求使用的表需要具有主键或者复合主键,在XX.hbm.xml中如果缺少id或者composite-id就会报错。如果我们使用的表没有主键和复合主键,使用myeclipse生成的hibernate文件会默认将整个表的字段组成一个复合主键。

对于有复合主键但是没有主键的表,需要生成两个bean类: XX.java和XXId.java,以及一个XX.hbm.xml文件

需要注意的是在XXId.java中需要对equals()和hashCode()方法重写

1. XX.hbm.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.rt.bean.relation" table="relation" catalog="test">
        <composite-id name="id" class="com.rt.bean.relationId">
            <key-property name="memberId" type="java.lang.Integer">
                <column name="member_id" />
            </key-property>
            <key-property name="companyId" type="java.lang.Integer">
                <column name="company_id" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

2. XX.java
package com.rt.bean;

/**
 * relation entity. @author MyEclipse Persistence Tools
 */

public class relation implements java.io.Serializable {

// Fields

private relationId id;

// Constructors

/** default constructor */
public relation() {
}


/** minimal constructor */
public relation(relationId id) {
this.id = id;
}

// Property accessors

public relationId getId() {
return this.id;
}

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

3. XXId.java

package com.rt.bean;

/**
 * relationId entity. @author MyEclipse Persistence Tools
 */

public class relationId implements java.io.Serializable {

// Fields

private Integer memberId;
private Integer companyId;

// Constructors

/** default constructor */
public relationId() {
}

/** full constructor */
public relationId(Integer memberId, Integer companyId) {
this.memberId = memberId;
this.companyId = companyId;
}

// Property accessors
public Integer getMemberId() {
return this.memberId;
}

public void setMemberId(Integer memberId) {
this.memberId = memberId;
}

public Integer getCompanyId() {
return this.companyId;
}

public void setCompanyId(Integer companyId) {
this.companyId = companyId;
}

public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof relationId))
return false;
relationId castOther = (relationId) other;

return ((this.getMemberId() == castOther.getMemberId()) || (this
.getMemberId() != null && castOther.getMemberId() != null && this
.getMemberId().equals(castOther.getMemberId())))
&& ((this.getCompanyId() == castOther.getCompanyId()) || (this
.getCompanyId() != null
&& castOther.getCompanyId() != null && this
.getCompanyId().equals(castOther.getCompanyId())));
}

public int hashCode() {
int result = 17;
result = 37 * result
+ (getMemberId() == null ? 0 : this.getMemberId().hashCode());
result = 37 * result
+ (getCompanyId() == null ? 0 : this.getCompanyId().hashCode());
return result;
}

}





原创粉丝点击