联合主键用的不是很多,用的时候查到就是。

Wife里id和name联合做主键,需要新建一个主键类,WifePK,必须实现Serializable接口,重写hashcode、equals。

在Husband类里用Wife的id和name做外键关联。

Wife类:

package com.oracle.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;

@Entity
@IdClass(WifePK.class)//指明你外键用的是哪个class
public class Wife {

    
    private int id;
    private String name;
    
    private int age;
    //指明id是一部分id
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    //name是一部分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;
    }
    
    
}

Husband类:

package com.oracle.hibernate;

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

@Entity
public class Husband {

    private int id;
    private String name;
    private Wife  wife;    //wife的引用
    
    @Id    //主键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;
    }
    
    @OneToOne    //一对一映射
    /**
     * 外键是两个字段,改变外键名字用joinColumns,值是joinColumn的数组
     * 指定id名字和参考的字段(因为是两个字段,不指定它不知道去找哪个)
     */
    @JoinColumns(
            {
                @JoinColumn(name="wifeId",referencedColumnName="id"),
                @JoinColumn(name="wifeName",referencedColumnName="name")
            }
    )
    public Wife getWife() {
        return wife;
    }
    public void setWife(Wife wife) {
        this.wife = wife;
    }
}

WifePK联合主键类:

package com.oracle.hibernate;

import java.io.Serializable;

/**
 * 联合主键类,不是实体,必须实现Serializable接口, 序列化,可以把这个对象写到硬盘上、读出来,或者在网络上传输。
 * 应该重写hashcode,equals,在内存里保证唯一性
 */
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();
    }

    // 重写equals,逻辑不能乱写
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof WifePK) {
            WifePK pk = (WifePK) obj;
            if (pk.id == this.getId() && pk.name.equals(this.getName()))
                return true;
        }
        return false;
    }

}

生成的表:

hibernate生成表的语句:

create table Husband (
id integer not null auto_increment,
name varchar(255),
wifeId integer,
wifeName varchar(255),
primary key (id)
)
07:14:29,202 DEBUG SchemaExport:377 -
create table Wife (
id integer not null,
name varchar(255) not null,
age integer not null,
primary key (id, name)
)
07:14:29,474 DEBUG SchemaExport:377 -
alter table Husband
add index FKAEEA401B2953CE18 (wifeId, wifeName),
add constraint FKAEEA401B2953CE18
foreign key (wifeId, wifeName)
references Wife (id, name)

 

 欢迎关注个人公众号一起交流学习: