hibernate的主键和复合主键

一般的数据库都是有主键或者复合主键的,在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;
}

}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值