定义复合主键类:Override Object的equals和hasCode方法;同时实现java.io.Serializable接口。如下:
/*
* Created on 2007-02-05
*/
package com.snow.hib.pojos;
import java.io.Serializable;
import javax.persistence.Column;
/**
* 定义一个复合主键
* @author snowflower
*
*/
public class ComplexIDObject implements Serializable {
private static final long serialVersionUID = 1L;
private String pkA;
private String pkB;
/**
* @return Returns the pkA.
*/
@Column(name="keyA")
public String getPkA() {
return pkA;
}
/**
* @param pkA The pkA to set.
*/
public void setPkA(String pkA) {
this.pkA = pkA;
}
/**
* @return Returns the pkB.
*/
@Column(name="keyB")
public String getPkB() {
return pkB;
}
/**
* @param pkB The pkB to set.
*/
public void setPkB(String pkB) {
this.pkB = pkB;
}
public int hashCode() {
int result;
result = pkA.hashCode();
result = 29 * result + pkB.hashCode();
return result;
}
public boolean equals(Object obj) {
if(this == obj)return true;
if ( !( obj instanceof ComplexIDObject ) ) return false;
final ComplexIDObject o = (ComplexIDObject) obj;
if(!pkA.equals(o.getPkA()))return false;
if(!pkB.equals(o.getPkB()))return false;
return true;
}
}
下面是对上面符合主键类的使用,需要注意的和复合主键类似:
/*
* Created on 2007-02-05
*/
package com.snow.hib.pojos;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
*
* @author snowflower
*
*/
@Entity
@Table(name="complex_PK")
public class UseComplexIDObject implements Serializable {
private static final long serialVersionUID = 1L;
private ComplexIDObject comPk;
/**
* @return Returns the comPk.
*/
@EmbeddedId
public ComplexIDObject getComPk() {
return comPk;
}
/**
* @param comPk The comPk to set.
*/
public void setComPk(ComplexIDObject comPk) {
this.comPk = comPk;
}
public boolean equals(Object o) {
if ( this == o ) return true;
if ( !( o instanceof UseComplexIDObject ) ) return false;
final UseComplexIDObject parent = (UseComplexIDObject) o;
if ( !comPk.equals( parent.comPk ) ) return false;
return true;
}
public int hashCode() {
return comPk.hashCode();
}
}
下面是一段测试代码:
UseComplexIDObject comp = new UseComplexIDObject();
ComplexIDObject pk = new ComplexIDObject();
pk.setPkA("0001A");
pk.setPkB("0001B");
comp.setComPk(pk);
manager.saveComplexO(comp);
这样就OK了,至于Annotation已经在源文件中使用了,这里就不多说了。
分享到:
相关推荐
该zip包含 hibernate3.3.2相关jar与文档,hibernate annotation3.3.4相关jar与文档。
hibernate annotation中文文档
hibernate annotation hibernate3
hibernate中文APIhibernate annotation 中文API
学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例
hibernate annotation 中文文档
这里面包涵了需要用Hibernate Annotation时,所需要的所有jar包! 现在我们公司在做web项目的时候,已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用...
SpringMVC Spring Hibernate 框架整合 Annotation Maven Project
hibernate 注解 annotation 教程
Struts2+Spring2+Hibernate3+Annotation所需JAR包
孤独的狼-------------hibernate_annotation 注解实现Hibernate映射
Hibernate Annotation
hibernate helloword annotation version
hibernate-annotations相关的jar包
逐渐的Hibernate Annotation的映射方式也越来越盛行,相信过不了多久有可能会取代*.hbm.xml映射方式
Hibernate_3.2.0_Reference_zh_CN.chm hibernate_annotation.chm
myeclipse的自带hibernate jar包不支持注解;自己找的hibernate注解所需的jar包:hibernate-core;hibernate-annotation;hbm-cfg-xml;log4j.properties
在hibernate中使用annotation的demo
Hibernate annotation 3.4 api CHM