码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate一对一外键双向关联(Annotation配置)

时间:2016-01-23 07:57:09      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
如上图所示:一个学生有一个学生证号,一个学生证号对应一名学生。在Hibernate中怎么用Annotation来实现呢?
学生类,主键是id;学生证的主键也是Id;
Student.java

package edu.xaut.hibernate;

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

@Entity
public class Student {
    private int id;
    private String name;
    private String gender;
    private int age;
    private StuIdCard stuIdCard;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    @Column(name = "name", length = 20)
    public String getName() {
        return name;
    }   

    @Column(name = "gender", length = 6)
    public String getGender() {
        return gender;
    }

    public int getAge() {
        return age;
    }

    @OneToOne
    @JoinColumn(name = "StuIdCard")
    public StuIdCard getStuIdCard() {
        return stuIdCard;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
   
    public void setGender(String gender) {
        this.gender = gender;
    }
   
    public void setAge(int age) {
        this.age = age;
    }

    public void setStuIdCard(StuIdCard stuIdCard) {
        this.stuIdCard = stuIdCard;
    }
}

package edu.xaut.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class StuIdCard {
    private int id;
    private String num;
    private Student student;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(length = 20)
    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    @OneToOne(mappedBy="stuIdCard")
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

导出的SQL语句如下:
create table StuIdCard (
        id integer not null auto_increment,
        num varchar(20),
        primary key (id)
    )

    create table Student (
        id integer not null auto_increment,
        age integer not null,
        gender varchar(6),
        name varchar(20),
        StuIdCard integer,
        primary key (id)
    )

    alter table Student
        add index FKF3371A1BFDFA0D4 (StuIdCard),
        add constraint FKF3371A1BFDFA0D4
        foreign key (StuIdCard)
        references StuIdCard (id)

测试代码如下:
package edu.xaut.hibernate;

import org.hibernate.Session;
import org.hibernate.cfg.*;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import edu.xaut.wuqiang.hibernate.util.HibernateUtil;

public class HibernateORMappingTest {

    @Test
    public void testStudentSave() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        StuIdCard stuIdCard = new StuIdCard();
        stuIdCard.setNum("1008120672");
        session.save(stuIdCard);
       
        Student stu = new Student();
        stu.setName("Lily");
        stu.setGender("Female");
        stu.setAge(22);
        stu.setStuIdCard(stuIdCard);
        session.save(stu);       

        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
    }

    @Test
    public void testQueryStuInfo() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        StuIdCard stuIdCard = (StuIdCard) session.get(StuIdCard.class, 1);
        System.out.println(stuIdCard.getNum());
        System.out.println(stuIdCard.getStudent().getName() + "\t"
                + stuIdCard.getNum() + "\t"
                + stuIdCard.getStudent().getGender() + "\t"
                + stuIdCard.getStudent().getAge());
        session.getTransaction().commit();
        HibernateUtil.getSessionFactory().close();
    }

    @Test
    public void testSchemaExport() {
        new SchemaExport(new AnnotationConfiguration().configure()).create(
                true, true);
    }   
}

运行结果如下:
    select
        stuidcard0_.id as id0_1_,
        stuidcard0_.num as num0_1_,
        student1_.id as id1_0_,
        student1_.age as age1_0_,
        student1_.gender as gender1_0_,
        student1_.name as name1_0_,
        student1_.StuIdCard as StuIdCard1_0_
    from
        StuIdCard stuidcard0_
    left outer join
        Student student1_
            on stuidcard0_.id=student1_.StuIdCard
    where
        stuidcard0_.id=?
1008120672
Lily    1008120672    Female    22

http://blog.sina.com.cn/s/blog_4979ec3e010174nx.html

Hibernate一对一外键双向关联(Annotation配置)

标签:

原文地址:http://www.cnblogs.com/startery/p/5152763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!