OracleHibernate框架的注解的布

当hibernate中,通常配置对象关系映射关系产生半点栽,一栽是根据xml的方法,另一样种是基于annotation的笺注方式,熟话说,萝卜青菜,可享有好,每个人都起友好喜好的配备方式,我以摸索了即有限种方法下,发现采用annotation的法可以再简介,所以这里就是概括记录下通过annotation来安排各种映射关系,在hibernate4以后曾经用annotation的jar包集成进来了,如果运用hibernate3的本就待引入annotation的jar包。

安排持久化类常用的注释:

Oracle 1

流淌:GeneratedValue指定了标识符的变通策略。jpa提供了4栽标准用法。

01.AUTO:根据不同的数据库选择不同之策略

02.TABLE:使用表保存id值

03.INDENITY:使用数据库自动生成主键

04.SEQUENCE:使用序列创建主键(如Oracle)

 

布局关联关系常用之诠释:

Oracle 2

话不多说了,待大家来看看到底怎么用的才最要害

1.注解配置对象关联关系的   一对一(学生对学生证)

Oracle 3

 

Student.java

Oracle 4Oracle 5

package cn.zhang.entity;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

//学生实体类
@Entity
@Table(name="Student2161")
public class Student {
    @Id //主键
    @GeneratedValue //主键生成策略
    private Integer sid;

    private String name;
    //配置之间的一对一的关系
    //cascade={CascadeType.ALL}设置了级联
    //mappedBy="stu"设置维护关系的控制权交给StuCard类这一方,相当于Student.hbm.xml中配置的inverse="true"
    @OneToOne(mappedBy="stu",cascade={CascadeType.ALL})
    private StuCard sCard;
    public Integer getSid() {
        return sid;
    }
    public void setSid(Integer sid) {
        this.sid = sid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public StuCard getsCard() {
        return sCard;
    }
    public void setsCard(StuCard sCard) {
        this.sCard = sCard;
    }

}

View Code

StuCard.java

Oracle 6Oracle 7

package cn.zhang.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

//学生证实体类
@Entity
@Table(name="StuCard2161")
public class StuCard {
    @Id //主键
    @Column(length=18) //可以指定最大长度
    private String cid;//身份证

    @OneToOne  //配置一对一关联
    @JoinColumn(name="sid")//指定了维护关系的外键字段是Student的sid
    private Student stu;

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

}

View Code

最终只有需要以hibernate.cfg.xml文件之中将拖欠实体类加进去即可:

<!-- 注解配置 -->
<mapping class="cn.zhang.entity.Student"/>
<mapping class="cn.zhang.entity.StuCard"/>

测试:

Oracle 8Oracle 9

package cn.zhang.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


import cn.zhang.entity.StuCard;
import cn.zhang.entity.Student;
import cn.zhang.util.HibernateUtil;

public class MyTest {

    Session session;
    Transaction tx;

    //单元测试前走
    @Before
    public void Before(){
        session= HibernateUtil.currentSession();        
         tx= session.beginTransaction();        
    }

    //单元测试后走
    @After
    public void After(){
        tx.commit();        
        HibernateUtil.closeSession();    
    }

    @Test
    public void TestOne(){        

        Student student=new Student();
        student.setName("good");

        StuCard sCard=new StuCard();
        sCard.setCid("11111111111111");
        sCard.setStu(student);
        student.setsCard(sCard);

        session.save(student);

    }        
}

View Code

2.注解配置对象关系关系的   多对相同(部门与员工)

Oracle 10

Dept.java

Oracle 11Oracle 12

package cn.zhang.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

//部门实体类
@Entity
@Table(name="Dept2161")
public class Dept {
    @Id
    //主键生成策略
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_gen")
    @SequenceGenerator(name="seq_gen",sequenceName="SEQ_HouseNum",allocationSize=1,initialValue=1)
    private Integer deptid;

    private String deptname;
    @OneToMany(mappedBy="dept",cascade={CascadeType.ALL})
    @LazyCollection(LazyCollectionOption.FALSE) //设置立即加载,默认为延迟加载
    private Set<Emp> emps = new HashSet<Emp>();// 员工集合

    public Set<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }

    public Integer getDeptid() {
        return deptid;
    }

    public void setDeptid(Integer deptid) {
        this.deptid = deptid;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

}

View Code

Emp.java

Oracle 13Oracle 14

package cn.zhang.entity;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

//员工实体类
@Entity
@Table(name="Emp2161")
public class Emp {
    @Id
    //主键生成策略
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_gen")
    @SequenceGenerator(name="seq_gen",sequenceName="SEQ_HouseNum",allocationSize=1,initialValue=1)
    private Integer empno;

