JPA Hibernate启动尝试两次删除外键

时间:2015-08-02 14:34:42

标签: hibernate jpa

当我将Hibernat设置为hbmddl = create

时,当应用程序启动一个外键关系时出现错误

父实体就是这一个 - 基类没有冲突的属性。

@Entity
@Table(name = "attributes_text_values")
@PrimaryKeyJoinColumn(name = "attributeValue_id", referencedColumnName = "id")
@DiscriminatorValue("TEXT")
public class TextAttributeValue extends AttributeValue {

    @OneToMany(mappedBy = "attributeValue", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<TextAttributeValueLabel> labels;

然后相关的类定义如下:

@Entity
@Table(name = "attributes_text_value_labels")
public class TextAttributeValueLabel extends AbstractAuditableEntity{

    @ManyToOne
    private AttributeValue attributeValue;

启动时我在日志中收到错误:

22:23:40.640 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger 109 logStatement - alter table attributes_text_value_labels drop foreign key FK_7yali4vrura8hbmpj2h6gyf4n
22:23:40.653 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger 109 logStatement - alter table attributes_text_value_labels drop foreign key FK_jn9v2dt1vcr1bf14p48wxgpnv
22:23:40.667 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger 109 logStatement - alter table attributes_text_value_labels drop foreign key FK_35dt165s920hrh03kupm0b9n7
22:23:40.683 DEBUG org.hibernate.engine.jdbc.spi.SqlStatementLogger 109 logStatement - alter table attributes_text_value_labels drop foreign key FK_35dt165s920hrh03kupm0b9n7
22:23:40.694 ERROR org.hibernate.tool.hbm2ddl.SchemaExport 426 perform - HHH000389: Unsuccessful: alter table attributes_text_value_labels drop foreign key FK_35dt165s920hrh03kupm0b9n7

尝试再次添加外键时出现类似错误。

我注意到它出于某种原因试图两次降低FK。

有谁知道为什么以及如何解决它?

1 个答案:

答案 0 :(得分:0)

多对一方必须是TextAttributeValue类型,因为它代表TextAttributeValueTextAttributeValueLabel之间的双向关联:

@Entity
@Table(name = "attributes_text_value_labels")
public class TextAttributeValueLabel extends AbstractAuditableEntity{

@ManyToOne
private TextAttributeValue attributeValue;

如果要在TextAttributeValueLabel中管理抽象类型集合,可以在AttributeValue实体上定义一对多方面。但它看起来不是你的情况。