One-to-one Relevance in Hibernate Learning Notes 6

Keywords: Hibernate xml Database Attribute

One-to-one Relevance in Hibernate Learning Notes 6

Starting from today, we will begin to learn association, which can be divided into the following categories:

1. One-to-one Association

2. One-to-many Association

3. Many-to-One Association

4. Many-to-many Association

Generally speaking, although there are several ways of association, we can understand that it is only one way of association, that is, many-to-many association. This is because other ways of association can be understood as a special way of association.

Among them, the relationship mentioned above has one-way and two-way relationship.

This blog post focuses on the next one-to-one relationship.

One-to-one foreign key associations include:

1. Annotation implementation of one-to-one one one-to-one key Association

2. One-to-one and one-to-one xml implementation of key Association

3. Annotation Implementation of One-to-One Two-way Foreign Key Association

4. The xml implementation of one-to-one two-way foreign key Association

Primary key associations correspond to foreign keys.

One-to-one Association

There are many examples of one-to-one relationships in our lives, for example, "monogamy" is a one-to-one relationship. A student who has an ID card is also a one-to-one relationship.

One-to-one and one-to-one key Association Annotation implementation

One-to-one associations are modified with the annotation @OneToOne.

The following is an example of "monogamy".

Wife Entity Class

@Entity
    public class Wife {
    private int id;
    private String name;
    @Id
    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;
    }   
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Husband entity class

There is a reference wife of the Wife class in the Huaband entity class, and the annotation @OneToOne is added to the getWife method. If we want to specify the name of the foreign key, we specify it by annotation @JoinColumn(name="wifeId"), where wifeId is the name specified in our example.

The Husband entity class code is as follows:

