hibernate(jpa)复合主键annotation声明方法

开发 后端
hibernate(jpa)复合主键annotation声明方法。
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

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:
    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;
}
}
您正在阅读:hibernate(jpa)复合主键annotation声明方法

【编辑推荐】

  1. 让Spring帮助你在MVC层解决JPA的缓迟加载问题
  2. JPA重整ORM山河
  3. 利用EJB 3.0的JPA设计企业应用程序
责任编辑:张攀 来源: 豆豆网
相关推荐

2012-02-03 10:07:04

HibernateJava

2009-09-22 11:30:57

2009-09-22 17:32:38

Hibernate A

2009-09-23 09:16:25

Hibernate复合

2010-07-12 17:12:37

JPA 2.0缓存Hibernate缓存Java EE

2010-07-13 16:20:18

JPA 2.0缓存Hibernate缓存Java EE

2009-09-25 17:26:55

使用Hibernate

2021-10-06 23:31:45

HibernateJPASpring Data

2009-09-23 14:23:51

Hibernate主键

2009-09-24 10:50:31

Hibernate主键

2009-06-29 08:56:49

Hibernate主键生成主键

2009-09-25 13:33:43

Hibernate主键

2009-09-22 09:31:15

Hibernate主键

2009-09-24 13:49:31

Hibernate自增

2009-06-19 18:36:15

JPAHibernate

2011-07-25 18:11:47

SQL Server数复合主键

2009-06-29 08:50:20

Hibernate主键

2009-06-25 17:24:06

Hibernate主键

2009-09-28 10:52:33

Hibernate主键主键生成

2012-05-30 15:03:43

ibmdw
点赞
收藏

51CTO技术栈公众号