我正在使用 HSQLDB + Hibernate 为我正在开发的应用程序运行一些单元测试。我有一个测试,它使一个实体与其他实体具有三个 OneToOne 关系。当我使用 MySql 运行测试时,我没有错误并且测试通过,但是当我对 HSQLDB 运行相同的测试时,我得到一个java.sql.SQLException:完整性约束违规 - 没有父 FK_...是 FK_ 之一在 Person 类的关系中定义的外键。
我的代码是:
@Entity
@Table(name = "PERSON")
public class Person {
private static final long serialVersionUID = 1L;
private Long oid;
private Address commercialAddress;
private Address homeAddress;
private Settings settings;
@Id
@GeneratedValue
@Column(name = "OID")
public Long getOID() {
return this.oid;
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "COMPANY_ADDRESS_OID", nullable = false)
@ForeignKey(name = "FK_PERSON__COMPANY_ADDRESS")
public Address getCommercialAddress() {
return this.commercialAddress;
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "HOME_ADDRESS_OID", nullable = false)
@ForeignKey(name = "FK_PERSON__HOME_ADDRESS")
public Address getHomeAddress() {
return this.homeAddress;
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "SETTINGS_OID", nullable = false)
@ForeignKey(name = "FK_PERSON__SETTINGS")
public Settings getSettings() {
return this.settings;
}
}
Address 和 Settings 类没有关系,它们是简单的休眠实体:
@Entity
@Table(name = "ADDRESS")
public class Address {
private Long oid;
private String city;
private String country;
private String province;
private String postalCode;
private String street;
@Id
@GeneratedValue
@Column(name = "OID")
public Long getOID() {
return this.oid;
}
...
}
@Entity
@Table(name = "Settings")
public class Settings {
private Long oid;
private String backgroundColor;
private String fontName;
private Integer fontSize;
private String themeName;
@Id
@GeneratedValue
@Column(name = "OID")
public Long getOID() {
return this.oid;
}
...
}
我使用谷歌进行了几次搜索,但找不到有用的东西。希望有人能指出我正确的方向。提前致谢。