我的想法是,我的供应商可以拥有几对VCurrency和Currency。
例如,SupplierA可以拥有这些VC货币和货币联盟:
这是我的供应商类:
@Entity
@Table(name = "supplier")
public class Supplier {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long idSupplier;
private String name;
@OneToMany(mappedBy = "supplier")
private List<SupplierCurrency> availableCurrencies;
//Getters and setters
}
这是我的VCurrency类:
@Entity
@DiscriminatorValue("V")
public class VCurrency extends Currency {
@ManyToMany(mappedBy = "vCurrency")
private List<SupplierCurrency> suppliers;
//Getters and setters
}
这是我的货币类:
@Entity
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="currencyType",
discriminatorType= DiscriminatorType.STRING)
public class Currency {
@Id
private String symbol;
private String name;
要合并这一切,这里是我的SupplierCurrency类
@Entity
@Table(name = "supplier_currency")
public class SupplierCurrency {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long idSupplierCurrency;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="supplier")
private Fournisseur supplier;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="v_currency")
private VCurrency vCurrency;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="currency")
private Currency Currency;
}
但是当我启动Spring App时,我收到了这个错误:
org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table supplier_currency add constraint FKt2waed3bwbw43k9xkiqebjxjh foreign key (id_supplier_currency) references supplier_currency (id_supplier_currency)]
java.sql.SQLException: Error on rename of '.\#sql-1b0c_1a9' to '.\supplier_currency' (errno: 150 "Foreign key constraint is incorrectly formed")
就像Hibernate试图将supplier_currency链接到自身。