当前位置: 移动技术网 > IT编程>开发语言>Java > java hibernate使用注解来定义联合主键

java hibernate使用注解来定义联合主键

2019年07月22日  | 移动技术网IT编程  | 我要评论

java  hibernate使用注解来定义联合主键

下面使用hibernate的api中说明的三种方式来定义主键,主要使用annotation来定义hibernate中的联合主键

下面取至hibernate的api文档:

定义组合主键的几种语法:

1、将组件类注解为@embeddable,并将组件的属性注解为@id
2、将组件的属性注解为@embeddedid
3、将类注解为@idclass,并将该实体中所有属于主键的属性都注解为@id

下面就分别使用这三种方式来定义联合主键。

建表的sql语句:

create table `syslogs` ( 
 `id` varchar(50) not null, 
 `yhid` varchar(50) not null, 
 `modelname` varchar(100) default null, 
 `content` varchar(500) default null, 
 `inserttime` varchar(20) default null, 
 `remark` varchar(50) default null, 
 primary key (`id`,`yhid`) 
) engine=innodb default charset=utf-8; 

一、将组件类注解为@embeddable

/** 
 * syslogsdtoid代表主键类 
 */ 
package com.hibernate.dto; 
 
import javax.persistence.embeddable; 
/** 
 * 1、主键类必须要实现java.io.serializable接口 
 * 2、主键类必须要重写equals和hashcode方法 
 * @author ibm 
 */ 
@embeddable 
public class syslogsdtoid implements java.io.serializable { 
 
  private static final long serialversionuid = 1l; 
  private string id; 
  private string yhid; 
 
  public syslogsdtoid() { 
  } 
 
  public syslogsdtoid(string id, string yhid) { 
    this.id = id; 
    this.yhid = yhid; 
  } 
 
  public string getid() { 
    return this.id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getyhid() { 
    return this.yhid; 
  } 
 
  public void setyhid(string yhid) { 
    this.yhid = yhid; 
  } 
 
  public boolean equals(object other) { 
    if ((this == other)) 
      return true; 
    if ((other == null)) 
      return false; 
    if (!(other instanceof syslogsdtoid)) 
      return false; 
    syslogsdtoid castother = (syslogsdtoid) other; 
 
    return ((this.getid() == castother.getid()) || (this.getid() != null && castother.getid() != null && this.getid().equals(castother.getid()))) 
        && ((this.getyhid() == castother.getyhid()) || (this.getyhid() != null && castother.getyhid() != null && this.getyhid().equals( 
            castother.getyhid()))); 
  } 
 
  public int hashcode() { 
    int result = 17; 
 
    result = 37 * result + (getid() == null ? 0 : this.getid().hashcode()); 
    result = 37 * result + (getyhid() == null ? 0 : this.getyhid().hashcode()); 
    return result; 
  } 
 
} 

/** 
 * syslogsdto为表对象映射类,其中主键为主键类syslogsdtoid 
 */ 
package com.hibernate.dto; 
 
import javax.persistence.column; 
import javax.persistence.entity; 
import javax.persistence.id; 
import javax.persistence.table; 
 
@entity 
@table(name = "syslogs") 
public class syslogsdto implements java.io.serializable { 
  private static final long serialversionuid = 1l; 
  private syslogsdtoid id; 
  private string modelname; 
  private string content; 
  private string inserttime; 
  private string remark; 
 
  public syslogsdto() { 
  } 
 
  public syslogsdto(syslogsdtoid id) { 
    this.id = id; 
  } 
 
  public syslogsdto(syslogsdtoid id, string modelname, string content, string inserttime, string remark) { 
    this.id = id; 
    this.modelname = modelname; 
    this.content = content; 
    this.inserttime = inserttime; 
    this.remark = remark; 
  } 
 
  @id 
  public syslogsdtoid getid() { 
    return this.id; 
  } 
 
  public void setid(syslogsdtoid id) { 
    this.id = id; 
  } 
 
  @column(name = "modelname", length = 100) 
  public string getmodelname() { 
    return this.modelname; 
  } 
 
  public void setmodelname(string modelname) { 
    this.modelname = modelname; 
  } 
 
  @column(name = "content", length = 500) 
  public string getcontent() { 
    return this.content; 
  } 
 
  public void setcontent(string content) { 
    this.content = content; 
  } 
 
  @column(name = "inserttime", length = 20) 
  public string getinserttime() { 
    return this.inserttime; 
  } 
 
  public void setinserttime(string inserttime) { 
    this.inserttime = inserttime; 
  } 
 
  @column(name = "remark", length = 50) 
  public string getremark() { 
    return this.remark; 
  } 
 
  public void setremark(string remark) { 
    this.remark = remark; 
  } 
 
} 

二、将组件的属性注解为@embeddedid

这种情况最简单,主键类只用定义主键字段,不需要写任何注解。然后在对象类中在主键类的get方法上加上@embeddedid注解。

/** 
 * syslogsdtoid代表主键类 
 */ 
package com.hibernate.dto; 
 
public class syslogsdtoid implements java.io.serializable { 
 
  private static final long serialversionuid = 1l; 
  private string id; 
  private string yhid; 
 
  public syslogsdtoid() { 
  } 
 
  public syslogsdtoid(string id, string yhid) { 
    this.id = id; 
    this.yhid = yhid; 
  } 
 
