Hibernate教程05_关系映射之一对一双向外键关联

本教程每节课都附带源码,强烈大家建议配合源码学习。
本节源码:http://download.csdn.net/detail/e421083458/5253707


Annotation: @0ne20ne(mappedBy=”…”)

    规律:凡是双向关联,必设mappedBy
        在Wife类中 写Husband对象属性 并添加注解@OneToOne(mappedBy="wife") mappedBy作用
    是指定这个一对一关联是被Husband类的 wife属性(准确说是getWife方法)做的映射
    @OneToOne(mappedBy="wife")
        public Husband getHusband() {
        return husband;
        }
        在类中写Wife对象属性
        @OneToOne
        @JoinColumn(name="wifeid") //指定生成的数据库字段名
        public Wife getWife() {
            return wife;
        }
    此注释将由Husband表中生成wifeid字段作为fk外键,wife表中不生成额外的Husbandid字段
    
    
xml: many-to-one unique <one-to-one property-ref
    在Student类中写StuIdCard属性, StuIdCard类中写Student属性
    StuIdCard.hbm.xml文件中加
    <many-to-one name="student" column="studentId" unique="true"></many-to-one>
    Student.hbm.xml文件中加
  <one-to-one name="stuIdCard" property-ref="student"></one-to-one>
  其中, property-ref 相当于mappedBy
  此方式生成的StuIdCard表中包含studentid字段作为fk外键, Student表中不生成额外的字段
  特别说明: 一对一单向外键关联与一对一双向外键关联在数据库的表的格式是一样的,区别在于
    
注意:java程序中. 双向外键关联可通过Hibernate在两个类间互相调用彼此,而单向外键关联只能单方向调用.

下面展示实例:
 
Husband.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Husband {
    private int id;
    private String name;
    private Wife wife;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToOne
    @JoinColumn(name="wifeId")
    public Wife getWife() {
        return wife;
    }
    public void setWife(Wife wife) {
        this.wife = wife;
    }
}



Student.java

package com.bjsxt.hibernate;

public class Student {
    private int id;
    private String name;
    
    private int age;
    private String sex;
    private boolean good;
    private StuIdCard stuIdCard;
    public StuIdCard getStuIdCard() {
        return stuIdCard;
    }
    public void setStuIdCard(StuIdCard stuIdCard) {
        this.stuIdCard = stuIdCard;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public boolean isGood() {
        return good;
    }
    public void setGood(boolean good) {
        this.good = good;
    }
    
}


StuIdCard.java

package com.bjsxt.hibernate;

public class StuIdCard {
    private int id;
    private String num;
    private Student student;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNum() {
        return num;
    }
    public void setNum(String num) {
        this.num = num;
    }
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
}


Wife.java

package com.bjsxt.hibernate;

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

@Entity
public class Wife {
    private int id;
    private String name;
    private Husband husband;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @OneToOne(mappedBy="wife")
    public Husband getHusband() {
        return husband;
    }
    public void setHusband(Husband husband) {
        this.husband = husband;
    }
}



Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.bjsxt.hibernate.Student" table="C_student" dynamic-update="true">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="name"></property>
        <property name="age" />
        <property name="sex" />
        <property name="good" type="yes_no"></property>
        <one-to-one name="stuIdCard" property-ref="student"></one-to-one>
    </class>
    
</hibernate-mapping>



StuIdCard.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.bjsxt.hibernate.StuIdCard">
        <id name="id">
            <generator class="native"></generator>
        </id>
        
        <property name="num"/>
        <many-to-one name="student" column="studentId" unique="true"></many-to-one>
    </class>
    
</hibernate-mapping>



测试脚本:

package com.bjsxt.hiberante;


import java.util.Date;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.Session;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.bjsxt.hibernate.Husband;
import com.bjsxt.hibernate.StuIdCard;
import com.bjsxt.hibernate.Student;
import com.bjsxt.hibernate.Wife;


public class HibernateQLTest {
    private static SessionFactory sf;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("BeforeClass");
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("AfterClass");
        sf.close();
    }
    //
    @Test
    public void testSchemaExport(){
        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);        
    }
    @Test
    public void testOneTOOne_Wife(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Wife wife = new Wife();
        wife.setName("wife");
        Husband husband = new Husband();
        husband.setName("husband");
        husband.setWife(wife);
        session.save(wife);                            //先保存妻子
        session.save(husband);                        //再保存丈夫
        session.getTransaction().commit();
    }
    @Test
    public void testOneToOne_Stu(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Student stu = new Student();
        stu.setName("student");
        stu.setAge(20);
        stu.setGood(false);
        stu.setSex("man");
        StuIdCard idcard = new StuIdCard();
        idcard.setStudent(stu);
        idcard.setNum("什马卡?");
        session.save(stu);
        session.save(idcard);
        session.getTransaction().commit();
        
    }
    public static void main(String args[]){
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
        
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Student stu = new Student();
        stu.setName("student");
        stu.setAge(20);
        stu.setGood(false);
        stu.setSex("man");
        StuIdCard idcard = new StuIdCard();
        idcard.setStudent(stu);
        idcard.setNum("什马卡?");
        session.save(stu);
        session.save(idcard);
        session.getTransaction().commit();
        
        sf.close();        
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

e421083458

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值