hibernate注解的简单应用

注解代替了我们用的*.hbm.xml文件。简少了我们的代码量;应用简单。

@Override

用途:重写父类的同名方法

单元测试注解

@Test

用途:用于测试

@Before

用途:单测方法走之前执行

@After

用途:单测方法走之后执行

注解的目标:替换小配置。

替换hbm文件

@Entity  :标识实体类(被持久化)

@Table  :DB层表的名称

@Transient :不持久化某个属性

@Column:数据表中的字段名

@GenerateValue :主键生成策略

—————–关联——————————————————————————————————-

@OneToMany :

@ManyToMany

@OneToOne

@JoinColumn

@JoinTable

Hibernate提供了Hibernate Annotations扩展包,使用注解完成映射。

在Hibernate3.3之前,需单独下载注解开发包。

配置持久化类

Hibernate 注解可以替代hbm文件

看完了注解的意思我们用注解配置一对一;(我用的是idea;MyEclipse应该是一样的。)

先看实体类

员工实体类

package cn.curry.entity2;import javax.persistence.*;/** * Created by Curry on 2017/2/16. */@Entity@Table(name = "Emp2")public class Emp {    private Integer eid;    @Column    private String ename;    private Idcard idcard;    @OneToOne    @JoinColumn(name = "iid")    public Idcard getIdcard() {        return idcard;    }    public void setIdcard(Idcard idcard) {        this.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;    }}

身份证实体类

package cn.curry.entity2;import javax.persistence.*;/** * Created by Curry on 2017/2/16. */@Entity@Table(name = "Idcard2")public class Idcard {    @Id    @GeneratedValue    private Integer iid;    private String inum;    @OneToOne(mappedBy = "idcard",cascade = CascadeType.ALL)    private Emp 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 Emp getEmp() {        return emp;    }    public void setEmp(Emp emp) {        this.emp = emp;    }}

然后我们再看hibernate.cfg.xml文件

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>        <property name=:orcl</property>        <property name="connection.username">curry</property>        <property name="connection.password">curry</property>        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>        <property name="show_sql">true</property>        <property name="hbm2ddl.auto">update</property>        <!--和当前线程绑定 -->        <property name="current_session_context_class">thread</property>        <!--<mapping resource="cn/curry/entity/Grade.hbm.xml"/>        <mapping resource="cn/curry/entity/Student.hbm.xml"/>-->       <!-- 一对一-->        <mapping class="cn.curry.entity2.Emp"/>        <mapping class="cn.curry.entity2.Idcard"/>       <!-- 一对多双向关联-->        <mapping class="cn.curry.entity3.Emp"/>        <mapping class="cn.curry.entity3.Dept"/>        <!--多对多双向关联-->        <mapping class="cn.curry.entity4.Game"/>        <mapping class="cn.curry.entity4.Palyer"/>    </session-factory></hibernate-configuration>

最后测试一下

package cn.curry.test;import cn.curry.entity2.Emp;import cn.curry.entity2.Idcard;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.After;import org.junit.Before;import org.junit.Test;/** * Created by Curry on 2017/2/16. */public class MyTest2 {    Session session=null;    Transaction tx;    @Before    public void before(){        session = HibernateUtil.currentSession();        tx=session.beginTransaction();    }    @After    public void after(){        tx.commit();        HibernateUtil.closeSession();    }    @Test    public void testOneToOne(){        Emp emp=new Emp();        emp.setEname("熊");        Idcard idcard=new Idcard();        idcard.setInum("232325199009092020");        idcard.setEmp(emp);        emp.setIdcard(idcard);        session.save(idcard);    }}

  然后我们看运行结果

接下来我们再看一对多双向关联的配置

我们重新写一个例子,这次我们用员工和部门的列子来说;

首先还是先看实体类,注意看配置,看注解的书写,看每个属性的配置。

这个部门的实体类

package cn.curry.entity3;import javax.persistence.*;import java.util.HashSet;import java.util.Set;/** * Created by Curry on 2017/2/16. */@Entity@Table(name="Dept02")public class Dept {    @Id    @GeneratedValue    private Integer did;    @Column    private String dname;    @OneToMany(mappedBy = "dept",cascade = CascadeType.ALL,fetch = FetchType.LAZY)    private Set<Emp> emps=new HashSet<Emp>();    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<Emp> getEmps() {        return emps;    }    public void setEmps(Set<Emp> emps) {        this.emps = emps;    }}

这个是员工的实体类

