@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个注解

  1. @Entity标志这个类是实体

  2. @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,postgresql

TABLE表示根据另外的表生成

3.@Column可以配置实体属性与数据库表字段的对应关系

name属性值为表字段名

unique 是否唯一

nullable 是否可以为null

precision 有效位数

scale  小数位数

length 数据长度

insertable 该字段是否可以插入到数据库表中

updateable 该字段是否可以更新

columnDefinition 设置对应的表字段的数据类型,不常用

4.@Temporal它只有一个value属性,所以这个value可以省略,它的值时枚举值TemporalType

TemporalType.DATE  日期 精确到年月日,例如"2015-11-04"

TemporalType.TIME  时间 精确到时分秒,例如"20:00:00"

TemporalType.TIMESTAMP 日期时间 精确到年月日时分秒,例如"2015-11-04 20:00:00"

@BLob

@Clob

5.@Version 版本标志

6.@Transient 表示这个属性时游离的,不会持久化到数据库中

6.@ManyToOne 先占好坑,下次再详细写

7.@JoinColumn

8.@OneToMany

9.@ManyToMany

10.@OneToOne


单独写出来:

  1. 联合主键配置,这里随便选了一种实现方法,说实话hibernate的联合主键配置确实麻烦,还不如mybatis.

    这种是myeclipse自动生成的

    第一个类

    @Embeddable

    public 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;

    }

    }

  2. Sequence主键生成策略,这个有点复杂

    首先先建好序列seq_student

    class类为

    @Entity

    @SequenceGenerator(name="seq_t_student" sequenceName='seq_student')

    @Table(name="student" schma=""

    public class Student{



    private long id;

  3. private void setId(long id){

  4.  this.id=id;

  5. }

  6. @Id

  7. @GeneratedValue(strategy=Generation.SEQUENCE,generator="seq_t_student")

  8. @Column(name="ID",length=40,nullable=false,unique=true)

  9. public long getId(){

  10.  return this.id

  11. }

  12. }