@Entity
    public class Husband {
    private int id;
    private String name;
    private Wife wife;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    @OneToOne
    @JoinColumn(name="wifeId") //Specify the name of the foreign key as wifeId
    public Wife getWife() {
        return wife;
    }
    public void setWife(Wife wife) {
        this.wife = wife;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

The contents of the hibernate.cfg.xml file are no longer posted, similar to those of previous blogs.

The test code is as follows:

public class TeacherTest {
    @Test
    public void testSchema(){
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
        SchemaExport schemaExport = new SchemaExport();
        schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata);
    }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

The SchemaExport class is mainly used to export SQL statements to the database.

In the console, we will see the following main output information, including two table-building statements, and one statement inserting foreign keys.

Above all, we have established one-to-one one one one one-to-one key association between Husband and Wife through Annotation.

Implementation of one-to-one and one-to-one external key correlation XXX.hbm.xml

Now let's take another example to illustrate the one-to-one relationship in which a student has an ID card.

Class Student

package com.hibernate.model;

    public class Student {
    private int id;
    private String name;
    // getter and setter methods for id, num, student are omitted
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Class IdCard

public class IdCard {
    private int id;
    private String num;
    private Student student;

    // getter and setter methods for id, num, student are omitted
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The IdCard.hbm.xml file is as follows:

<?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 package="com.hibernate.model">
    <class name="IdCard" table="idcard" >
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="num" column="num"/>
        <!--  One-to-one associations are xml Use in documents many-to-one   and unique="true"Joint restrictions  -->
        <many-to-one name="student" column="studentId" unique="true"></many-to-one>      
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

In the above code: One-to-one association is specified in the following code:

<many-to-one name="student" column="studentId" unique="true"></many-to-one>
  • 1

The contents of Student.hbm.xml file are as follows:

<?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 package="com.hibernate.model">
    <class name="Student" table="student" >
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>     
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

In this way, we establish a one-to-one association between Student and IdCard through foreign keys.

The test file is the same as the test file for the first example. It is no longer posted here.

One-to-one two-way foreign key Association Annotation implementation

Class Student
There is a Wife reference wife in the class.

@Entity
    public class Husband {
    private int id;
    private String name;
    private Wife wife;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    @OneToOne
    @JoinColumn(name="wifeId") //Specify the name of the foreign key as wifeId
    public Wife getWife() {
        return wife;
    }
    //The get method of name attribute and the set method of all attributes are omitted.

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Wife Entity Class

@Entity
    public class Wife {
    private int id;
    private String name;
    private Husband husband;
    @OneToOne(mappedBy="wife") //What this means is that the external key association is done on Husband's wife object, that is, Husband is dominant.
    public Husband getHusband() {
        return husband;
    }
    public void setHusband(Husband husband) {
        this.husband = husband;
    }
    @Id
    public int getId() {
        return id;
    }
    //The get method of name attribute and the set method of all attributes are omitted.
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

The Wife class also has a reference to the Husband class and needs to be annotated with @OneToOne(mappedBy="wife") before the getHusband() method. If you only use @OneToOne instead of annotations for parameters, you create an external key Association for tables Husband and Wife, which is redundant.

The best way is to set @OneToOne(mappedBy="XXX") in all bidirectional associations.

One-to-one two-way foreign key Association XXX.hbm.xml file implementation

That is to say, both Student class and IdCard class have a reference from each other, and the content in IdCard.hbm.xml file does not change from one-to-one content, as follows:

<?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 package="com.hibernate.model">
    <class name="IdCard" table="idcard" >
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="num" column="num"/>
        <!--  One-to-one associations are xml Use in documents many-to-one   and unique="true"Joint restrictions  -->
        <many-to-one name="student" column="studentId" unique="true"></many-to-one>

    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

In the Student.hbm.xml file, there is a change.
Plus

<one-to-one name="idCard" class="IdCard" property-ref="student"/> 
  • 1

The complete contents are as follows:

<?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 package="com.hibernate.model">
    <class name="Student" table="student" >
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>  
        <!--property-ref The parameter is the class associated with this class IdCard Class Student Quotation   -->
        <one-to-one name="idCard" class="IdCard" property-ref="student"/>    
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Note: In databases, there is no difference between one-way and two-way, but the corresponding relationships in entity classes are different.

One-to-one primary key Association

In foreign key associations, for example, a wifeId field is generated in the Husband table as a foreign key, while primary key associations do not generate any other fields.

One-to-one One Primary Key Association Annotation Implementation

Use the following annotations to achieve:

@ Primary Key Join Column // / Use this annotation to represent primary key associations

@Entity
    public class Husband {
    private int id;
    private String name;
    private Wife wife;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    @OneToOne
    //@ Join Column (name= "wifeId")// Specify the name of the foreign key as wifeId
    @PrimaryKeyJoinColumn  //Use this annotation to represent primary key associations
    public Wife getWife() {
        return wife;
    }
    public void setWife(Wife wife) {
        this.wife = wife;
    }

    //The getter/setter method of some attributes is omitted
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

One-to-one primary key Association xml file implementation

In the document, the method screenshots given are as follows:

The above methods are applied to our Student and IdCard classes as follows:

Class Student

public class Student {
    private int id;
    private String name;    
    //... get, set method

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Class IdCard

public class IdCard {
    private int id;
    private String num;
    private Student student;
    //... get, set method
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

The IdCard.hbm.xml file is as follows:

Note: The id generation strategy at this time.

<?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 package="com.hibernate.model">
    <class name="IdCard" table="idcard" >
        <id name="id" column="id">
            <generator class="foreign">
                <param name="property">student</param>
            </generator>
        </id>
        <property name="num" column="num"/>
        <!--    -->
        <one-to-one name="student" class="Student" constrained="true"></one-to-one>                 
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

The Student.hbm.xml file is as follows

<?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 package="com.hibernate.model">
    <class name="Student" table="student" >
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name"/>       
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

The test results are as follows:

There is no more introduction about primary key Association here, because in actual projects, one-to-one association is not too much, even if it appears, we usually use one-to-one foreign key for association.

Summary

Because using Annotation is easier to understand and implement than using xml for association. Therefore, it is recommended to use.


from: http://blog.csdn.net/u010412719/article/details/51298343

Posted by student101 on Tue, 21 May 2019 14:33:31 -0700