Hibernate:复合主键 - 外键:如何在两个表之间映射各个字段

Hibernate: Composite primary key - foreign key: How individual fields are mapped between two tables

我试图理解休眠中两个实体之间的 OneToOne 映射,还有一个条件,即主键(以及外键)必须是复合的。

UserID 可嵌入类型:

@Embeddable
public class UserID  implements Serializable {

    private static final long serialVersionUID = 1L;
    private int ssnID;
    private int uniqueNum;
    // getters and setters..
}

VehicleID 可嵌入类型:

@Embeddable
public class VehicleID implements Serializable {
    private int vehicleID;
    private int regNum;
    // getters and setters
}

具有复合主键 VehicleID 的车辆实体:

@Entity
public class Vehicle {

    @EmbeddedId
    private VehicleID vehicleID;
    private String description;
    // getters and setters..
}

UserInfo 实体具有复合主键 UserID,并且是 UserInfo 和 Vehicle 之间 OneToOne 关系的拥有方实体:

@Entity
public class UserInfo {
    private String full_name;
    @EmbeddedId
    UserID userID;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumns({@JoinColumn(name="XYZ_ID") , @JoinColumn(name="ABC_Num")})
    Vehicle veh;

    // getters, setters and other code
}

当我运行这个程序时,下面是Hibernate的SQL输出:

Hibernate: drop table UserInfo if exists

Hibernate: drop table Vehicle if exists

Hibernate: create table UserInfo (ssnID integer not null, uniqueNum integer not null, full_name varchar(255), XYZ_ID integer, ABC_Num integer, primary key (ssnID, uniqueNum))

Hibernate: create table Vehicle (regNum integer not null, vehicleID integer not null, description varchar(255), primary key (regNum, vehicleID))

Hibernate: alter table UserInfo add constraint FKlbuhulsmr2lghirdvem61b9lb foreign key (XYZ_ID, ABC_Num) references Vehicle

简而言之,UserInfo 是拥有方实体,与 Vehicle 实体(以复合 VehicleID 作为主键)具有 OneToOne 关系。

我的疑惑在这里:

UserInfo 实体中,我们指定 OneToOne 关系(和注释):

@OneToOne(cascade=CascadeType.ALL)
@JoinColumns({@JoinColumn(name="XYZ_ID") , @JoinColumn(name="ABC_Num")})
Vehicle veh;

如上所示,在 UserInfo table 中,我们在 Vehicle table 上有一个复合外键,[=22= 的名称] 指定为 XYZ_IDABC_Num.

我的疑问是 XYZ_IDABC_Num 对应于 Vehicle table 中的哪些字段?

谁能帮我理解一下?

到车辆主键(ssnID,uniqueNum)