Hibernate註解-實體類註解

來源:互聯網
上載者:User

Hibernate註解-實體類註解
@Entity@Table(name = "EMP", schema = "SCOTT")public class Emp implements java.io.Serializable { // Fields private Short empno;private Dept dept;private String ename;private String job;private Short mgr;private Date hiredate;private Double sal;private Double comm; // Constructors /** default constructor */public Emp() {} /** full constructor */public Emp(Dept dept, String ename, String job, Short mgr, Date hiredate,Double sal, Double comm) {this.dept = dept;this.ename = ename;this.job = job;this.mgr = mgr;this.hiredate = hiredate;this.sal = sal;this.comm = comm;} // Property accessors@Id@GeneratedValue@Column(name = "EMPNO", unique = true, nullable = false, precision = 4, scale = 0)public Short getEmpno() {return this.empno;} public void setEmpno(Short empno) {this.empno = empno;} @ManyToOne(fetch = FetchType.LAZY)@JoinColumn(name = "DEPTNO")public Dept getDept() {return this.dept;} public void setDept(Dept dept) {this.dept = dept;} @Column(name = "ENAME", length = 10)public String getEname() {return this.ename;} public void setEname(String ename) {this.ename = ename;} @Column(name = "JOB", length = 9)public String getJob() {return this.job;} public void setJob(String job) {this.job = job;} @Column(name = "MGR", precision = 4, scale = 0)public Short getMgr() {return this.mgr;} public void setMgr(Short mgr) {this.mgr = mgr;} @Temporal(TemporalType.DATE)@Column(name = "HIREDATE", length = 7)public Date getHiredate() {return this.hiredate;} public void setHiredate(Date hiredate) {this.hiredate = hiredate;} @Column(name = "SAL", precision = 7)public Double getSal() {return this.sal;} public void setSal(Double sal) {this.sal = sal;} @Column(name = "COMM", precision = 7)public Double getComm() {return this.comm;} public void setComm(Double comm) {this.comm = comm;} }類上方的2個註解@Entity標誌這個類是實體@Table中name屬性值為資料庫表名, schema屬性值就是資料庫使用者名稱,預設為"",可以不寫,一般來說,寫這2個屬性就夠了.其他不介紹.屬性註解最好寫在get方法上方:1.@id表示這個是OID屬性,對應著資料庫的主鍵,注意OID必須為long,int或者short.2.@GenerateValue 中有2個屬性,strategy表示主鍵建置原則,預設值為StrategyType.AUTO(另外3個值為:IDENTITY,SEQUENCE,TABLE)其中AUTO表示hibernate自動選擇IDENTITY表示根據底層資料庫產生,支援mysql,db2,sqlserver,sybase等資料庫SEQUENCE也由底層資料庫產生,支援oracle,db2,postgresqlTABLE表示根據另外的表產生3.@Column可以配置實體屬性與資料庫表欄位的對應關係name屬性值為表欄位名unique 是否唯一nullable 是否可以為nullprecision 有效位元scale  小數位元length 資料長度insertable 該欄位是否可以插入到資料庫表中updateable 該欄位是否可以更新columnDefinition 設定對應的表欄位的資料類型,不常用4.@Temporal它只有一個value屬性,所以這個value可以省略,它的值時枚舉值TemporalTypeTemporalType.DATE  日期 精確到年月日,例如"2015-11-04"TemporalType.TIME  時間 精確到時分秒,例如"20:00:00"TemporalType.TIMESTAMP 日期時間 精確到年月日時分秒,例如"2015-11-04 20:00:00"@BLob@Clob5.@Version 版本標誌6.@Transient 表示這個屬性時游離的,不會持久化到資料庫中6.@ManyToOne 先佔好坑,下次再詳細寫7.@JoinColumn8.@OneToMany9.@ManyToMany10.@OneToOne 單獨寫出來:聯合主鍵配置,這裡隨便選了一種實現方法,說實話hibernate的聯合主鍵配置確實麻煩,還不如mybatis.這種是myeclipse自動產生的第一個類@Embeddablepublic class BonusId implements java.io.Serializable {     private String ename;private String job;private BigDecimal sal;private BigDecimal comm;...}第二個類@Entity@Table(name = "BONUS", schema = "SCOTT")public class Bonus implements java.io.Serializable {  private BonusId id;@EmbeddedId@AttributeOverrides({@AttributeOverride(name = "ename", column = @Column(name = "ENAME", length = 10)),@AttributeOverride(name = "job", column = @Column(name = "JOB", length = 9)),@AttributeOverride(name = "sal", column = @Column(name = "SAL", precision = 22, scale = 0)),@AttributeOverride(name = "comm", column = @Column(name = "COMM", precision = 22, scale = 0)) })public BonusId getId() {return this.id;}public void setId(BonusId id) {this.id = id;}}Sequence主鍵建置原則,這個有點複雜首先先建好序列seq_studentclass類為@Entity@SequenceGenerator(name="seq_t_student" sequenceName='seq_student')@Table(name="student" schma=""public class Student{  private long id;private void setId(long id){ this.id=id;}@Id@GeneratedValue(strategy=Generation.SEQUENCE,generator="seq_t_student")@Column(name="ID",length=40,nullable=false,unique=true)public long getId(){ return this.id}}   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.