標籤:ransient bean 實體類 eid persist min() isa tool targe
- Hibernate 註解
- 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; }}
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 註解