首页 > 代码库 > 关于hibernate注解的简单应用

关于hibernate注解的简单应用

@Override

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

单元测试注解

@Test

   用途:用于测试

@Before

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

 

@After

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

 

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

 

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

@Table  :DB层表的名称

@Transient :不持久化某个属性

@Column:数据表中的字段名

  @GenerateValue :主键生成策略

-----------------关联-------------------------------------------------------------------------------------------------------

@OneToMany :

@ManyToMany

@OneToOne

@JoinColumn

@JoinTable

 

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

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

配置持久化类

技术分享

 Hibernate注解可以代理hbm文件。

下面我们来用行动看看效果:

员工实体类:

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="connection.url">jdbc:oracle:thin:@localhost:1521: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("88888888888");
        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.get(Dept.class,3);
        System.out.println(dept.getDname());
        System.out.println(dept.getEmps().iterator().next().getEname());

    }
}
我们来看一下结果:

技术分享

技术分享

技术分享

好了,今天就先写这些例子。如果有什么问题可以交流。

 




 



关于hibernate注解的简单应用