jpa中复合主键并且都是外键,请问怎么写注解
发布网友
发布时间:2022-04-26 00:18
我来回答
共2个回答
懂视网
时间:2022-05-07 04:32
http://note.youdao.com/share/?id=9065b839f2d6b3f1b6310ad9bb92d3f6type=note @Entity@Table(name = RELEASE_INFO_ELEMENT)public class ReleaseInfoElement { @EmbeddedId private PK pk = new PK(); @Column(name = VALUE) private String value; public
http://note.youdao.com/share/?id=9065b839f2d6b3f1b6310ad9bb92d3f6&type=note
@Entity
@Table(name = "RELEASE_INFO_ELEMENT")
public class ReleaseInfoElement {
@EmbeddedId
private PK pk = new PK();
@Column(name = "VALUE")
private String value;
public Long getInfoId() {
return pk.infoId;
}
public void setInfoId(Long infoId) {
pk.infoId = infoId;
}
public Long getParamId() {
return pk.paramId;
}
@Override
public String toString() {
return "ReleaseInfoElement [infoId=" + pk.infoId + ", paramId=" + pk.paramId
+ ", value=" + value + "]";
}
public void setParamId(Long paramId) {
pk.paramId = paramId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Embeddable
public static class PK implements Serializable {
@Column(name = "INFO_ID")
private Long infoId;
@Column(name = "PARAM_ID")
private Long paramId;
public PK() {
super();
}
}
}
热心网友
时间:2022-05-07 01:40
在设计数据库表的时候,往往会设计出带有复合主键的表,即表的记录由多个字段联合标识,如:
表
Sql代码
CREATE TABLE TB_HOUR_DATA
(
STAT_DATE DATE NOT NULL,
PATH_ID NUMBER(20) NOT NULL,
VALUE VARCHAR2(512 BYTE),
TYPE NUMBER(1) NOT NULL
)
其中,复合主键为(STAT_DATE,PATH_ID,TYPE)
针对这种情况,hibernate(jpa) 的 annotation 映射声明如下:
1、复合主键类HourDataPK
Java代码
package net.kong.wolf.stat.db.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Embeddable
public class HourDataPK implements Serializable {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name = "path_id", nullable = false)
private Path path;
@Column(name = "stat_date")
@Temporal(TemporalType.DATE)
private Date statDate;
private int type;
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public Date getStatDate() {
return statDate;
}
public void setStatDate(Date statDate) {
this.statDate = statDate;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
2、实体类HourData:
Java代码
package net.kong.wolf.stat.db.entity;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import net.kong.wolf.stat.core.Text;
@Entity
@Table(name = "TB_HOUR_DATA")
public class HourData implements Serializable {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
@EmbeddedId
private HourDataPK pk;
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int[] getHours() {
return parseValue(value);
}
private int[] parseValue(String value) {
int[] result = new int[24];
for (int i = 0; i < 24; i++) {
result[i] = -1;
}
if (value == null) {
return result;
}
String[] hs = Text.splitCSV(value);
int len = Math.min(24, hs.length);
for (int i = 0; i < len; i++) {
result[i] = Text.parseInt(hs[i], -1);
}
return result;
}
public void setHours(int[] hours) {
int[] tHours = parseValue(value);
StringBuilder sb = new StringBuilder();
int len = Math.min(24, hours.length);
for (int i = 0; i < len; i++) {
sb.append(hours[i] < 0 ? tHours[i] : hours[i]).append(',');
}
sb.deleteCharAt(sb.length() - 1);
this.value = sb.toString();
}
public HourDataPK getPk() {
return pk;
}
public void setPk(HourDataPK pk) {
this.pk = pk;
}
}