關於hibernate註解的簡單應用

來源:互聯網
上載者:User

標籤:ima   xml檔案   sha   ring   return   ransient   set   plain   get   

@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註解的簡單應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.