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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate一对一外键双向关联(Annotation配置)相关的知识,希望对你有一定的参考价值。

技术分享
如上图所示:一个学生有一个学生证号,一个学生证号对应一名学生。在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() + " "
                + stuIdCard.getNum() + " "
                + stuIdCard.getStudent().getGender() + " "
                + 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配置)的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate学习———— hibernate一对一关系映射详解

hibernate 之 关联映射的基于外键的双向一对一关联

java之hibernate之基于外键的双向一对一关联映射

hibernate关联映射

hibernate学习四 hibernate关联关系映射

hibernate基础08:关联映射之基于外键的双向一对一