package cn.curry.entity3;import javax.persistence.*;/** * Created by Curry on 2017/2/16. */@Entity@Table(name = "Emp02")public class Emp {    @Id    @GeneratedValue    private Integer eid;    @Column    private String ename;    @ManyToOne    @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;    }}

因为我的hibernate.cfg.xml文件已经写好了,可以回看下。

接下来我们看测试类。

package cn.curry.test;import cn.curry.entity3.Dept;import cn.curry.entity3.Emp;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.After;import org.junit.Before;import org.junit.Test;/** * Created by Curry on 2017/2/16. */public class MyTest3 {    Session session=null;    Transaction tx;    @Before    public void before(){        session = HibernateUtil.currentSession();        tx=session.beginTransaction();    }    @After    public void after(){        tx.commit();        HibernateUtil.closeSession();    }    @Test    public void testOneToOne(){        Dept dept=new Dept();        dept.setDname("财务部");        Emp emp=new Emp();        emp.setEname("熊");        dept.getEmps().add(emp);        emp.setDept(dept);        session.save(dept);    }    @Test    public void select(){        Dept dept =session.);        System.out.println(dept.getDname());        System.out.println(dept.getEmps().iterator().next().getEname());    }}

然后我们看运行结果

就是这样接下来我们看最后一个例子多对多双向关联。最后一个例子我们拿游戏和玩家来进行举例。

还是先看下实体类。

先看游戏的实体类

package cn.curry.entity4;import javax.persistence.*;import javax.swing.text.PlainDocument;import java.util.HashSet;import java.util.Set;/** * Created by Curry on 2017/2/16. */@Entity@Table(name = "Game")public class Game {    @Id    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "sql_Game")    @SequenceGenerator(name=,initialValue = )    private Integer gid;    @Column    private String gname;    @ManyToMany(cascade = CascadeType.ALL)    @JoinTable(name = "PG",joinColumns = {@JoinColumn(name="gid")},inverseJoinColumns = {@JoinColumn(name="pid")})    private Set<Palyer> palyerSet=new HashSet<Palyer>();    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<Palyer> getPalyerSet() {        return palyerSet;    }    public void setPalyerSet(Set<Palyer> palyerSet) {        this.palyerSet = palyerSet;    }}

再看玩家的实体类

package cn.curry.entity4;import javax.persistence.*;import java.util.HashSet;import java.util.Set;/** * Created by Curry on 2017/2/16. */@Entity@Table(name = "Player")public class Palyer {    @Id    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "sql_Player")    @SequenceGenerator(name=,initialValue = )    private Integer pid;    @Column    private String pname;    @ManyToMany(cascade = CascadeType.ALL,mappedBy = "palyerSet")    private Set<Game> gameSet=new HashSet<Game>();    public Integer getPid() {        return pid;    }    public void setPid(Integer pid) {        this.pid = pid;    }    public String getPname() {        return pname;    }    public void setPname(String pname) {        this.pname = pname;    }    public Set<Game> getGameSet() {        return gameSet;    }    public void setGameSet(Set<Game> gameSet) {        this.gameSet = gameSet;    }}

最后看一下测试吧

package cn.curry.test;import cn.curry.entity4.Game;import cn.curry.entity4.Palyer;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.After;import org.junit.Before;import org.junit.Test;/** * Created by Curry on 2017/2/16. */public class MyTets4 {    Session session=null;    Transaction tx;    @Before    public void before(){        session = HibernateUtil.currentSession();        tx=session.beginTransaction();    }    @After    public void after(){        tx.commit();        HibernateUtil.closeSession();    }    @Test    public void testManyToMany(){        Game game1=new Game();        game1.setGname("NBA2K17");        Game game2=new Game();        game2.setGname("DNF");        Palyer palyer1=new Palyer();        palyer1.setPname("熊大");        Palyer palyer2=new Palyer();        palyer2.setPname("熊二");        game1.getPalyerSet().add(palyer1);        game1.getPalyerSet().add(palyer2);        game2.getPalyerSet().add(palyer1);        game2.getPalyerSet().add(palyer2);        palyer1.getGameSet().add(game1);        palyer1.getGameSet().add(game2);        palyer2.getGameSet().add(game1);        palyer2.getGameSet().add(game2);        session.save(game1);        session.save(game2);    }}

我们看运行结果

到这里所有例子就写完了。如果有不明白我写的东西,或者觉得那里有问题可以留言交流。

还有idea16的使用也可留言交流下。