首页 > 代码库 > (十) 使用Hibernate 注解

(十) 使用Hibernate 注解

  • 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 注解