package org.jixiuf.bean; 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 H2 { int id ; String name; W2 w; @Id @GeneratedValue public int getId() { return id; } @OneToOne @JoinColumns( { @JoinColumn(name="wifeid" ,referencedColumnName="id"), @JoinColumn(name="wifename" ,referencedColumnName="name"), } ) public W2 getW() { return w; } public void setW(W2 w) { this.w = w; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } package org.jixiuf.bean; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.OneToOne; @Entity @IdClass(value = W2ID_Pk.class) public class W2 { int id; String name; @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; } } package org.jixiuf.bean; import java.io.Serializable; public class W2ID_Pk implements Serializable{ private static final long serialVersionUID = 1L; int id ; 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; } } create table H2 ( id integer not null auto_increment, name varchar(255), wifeid integer, wifename varchar(255), primary key (id) ) create table W2 ( id integer not null, name varchar(255) not null, primary key (id, name) ) 1 alter table H2 add index FK8EAEAED791E (wifeid, wifename), add constraint FK8EAEAED791E foreign key (wifeid, wifename) references W2 (id, name) alter table Wife add index FK292331B1B6833F (husband_id), add constraint FK292331B1B6833F foreign key (husband_id) references Husband (id)