hibernate详解(七)基于外键映射的一对一关联关系

直接上代码:
sql语句:


/* 基于外键的映射 */
CREATE TABLE t_person_fk
 (
    id NUMBER(10) PRIMARY KEY,
    name VARCHAR2(20) NOT NULL,
    gender VARCHAR2(10),
    age NUMBER(3) CHECK(age BETWEEN 1 AND 100)
 );

CREATE TABLE t_passport_fk
 (
    id NUMBER(10) PRIMARY KEY,
    bh VARCHAR2(20) NOT NULL UNIQUE,
    person_id NUMBER(10) REFERENCES t_person_fk(id) UNIQUE
 );
 
 CREATE SEQUENCE t_person_fk_seq
 START WITH 1
 INCREMENT BY 1;
 
 CREATE SEQUENCE t_passport_fk_seq
 START WITH 1
 INCREMENT BY 1;

Person.java

package com.iotek.basic.association.one2one.pojo;

import java.io.Serializable;

public class Person implements Serializable{

    private static final long serialVersionUID = -5614535607156334069L;
    private Long id;
    private String name;
    private String gender;
    private Integer age;
    private Passport passport;
    
    public Person() {}

    public Person(Long id, String name, String gender, Integer age, Passport passport) {
        super();
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.passport = passport;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Passport getPassport() {
        return passport;
    }

    public void setPassport(Passport passport) {
        this.passport = passport;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", passport=" + passport
                + "]";
    }

}

Passport.java

package com.iotek.basic.association.one2one.pojo;

import java.io.Serializable;

public class Passport implements Serializable{
    
    private static final long serialVersionUID = -2402152980390377315L;
    private Long id;
    private String bh;
    private Person person;
    
    public Passport() {}

    public Passport(Long id, String bh, Person person) {
        super();
        this.id = id;
        this.bh = bh;
        this.person = person;
    }

    public Long getId() {
        return id;
    }

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

    public String getBh() {
        return bh;
    }

    public void setBh(String bh) {
        this.bh = bh;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    
    @Override
    public String toString() {
        return "Passport [id=" + id + ", bh=" + bh + "]";
    }
    
    
}

映射文件:
Person_FK.hbm.xml





    
        
            
                t_person_fk_seq
            
        
        
        
        
        
        
    

Passport_FK.hbm.xml





    
        
            
                t_passport_fk_seq
            
        
        
        
        
    

测试类:
One2OneTest.java

package com.iotek.basic.association.one2one.pojo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.iotek.basic.common.HibernateSessionFactory;

public class One2OneTest {

    public static void main(String[] args) {
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(18);
        person.setGender("male");
        
        Passport passport = new Passport();
        passport.setBh("G10562343");
        
        person.setPassport(passport);
        passport.setPerson(person);
        
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = null;
        
        try {
            transaction = session.beginTransaction();
            // 增
            // session.save(person);
            // 查
            Person person2 = session.get(Person.class, 21L);
            // 改
            // person2.setName("zhangsanfeng");
            // person2.setAge(22);
            // person2.getPassport().setBh("G12211221");
            // session.update(person2);
            // 删
            session.delete(person2);
            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }

}

这里要注意的是如何分别在表的层面以及类的层面配置一对一的关联关系。以及双向的关联关系。

你可能感兴趣的:(hibernate详解(七)基于外键映射的一对一关联关系)