在HSQLDB中插入数据时违反完整性约束

时间:2013-05-03 13:12:16

标签: java mysql hibernate unit-testing hsqldb

我正在使用HSQLDB + Hibernate为我正在开发的应用程序运行一些单元测试。我有一个测试,它持久存在与其他实体有三个OneToOne关系的实体。当我使用MySql运行测试时我没有错误并且测试通过,但是当我针对HSQLDB运行相同的测试时,我得到 java.sql.SQLException:完整性约束违规 - 没有父FK _... 作为Person类关系中定义的FK_外键之一。

我的代码是:

@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类没有关系,它们是简单的hibernate实体:

@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;
    }

    ...
}

我使用谷歌进行了几次搜索,但找不到有用的东西。希望有人能指出我正确的方向。 提前谢谢。

0 个答案:

没有答案