(十) 使用Hibernate 註解

來源:互聯網
上載者:User

標籤:ransient   bean   實體類   eid   persist   min()   isa   tool   targe   

  • Hibernate裡有兩種註解 : 
  1. Hibernate 註解
  2. JPA註解
  •   主鍵產生機制 : http://www.cnblogs.com/ph123/p/5692194.html  

案例一:   用註解對非複合主鍵的表組建組態檔案

package bean;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity   // 表明這個類是一個實體類,所謂實體類就是該類對應資料庫中的表@Table(name = "user")   // 該實體類對應著資料庫中的user表public class UserBean {    @Id    @GeneratedValue(generator="a")    @GenericGenerator(name="a",strategy="assigned")   //設定主鍵增長機制,assigned為程式中定義,這也是預設。    private Integer userid;    private String username;    private String password;    private String sex;    @Column(name = "is_admin")      // 表示idAdmin這個屬性對應著資料庫中的is_admin這個欄位    private String isAdmin;    public UserBean() {    }    public UserBean(Integer userid, String username, String password,            String sex, String isAdmin) {        super();        this.userid = userid;        this.username = username;        this.password = password;        this.sex = sex;        this.isAdmin = isAdmin;    }    public Integer getUserid() {        return userid;    }    public void setUserid(Integer userid) {        this.userid = userid;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getIsAdmin() {        return isAdmin;    }    public void setIsAdmin(String isAdmin) {        this.isAdmin = isAdmin;    }}

 

  • 更新總設定檔
    <mapping class="bean.UserBean"/>

 

 

案例二:  用註解對含有複合主鍵的表組建組態檔案
package bean;import javax.persistence.EmbeddedId;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.Table;/** * Score entity. @author MyEclipse Persistence Tools */@Entity@Table(name="score")public class Score implements java.io.Serializable {    // Fields    @Id    @EmbeddedId  // 表示主鍵是一個複合主鍵    private ScoreId id;    private Integer score;    // Constructors    /** default constructor */    public Score() {    }    /** minimal constructor */    public Score(ScoreId id) {        this.id = id;    }    /** full constructor */    public Score(ScoreId id, Integer score) {        this.id = id;        this.score = score;    }    // Property accessors    public ScoreId getId() {        return this.id;    }    public void setId(ScoreId id) {        this.id = id;    }    public Integer getScore() {        return this.score;    }    public void setScore(Integer score) {        this.score = score;    }}
  • ScoreId.java
package bean;import javax.persistence.Embeddable;/** * ScoreId entity. @author MyEclipse Persistence Tools */@Embeddable   //表示該類可被其他類嵌入public class ScoreId implements java.io.Serializable {    // Fields    private Integer userid;    private String subject;    // Constructors    /** default constructor */    public ScoreId() {    }    /** full constructor */    public ScoreId(Integer userid, String subject) {        this.userid = userid;        this.subject = subject;    }    // Property accessors    public Integer getUserid() {        return this.userid;    }    public void setUserid(Integer userid) {        this.userid = userid;    }    public String getSubject() {        return this.subject;    }    public void setSubject(String subject) {        this.subject = subject;    }    public boolean equals(Object other) {        if ((this == other))            return true;        if ((other == null))            return false;        if (!(other instanceof ScoreId))            return false;        ScoreId castOther = (ScoreId) other;        return ((this.getUserid() == castOther.getUserid()) || (this                .getUserid() != null && castOther.getUserid() != null && this                .getUserid().equals(castOther.getUserid())))                && ((this.getSubject() == castOther.getSubject()) || (this                        .getSubject() != null && castOther.getSubject() != null && this                        .getSubject().equals(castOther.getSubject())));    }    public int hashCode() {        int result = 17;        result = 37 * result                + (getUserid() == null ? 0 : this.getUserid().hashCode());        result = 37 * result                + (getSubject() == null ? 0 : this.getSubject().hashCode());        return result;    }}

 

  • 更新總設定檔
<mapping class="bean.Score"/>

 

 

 

使用註解方式配置bean的時候,Hibernate預設把bean中的所有成員屬性都與資料庫中的欄位相關聯,如果有些成員屬性並不是資料庫中的欄位的時候,此時就會報錯,

則這時在這些屬性前添加  @Transient 註解, 表明這個屬性不是資料庫的欄位。

 

(十) 使用Hibernate 註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.