您好,登錄后才能下訂單哦!
hibernate中怎么聲明復(fù)合主鍵,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1、復(fù)合主鍵類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、實(shí)體類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; } } |
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。