java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2018年05月28日
 
Hibernate(jpa)复合主键annotation声明方法

CREATE TABLE TB_HOUR_DATA

(

STAT_DATE DATE NOT NULL

PATH_ID NUMBER() NOT NULL

VALUE VARCHAR( BYTE)

TYPE NUMBER() NOT NULL

)

其中复合主键为(STAT_DATEPATH_IDTYPE)

针对这种情况hibernate(jpa) 的 annotation 映射声明如下

复合主键类HourDataPK

package netkongwolfstatdbentity;

import javaioSerializable;

import javautilDate;

import javaxpersistenceColumn;

import javaxpersistenceEmbeddable;

import javaxpersistenceJoinColumn;

import javaxpersistenceManyToOne;

import javaxpersistenceTemporal;

import javaxpersistenceTemporalType;

@Embeddable

public class HourDataPK implements Serializable {

/** *//**

*

*/

private static final long serialVersionUID = L;

@ManyToOne

@JoinColumn(name = path_id nullable = false)

private Path path;

@Column(name = stat_date)

@Temporal(TemporalTypeDATE)

private Date statDate;

private int type;

public Path getPath() {

return path;

}

public void setPath(Path path) {

thispath = path;

}

public Date getStatDate() {

return statDate;

}

public void setStatDate(Date statDate) {

thisstatDate = statDate;

}

public int getType() {

return type;

}

public void setType(int type) {

thistype = type;

}

}

实体类HourData

package netkongwolfstatdbentity;

import javaioSerializable;

import javaxpersistenceEmbeddedId;

import javaxpersistenceEntity;

import javaxpersistenceTable;

import nereText;

@Entity

@Table(name = TB_HOUR_DATA)

public class HourData implements Serializable {

/** *//**

*

*/

private static final long serialVersionUID = L;

@EmbeddedId

private HourDataPK pk;

private String value;

public String getValue() {

return value;

}

public void setValue(String value) {

thisvalue = value;

}

public int[] getHours() {

return parseValue(value);

}

private int[] parseValue(String value) {

int[] result = new int[];

for (int i = ; i < ; i++) {

result[i] = ;

}

if (value == null) {

return result;

}

String[] hs = TextsplitCSV(value);

int len = Mathmin( hslength);

for (int i = ; i < len; i++) {

result[i] = TextparseInt(hs[i] );

}

return result;

}

public void setHours(int[] hours) {

int[] tHours = parseValue(value);

StringBuilder sb = new StringBuilder();

int len = Mathmin( hourslength);

for (int i = ; i < len; i++) {

sbappend(hours[i] < ? tHours[i] : hours[i])append();

}

sbdeleteCharAt(sblength() );

thisvalue = sbtoString();

}

public HourDataPK getPk() {

return pk;

}

public void setPk(HourDataPK pk) {

thispk = pk;

}

}

               

上一篇:浅析Struts 体系结构与工作原理(图)

下一篇:J2EE综合——Struts常见错误的全面汇总