我有一个父实体类和两个子实体类。在父类上,有三个用@ManyToOne注释的属性,以及其他五个用@Column注释的属性。
Hibernate希望在子数据库表(将来和库存)中使用@ManyToOne注释的三个属性(国家,交易所,部门)。
当数据库表“ future”和“ stock”中不存在这三个FK属性时,将引发以下异常: org.hibernate.tool.schema.spi.SchemaManagementException:模式验证:缺少列表[future]
中的[country_id]知道为什么吗? 是否需要在子DB表中重复/添加外键列?
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;
}
父母:文书
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="instr_type", discriminatorType = DiscriminatorType.STRING, length = 1)
@Table(name = "instrument")
public class Instrument extends BaseEntity {
@Column
private String symbol;
@Column
private String isin;
@Column
private String name;
@Column
@Enumerated(EnumType.STRING)
private LiquidEnum liquid;
@Column
private boolean active;
// FK
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false, foreignKey = @ForeignKey(name = "instrument_country_fk"))
private Country country;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "exchange_id", nullable = false, foreignKey = @ForeignKey(name = "instrument_exchange_fk"))
private Exchange exchange;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sector_id", nullable = true, foreignKey = @ForeignKey(name = "instrument_sector_fk"))
private Sector sector;
// FK end
getter / setter
...
}
子类别:未来
@Entity
@Table(name = "future")
@DiscriminatorValue("F")
@PrimaryKeyJoinColumn(name = "id")
public class Future extends Instrument {
@Column(name = "expirationdate")
private LocalDate expirationDate;
public LocalDate getExpirationDate() {
return expirationDate;
}
public void setExpirationdate(LocalDate expirationDate) {
this.expirationDate = expirationDate;
}
}
子类别:股票
@Entity
@Table(name = "stock")
@DiscriminatorValue("S")
@PrimaryKeyJoinColumn(name = "id")
public class Stock extends Instrument {
}
答案 0 :(得分:0)
我找到了问题的原因:外键对象(国家,交易所,部门)引用的是股票实体,而不是工具实体。
当我使用继承策略重构数据库模型(向父级添加实体“ Instrument”)时,我忘记将Country,Exchange和Sector中的@OneToMany属性重定向到Instrument实体。