美文网首页
hibernate详解(七)基于外键映射的一对一关联关系

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

作者: 秀逼 | 来源:发表于2017-11-05 10:57 被阅读0次

    直接上代码:
    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

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.iotek.basic.association.one2one.pojo">
        <class name="Person" table="T_PERSON_FK">
            <id name="id" column="ID" type="long">
                <generator class="sequence">
                    <param name="sequence">t_person_fk_seq</param>
                </generator>
            </id>
            
            <property name="name" type="string" column="NAME"/>
            <property name="age" type="integer" column="AGE"/>
            <property name="gender" type="string" column="GENDER"/>
            <one-to-one name="passport" class="Passport" property-ref="person" cascade="all"/>
        </class>
    </hibernate-mapping>
    

    Passport_FK.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.iotek.basic.association.one2one.pojo">
        <class name="Passport" table="T_PASSPORT_FK">
            <id name="id" column="ID" type="long">
                <generator class="sequence">
                    <param name="sequence">t_passport_fk_seq</param>
                </generator>
            </id>
            
            <property name="bh" type="string" column="BH"/>
            <many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>
        </class>
    </hibernate-mapping>
    

    测试类:
    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详解(七)基于外键映射的一对一关联关系

          本文链接:https://www.haomeiwen.com/subject/ykiqmxtx.html