    private String empname;
    @ManyToOne(fetch=FetchType.LAZY) //fentch: 设置了延迟加载 ,默认为立即加载,不设置则会和dept表外连接查询
    @JoinColumn(name="deptid")
    private Dept dept;//所属部门

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

}

View Code

末只有需要在hibernate.cfg.xml文件之中将拖欠实体类加进去即可:

<!-- 注解配置 -->
<mapping class="cn.zhang.entity.Dept"/>
<mapping class="cn.zhang.entity.Emp"/>

测试:

Oracle 15Oracle 16

package cn.zhang.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.zhang.entity.Dept;
import cn.zhang.entity.Emp;

import cn.zhang.util.HibernateUtil;

public class MyTest {

    Session session;
    Transaction tx;

    //单元测试前走
    @Before
    public void Before(){
        session= HibernateUtil.currentSession();        
         tx= session.beginTransaction();        
    }

    //单元测试后走
    @After
    public void After(){
        tx.commit();        
        HibernateUtil.closeSession();    
    }

    @Test
    public void TestOne(){        
        Emp emp=new Emp();
        emp.setEmpname("好人一个");

        Dept dept=new Dept();
        dept.setDeptname("人才部");
        dept.getEmps().add(emp);
        emp.setDept(dept);
        session.save(dept);


    }
    @Test
    public void TestTwo(){        
        Emp emp = (Emp)session.load(Emp.class, 2);
        System.out.println(emp.getEmpname());

        System.out.println("------------");

        Dept dept = (Dept)session.load(Dept.class, 1);
        System.out.println(dept.getDeptname());

    }

}

View Code

3.诠释配置对象关系关系之   多对几近(项目Oracle及员工)

 Oracle 17

Emp.java

Oracle 18Oracle 19

package cn.zhang.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Emp2161")
public class Emp {
    @Id
    @GeneratedValue
    private Integer eid;

    private String name;

    @ManyToMany(cascade=CascadeType.ALL)//设置多对多和级联
    //利用第三张表实现多对多的关联
    @JoinTable(
    name = "EmpPro2161", //指定第三张表名
    joinColumns = @JoinColumn(name = "eid"), //Emp表在第三张表中的外键
    inverseJoinColumns = @JoinColumn(name = "pid")//Project表在第三张表中的外键
    )
    private Set<Project> projects = new HashSet<Project>();

    public Integer getEid() {
        return eid;
    }

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

    public String getName() {
        return name;
    }

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

    public Set<Project> getProjects() {
        return projects;
    }

    public void setProjects(Set<Project> projects) {
        this.projects = projects;
    }

}

View Code

Project.java

Oracle 20Oracle 21

package cn.zhang.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="Project2161")
public class Project {
    @Id
    @GeneratedValue
    private Integer pid;

    private String name;
    @ManyToMany(mappedBy="projects")//设置多对多并指定维护关系的控制权交给Emp类这一方
    private Set<Emp> emps=new HashSet<Emp>();

    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<Emp> getEmps() {
        return emps;
    }

    public void setEmps(Set<Emp> emps) {
        this.emps = emps;
    }

}

View Code

末尾只有需要以hibernate.cfg.xml文件之中将拖欠实体类加进去即可:

<!-- 注解配置 -->
<mapping class="cn.zhang.entity.Project"/>
<mapping class="cn.zhang.entity.Emp"/>

测试:

Oracle 22Oracle 23

package cn.zhang.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



import cn.zhang.entity.Emp;
import cn.zhang.entity.Project;
import cn.zhang.util.HibernateUtil;

public class MyTest {

    Session session;
    Transaction tx;

    //单元测试前走
    @Before
    public void Before(){
        session= HibernateUtil.currentSession();        
         tx= session.beginTransaction();        
    }

    //单元测试后走
    @After
    public void After(){
        tx.commit();        
        HibernateUtil.closeSession();    
    }

    @Test
    public void TestOne(){        
        Emp emp=new Emp();
        emp.setName("好人一个");

        Project project=new Project();

        project.setName("好项目");

        project.getEmps().add(emp);
        emp.getProjects().add(project);

        session.save(emp);        

    }
}

View Code

 

注意:在认清究竟是何人保护关系关系经常,可以由此翻看外键,哪个实体类定义了外键,哪个类即顶保护关系关系。

相关文章