hibernate 复合主键映射

本文主要是学习hibernate的复合主键映射

复合主键表示数据库表中主键由两个字段决定,比如角色权限表,这个表的主键若没有配置额外的主键id,则主键是authid和roleid共同决定的。下面来看hibernate的配置方式:

1、主键id单独出一个类

实体类的定义如下:

public class RoleauthId implements java.io.Serializable {

	private long roleid;
	private long authid;

	public RoleauthId() {
	}

	public RoleauthId(long roleid, long authid) {
		this.roleid = roleid;
		this.authid = authid;
	}

	public long getRoleid() {
		return this.roleid;
	}

	public void setRoleid(long roleid) {
		this.roleid = roleid;
	}

	public long getAuthid() {
		return this.authid;
	}

	public void setAuthid(long authid) {
		this.authid = authid;
	}

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

		return (this.getRoleid() == castOther.getRoleid())
				&& (this.getAuthid() == castOther.getAuthid());
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result + (int) this.getRoleid();
		result = 37 * result + (int) this.getAuthid();
		return result;
	}

}

public class Roleauth implements java.io.Serializable {

    private RoleauthId id;
    private String memo;
}

配置文件如下:

<hibernate-mapping>
    <class name="com.study.hbm.Roleauth" table="roleauth" catalog="hibernate">
        <composite-id name="id" class="com.study.hbm.RoleauthId">
            <key-property name="roleid" type="long">
                <column name="roleid" />
            </key-property>
            <key-property name="authid" type="long">
                <column name="authid" />
            </key-property>
        </composite-id>
        <property name="memo" type="string">
            <column name="memo" length="128" />
        </property>
    </class>
</hibernate-mapping>
注意:

1)组合主键的id是使用composite-id标签设置的,

2)主键类必须继承Serializable接口,这是因为session的get/load操作主键都必须是Serializable类型的

3)重写equal和hashcode方法来确保主键的唯一性,因为是组合主键所以唯一性必须由应用程序来保证

2、主键id属性和普通属性也可以在同一个类中

实体类定义如下

package com.study.hbm;

// Generated 2015-1-12 14:36:28 by Hibernate Tools 3.4.0.CR1

/**
 * Roleauth generated by hbm2java
 */
public class Roleauth implements java.io.Serializable {

	private long roleid;
	private long authid;
	private String memo;

	public Roleauth() {
	}

	public Roleauth(long roleid, long authid) {
		this.roleid = roleid;
		this.authid = authid;
	}

	public Roleauth(long roleid, long authid, String memo) {
		this.roleid = roleid;
		this.authid = authid;
		this.memo = memo;
	}

	public long getRoleid() {
		return roleid;
	}

	public void setRoleid(long roleid) {
		this.roleid = roleid;
	}

	public long getAuthid() {
		return authid;
	}

	public void setAuthid(long authid) {
		this.authid = authid;
	}

	public String getMemo() {
		return this.memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
	
	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof RoleauthId))
			return false;
		RoleauthId castOther = (RoleauthId) other;

		return (this.getRoleid() == castOther.getRoleid())
				&& (this.getAuthid() == castOther.getAuthid());
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result + (int) this.getRoleid();
		result = 37 * result + (int) this.getAuthid();
		return result;
	}
	
	@Override
	public String toString() {
		return this.getRoleid() + " " + this.getAuthid() + " " + this.getMemo();
	}

}
配置文件如下:

<hibernate-mapping>
    <class name="com.study.hbm.Roleauth" table="roleauth" catalog="hibernate">
        <composite-id>
            <key-property name="roleid" type="long">
                <column name="roleid" />
            </key-property>
            <key-property name="authid" type="long">
                <column name="authid" />
            </key-property>
        </composite-id>
        <property name="memo" type="string">
            <column name="memo" length="128" />
        </property>
    </class>
</hibernate-mapping>
注意:

1)实体类和配置文件类都和第一种方式有点区别,但是区别不大,主要是composite-id中没有致命id以及类了。

2)主键的构造,这里是直接使用Roleauth类的对象构造的,没有使用新的主键对象了。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值