熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate(jpa)復合主鍵annotation聲明方法

2013-11-23 20:27:54  來源: Java開源技術 

  

  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;
}
}


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28530.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.