One-to-one Joint Key Linkages in Hibernate Learning Notes 7

One-to-one Joint Key Linkages in Hibernate Learning Notes 7

We explained one-to-one foreign key Association and one-to-one primary key Association earlier. This blog article introduces joint primary key association.

First of all, review the main keys, you can refer to the previous blog article: http://blog.csdn.net/u010412719/article/details/51275744

Joint primary key linkages

Assuming there are Husband entity classes and Wife entity classes, the primary key in Husband class is id, while the primary key in Wife entity class is dominated by ID and name.

First look at the Wife entity class

The mapping of joint primary keys takes the third approach here: @IdClass class. Among them, the primary key class WifePK inherits the Serialable interface and rewrites equals and hashCode methods.

The code is as follows:

@Entity
    @IdClass(WifePK.class)
    public class Wife {
    private int id;
    private String name;
    private int age;
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = 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;
    }   

    }


    package com.hibernate.model;

    import java.io.Serializable;

    public class WifePK implements Serializable{
    private int id;
    private String name;
    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;
    }
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
    @Override
    public boolean equals(Object obj) {
         if (obj instanceof WifePK) {
             WifePK pk = (WifePK) obj;
             if(this.id==pk.getId()&&this.name.equals(pk.getName())){
                 return true;
             }          
        }
        return false;
    }

    }
  • 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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

Class Husband

In a single primary key, we use a one-to-one annotation: @JoinColumn(name="wifeId").

Now there are two primary keys in wife, what should we do?

The methods are as follows:

@OneToOne
    @JoinColumns(
            {
                @JoinColumn(name="wifeId",referencedColumnName="id"),
                @JoinColumn(name="wifeName",referencedColumnName="name")
            }   
    )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The Husband class details are as follows:

@Entity
    public class Husband {
    private int id;
    private String name;
    private Wife wife;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    @OneToOne
    @JoinColumns(
            {
                @JoinColumn(name="wifeId",referencedColumnName="id"),
                @JoinColumn(name="wifeName",referencedColumnName="name")
            }   
    )

    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
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

The test class code is as follows:

The test code mainly borrows SchemaExport to output the SQL statements to the database.

public class Test {

    @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
  • 11

The results are as follows:

As can be seen from the results, this establishes a one-to-one association between union primary keys.

Summary

The above mainly introduces the implementation of Annotation of one-to-one primary key association, and the implementation of.xml file is rather cumbersome, so it will not be introduced here.


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

Keywords: Hibernate Database Java SQL

Added by red-x on Wed, 22 May 2019 03:03:15 +0300