这是我的 SQL 表的样子:
CREATE TABLE IF NOT EXISTS `test`.`Families` (
`id` INT NOT NULL AUTO_INCREMENT,
`mother_id` INT DEFAULT NULL ,
`father_id` INT DEFAULT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `test`.`Parents` (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(50) DEFAULT NULL,
`last_name` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
这就是我的家庭实体的样子:
@Entity
@Table(name="Families")
public class Family implements Serializable {
@Id
@Column(name="id")
private String id;
@Column(name="mother_id")
private int mother;
@Column(name="father_id")
private int father;
}
这很棒,但如果我能做这样的事情,我真的很喜欢(注意我也已经定义了一个父实体):
@Entity
@Table(name="Families")
public class Family implements Serializable {
@Id
@Column(name="id")
private String id;
@OneToOne
@Column(name="mother_id")
private Parent mother;
@OneToOne
@Column(name="father_id")
private Parent father;
}
我怎么能做到这一点呢?