  public string getid() { 
    return this.id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getyhid() { 
    return this.yhid; 
  } 
 
  public void setyhid(string yhid) { 
    this.yhid = yhid; 
  } 
 
  public boolean equals(object other) { 
    if ((this == other)) 
      return true; 
    if ((other == null)) 
      return false; 
    if (!(other instanceof syslogsdtoid)) 
      return false; 
    syslogsdtoid castother = (syslogsdtoid) other; 
 
    return ((this.getid() == castother.getid()) || (this.getid() != null && castother.getid() != null && this.getid().equals(castother.getid()))) 
        && ((this.getyhid() == castother.getyhid()) || (this.getyhid() != null && castother.getyhid() != null && this.getyhid().equals( 
            castother.getyhid()))); 
  } 
 
  public int hashcode() { 
    int result = 17; 
 
    result = 37 * result + (getid() == null ? 0 : this.getid().hashcode()); 
    result = 37 * result + (getyhid() == null ? 0 : this.getyhid().hashcode()); 
    return result; 
  } 
 
} 



/** 
 * syslogsdto为表对象映射类,其中主键为主键类syslogsdtoid 
 */ 
package com.hibernate.dto; 
 
import javax.persistence.column; 
import javax.persistence.embeddedid; 
import javax.persistence.entity; 
import javax.persistence.table; 
 
@entity 
@table(name = "syslogs") 
public class syslogsdto implements java.io.serializable { 
  private static final long serialversionuid = 1l; 
  private syslogsdtoid id; 
  private string modelname; 
  private string content; 
  private string inserttime; 
  private string remark; 
 
  public syslogsdto() { 
  } 
 
  public syslogsdto(syslogsdtoid id) { 
    this.id = id; 
  } 
 
  public syslogsdto(syslogsdtoid id, string modelname, string content, string inserttime, string remark) { 
    this.id = id; 
    this.modelname = modelname; 
    this.content = content; 
    this.inserttime = inserttime; 
    this.remark = remark; 
  } 
 
  @embeddedid 
  public syslogsdtoid getid() { 
    return this.id; 
  } 
 
  public void setid(syslogsdtoid id) { 
    this.id = id; 
  } 
 
  @column(name = "modelname", length = 100) 
  public string getmodelname() { 
    return this.modelname; 
  } 
 
  public void setmodelname(string modelname) { 
    this.modelname = modelname; 
  } 
 
  @column(name = "content", length = 500) 
  public string getcontent() { 
    return this.content; 
  } 
 
  public void setcontent(string content) { 
    this.content = content; 
  } 
 
  @column(name = "inserttime", length = 20) 
  public string getinserttime() { 
    return this.inserttime; 
  } 
 
  public void setinserttime(string inserttime) { 
    this.inserttime = inserttime; 
  } 
 
  @column(name = "remark", length = 50) 
  public string getremark() { 
    return this.remark; 
  } 
 
  public void setremark(string remark) { 
    this.remark = remark; 
  } 
 
} 

三、将类注解为@idclass,并将该实体中所有属于主键的属性都注解为@id

/** 
 * syslogsdtoid代表主键类 
 */ 
package com.hibernate.dto; 
 
public class syslogsdtoid implements java.io.serializable { 
 
  private static final long serialversionuid = 1l; 
  private string id; 
  private string yhid; 
 
  public syslogsdtoid() { 
  } 
 
  public syslogsdtoid(string id, string yhid) { 
    this.id = id; 
    this.yhid = yhid; 
  } 
 
  public string getid() { 
    return this.id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getyhid() { 
    return this.yhid; 
  } 
 
  public void setyhid(string yhid) { 
    this.yhid = yhid; 
  } 
 
  public boolean equals(object other) { 
    if ((this == other)) 
      return true; 
    if ((other == null)) 
      return false; 
    if (!(other instanceof syslogsdtoid)) 
      return false; 
    syslogsdtoid castother = (syslogsdtoid) other; 
 
    return ((this.getid() == castother.getid()) || (this.getid() != null && castother.getid() != null && this.getid().equals(castother.getid()))) 
        && ((this.getyhid() == castother.getyhid()) || (this.getyhid() != null && castother.getyhid() != null && this.getyhid().equals( 
            castother.getyhid()))); 
  } 
 
  public int hashcode() { 
    int result = 17; 
 
    result = 37 * result + (getid() == null ? 0 : this.getid().hashcode()); 
    result = 37 * result + (getyhid() == null ? 0 : this.getyhid().hashcode()); 
    return result; 
  } 
 
} 

/** 
 * syslogsdto为表对象映射类,其中主键为主键类syslogsdtoid 
 */ 
package com.hibernate.dto; 
 
import javax.persistence.column; 
import javax.persistence.entity; 
import javax.persistence.id; 
import javax.persistence.idclass; 
import javax.persistence.table; 
 
@entity 
@table(name = "syslogs") 
@idclass(value=syslogsdtoid.class) 
public class syslogsdto implements java.io.serializable { 
  private static final long serialversionuid = 1l; 
  private string id; 
  private string yhid; 
  private string modelname; 
  private string content; 
  private string inserttime; 
  private string remark; 
 
  public syslogsdto() { 
  } 
 
  @id 
  public string getid() { 
    return id; 
  } 
 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  @id 
  public string getyhid() { 
    return yhid; 
  } 
 
 
  public void setyhid(string yhid) { 
    this.yhid = yhid; 
  } 
 
 
  @column(name = "modelname", length = 100) 
  public string getmodelname() { 
    return this.modelname; 
  } 
 
  public void setmodelname(string modelname) { 
    this.modelname = modelname; 
  } 
 
  @column(name = "content", length = 500) 
  public string getcontent() { 
    return this.content; 
  } 
 
  public void setcontent(string content) { 
    this.content = content; 
  } 
 
  @column(name = "inserttime", length = 20) 
  public string getinserttime() { 
    return this.inserttime; 
  } 
 
  public void setinserttime(string inserttime) { 
    this.inserttime = inserttime; 
  } 
 
  @column(name = "remark", length = 50) 
  public string getremark() { 
    return this.remark; 
  } 
 
  public void setremark(string remark) { 
    this.remark = remark; 
  } 
 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网