Hibernate 再接触 关系映射 一对一单向外键关联

对象之间的关系

 

数据库之间的关系只有外键

 

注意说关系的时候一定要反面也要说通

CRUD

数据库之间设计

主键关联

单向的外键关联

中间表

一对一单向外键关联

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 String getName() {
        return name;
    }
      public void setId(int id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setWife(Wife wife) {
        this.wife = wife;
    }
    
}

Wife.java

package com.bjsxt.hibernate;

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

@Entity
public class Wife {
    private int id;
    private String name;
    
    @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;
    }
    
}

运行

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

可以改掉wife_id的名字

XML中

Student.java

package com.bjsxt.hibernate;

public class Student {
    
    private int id;
    private String name;
    
    private int age;
    private String sex;
    private boolean good;
    public boolean isGood() {
        return good;
    }
    public void setGood(boolean good) {
        this.good = good;
    }
    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;
    }
    
    
    
}

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;
    }
    
}

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" 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>
    </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>

配置文件

    <mapping class="com.bjsxt.hibernate.Husband"/>
        <mapping class="com.bjsxt.hibernate.Wife"/>

运行测试

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

 

posted @ 2018-09-06 13:36  橘柑之味  阅读(98)  评论(0编辑  收藏  举报