Hibernate注解

Hibernate(注解)

首先注解有哪些常用的注解

@Entity:将一个类声明为一个持久化类。

@Id:声明了持久化累的标识属性

@GenerateValue:定义标识属性

@ Table为持久化类映射指定表

@UniqueConstraint 定义表的唯一约束

@Lob 表示属性将被持久化为Blob或者Clob类型

@Column 将属性映射到列

@Transient 忽略这些字和属性,不用持久化数据库

OneToOne(Emps)

@Entity //将一个类声明为一个持久化类。
@Table(name = "Emps2")
public class Emps {
    private Integer eid;
    @Column
    private String ename;
    private Idcard idcard;
   @Id
   @GeneratedValue
    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }
    @OneToOne
    @JoinColumn(name = "iid")//维护关联关系
    public Idcard getIdcard() {
        return idcard;
    }

    public void setIdcard(Idcard idcard) {
        this.idcard = idcard;
    }
}

OneToOne(Idcard)

@Entity
@Table(name = "Idcard2")
public class Idcard {
    @Id
    @GeneratedValue
    private Integer iid;

    private String inum;
    @OneToOne(mappedBy = "idcard",cascade = CascadeType.ALL)//通过cascad设置级联,通过mappedBy设置维护关系的关系的控制权,相当于inverse="true"
    private Emps emp;
    public Integer getIid() {
        return iid;
    }
    public void setIid(Integer iid) {
        this.iid = iid;
    }

    public String getInum() {
        return inum;
    }

    public void setInum(String inum) {
        this.inum = inum;
    }

    public Emps getEmp() {
        return emp;
    }

    public void setEmp(Emps emp) {
        this.emp = emp;
    }
    

cascade属性指定级联操作:

01:CascadeType.Remove:级联删除。

02:CascadeType.Persist:persist()方法级联。

03:CscadeType.Merge:级联更新。

04:CscadeType.Refresh:级联刷新;

05:CscadeType.All:包含所有级联。

然后去大配置:

 

 

OneToMany

@Entity
@Table(name = "Emp02")
public class Emp02{
    @Id
    @GeneratedValue
    private Integer eid;
    @Column
    private String ename;
    @OneToMany
    @JoinColumn(name = "did")
    private Dept dept;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }
}
@Entity
@Table(name = "Dept02")
public class Dept {
    @Id
    @GeneratedValue
    private  Integer did;
    @Column
    private String dname;
    @OneToMany(mappedBy = "dept",cascade = CascadeType.ALL)
    private Set<Emp02> empss=new HashSet<Emp02>();

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public Set<Emp02> getEmpss() {
        return empss;
    }

    public void setEmpss(Set<Emp02> empss) {
        this.empss = empss;
    }
}
  @Test  //测试类
    public void OneToMany(){
        Dept dept=new Dept();
        dept.setDname("财务部");
        Emp02 emp02=new Emp02();
        emp02.setEname("wwy");
        dept.getEmpss().add(emp02);
        emp02.setDept(dept);
        session.save(dept);
    }

ManyToMany

@Entity
@Table(name = "Phome")
public class Phome {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator ="sql_Play")
    @SequenceGenerator(name ="sql_Play",sequenceName = "sql_Play",allocationSize = 1,initialValue = 1)
    private Integer pid;
    @Column
    private String name;

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Game> getGames() {
        return games;
    }

    public void setGames(Set<Game> games) {
        this.games = games;
    }

    @ManyToMany(cascade = CascadeType.ALL,mappedBy = "Phome")
    private Set<Game> games=new HashSet<Game>();
}
@Entity
@Table(name = "Game")
public class Game {
    public Integer getGid() {
        return gid;
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public Set<Phome> getPhomes() {
        return phomes;
    }

    public void setPhomes(Set<Phome> phomes) {
        this.phomes = phomes;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "sql_Game")
    @SequenceGenerator(name = "sql_Game",sequenceName = "sql_Game",allocationSize = 1,initialValue = 1)
    private  Integer gid;
    @Column
    private String gname;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "PG",joinColumns ={@JoinColumn(name = "gid")},inverseJoinColumns = {@JoinColumn(name = "pid")})
    private Set<Phome> phomes=new HashSet<Phome>();

}

注意:

使用@GeneratedValue设置主键生成策略,strategy=GenerationType.SEQUENCE描述了,主键生成策略为Sequence,generator="sql_emp"指定了生成器为seq_emp

使用@SequenceGenerator设置了序列生成器,name="sql_emp"定义了序列生成器的名称为seq_emp;seqenceName="seq_emp_id指定了序列Sequence的名称为seq_emp_id数据库中创建表示预分配多少个主键值,如设置为1,表示不预分配主键值,默认为50.

 

posted @ 2017-02-16 14:53  时间&amp;煮雨  阅读(512)  评论(0编辑  收藏  举报