本文主要是学习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类的对象构造的,没有使用新的主键对象了。