• hibernate 关系映射之 主键关联一对一


    单向双向在数据库中没有区别,但是在java程序中有区别。

     

    在程序中:双向的通过一个就可以找到另一个,单向的通过设置了对应关系的类可以找到它所对应的类,反过来就不行。

     

    注解方式单向主键关联:

    和外键一样,只不过注解由@JoinColumn变成@PrimaryKeyJoinColumn

     

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

    @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;
        }
        @OneToOne
        @PrimaryKeyJoinColumn

        public Wife getWife() {
            return wife;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setWife(Wife wife) {
            this.wife = wife;
        }
       
    }

     

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

    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.test.hibernate.StuIdCard">
            <id name="id">
                <generator class="foreign">
                   <param name="property">student</param><!--   根据外键生成该字段 -->
                </generator>
            </id>
           
            <property name="num"/>
            <one-to-one name="student" constrained="true"></one-to-one>
        </class>
       
    </hibernate-mapping>

     

     

    注解方式双向主键关联:

    给Wife类追加以下内容

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

    @Entity
    public class Wife {
        private int id;
        private String name;
        private Husband husband;
        @OneToOne
        @PrimaryKeyJoinColumn
        public Husband getHusband() {
            return husband;
        }
        public void setHusband(Husband husband) {
            this.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;
        }
       
    }

     

    xml方式主键双向关联:

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

     

     

    <?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>
            <one-to-one name="stuIdCard" property-ref="student"></one-to-one>
        </class>
       
    </hibernate-mapping>

  • 相关阅读:
    [POI2011]Lightning Conductor
    [NOI2009]诗人小G

    公告&留言板
    联合省选 2020 题解
    年末时的一些思维碎屑
    RE:ゼロから始める AFO 生活
    退役前的做题记录6.0
    2019年湖南省大学生计算机程序设计竞赛 (HNCPC2019) 简要题解
    300iq Contest 1 简要题解
  • 原文地址:https://www.cnblogs.com/flying607/p/3477592.html
Copyright © 2020-2023  润新知