hibernate 复合主键

hibernate复合主键

在实体中可以定义一个嵌入式组件(embeddedcomponent),甚至覆盖该实体中原有的列映射.组件类必须在类一级定义@Embeddable注解.

在特定的实体的关联属性上使用@Embedded和@AttributeOverride注解可以覆盖该属性对应的嵌入式对象的列映射:

作为嵌入式主键类,要满足以下几点要求。

必须实现Serializable接口、必须有默认的public无参数的构造方法、必须覆盖equals和hashCode方法,这些要求与使用复合主键的要求相同。

将嵌入式主键类使用@Embeddable标注,表示这个是一个嵌入式类。

//方法1

importjavax.persistence.Embeddable;

@Embeddable

publicclassDemoPK1implementsSerializable{

privatestaticfinallongserialVersionUID=-3304319243957837925L;

privatelongid;

privateStringname;

publiclonggetId(){

returnid;

}

publicvoidsetId(longid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

@Override

publicbooleanequals(Objecto){

if(oinstanceofDemoPK1){

DemoPK1key=(DemoPK1)o;

if(this.id==key.getId()&&this.name.equals(key.getName())){

returntrue;

}

}

returnfalse;

}

@Override

publicinthashCode(){

returnthis.name.hashCode();

}

}

@Entity

@Table(name="Test")

publicclassDemo1implementsSerializable{

{

privateStringemail;

privateStringphone;

privateStringDemoPK1pk;

@Id

publicDemoPK1getPk(){

returnpk;

}

publicvoidsetPk(DemoPK1pk1){

this.pk=pk1;

}

@Column(name="phone",length=20)

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

@Column(name="email",length=23)

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

//省略getset方法注视

}

//方法2

publicclassDemoPK2implementsSerializable{

privatestaticfinallongserialVersionUID=-3304319243957837925L;

privatelongid;

privateStringname;

publiclonggetId(){

returnid;

}

publicvoidsetId(longid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

@Override

publicbooleanequals(Objecto){

if(oinstanceofDemoPK2){

DemoPK2key=(DemoPK2)o;

if(this.id==key.getId()&&this.name.equals(key.getName())){

returntrue;

}

}

returnfalse;

}

@Override

publicinthashCode(){

returnthis.name.hashCode();

}

}

@Entity

@Table(name="Test")

publicclassDemo2implementsSerializable{

{

privateStringemail;

privateStringphone;

privateStringDemoPK2pk;

@EmbeddedId

publicDemoPK2getPk(){

returnpk;

}

publicvoidsetPk(DemoPK2pk1){

this.pk=pk1;

}

@Column(name="phone",length=20)

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

@Column(name="email",length=23)

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

//省略getset方法注视

}

//方法3

publicclassDemoPK3implementsSerializable{

privatestaticfinallongserialVersionUID=-3304319243957837925L;

privatelongid;

privateStringname;

publiclonggetId(){

returnid;

}

publicvoidsetId(longid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

@Override

publicbooleanequals(Objecto){

if(oinstanceofDemoPK3){

DemoPK3key=(DemoPK3)o;

if(this.id==key.getId()&&this.name.equals(key.getName())){

returntrue;

}

}

returnfalse;

}

@Override

publicinthashCode(){

returnthis.name.hashCode();

}

}

@Entity

@Table(name="Test")

@IdClass(DemoPK3.class)

publicclassDemo3{

privateStringemail;

privateStringphone;

@Column(name="phone",length=20)

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

@Column(name="email",length=23)

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

}

下面看下配置文件:

<?xmlversion='1.0'encoding='utf-8'?>

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>

<propertyname="connection.url">jdbc:mysql://localhost/hibernate</property>

<propertyname="connection.username">root</property>

<propertyname="connection.password">root</property>

<propertyname="connection.pool_size">100</property>

<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>

<propertyname="current_session_context_class">thread</property>

<propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<propertyname="show_sql">true</property>

<propertyname="format_sql">true</property>

<!--Dropandre-createthedatabaseschemaonstartup-->

<!--<propertyname="hbm2ddl.auto">create</property>-->

<mappingclass="com.Demo1"/>

<mappingclass="com.Demo2"/>

<mappingclass="com.Demo3"/>

</session-factory>

</hibernate-configuration>

//参考连接:

http://blog.csdn.net/ly5156/article/details/6755585JPA实体的注解规范以及Hibernate特有的扩展

//测试代码:

criteria.addOrder(Order.desc("pk.id"));//对符合主键排序注意的地方

criteria.addOrder(Order.desc("pk.name"));

Listlist=criteria.list();

相关推荐