SpringBoot 2.0.1。我正在尝试设置一个通用的“Address”对象,该对象可以附加到各种“AddressableDomainObject”对象上。下面的代码成功构建了Hibernate会话,但是当我尝试检索Company对象时,映射问题:
MappingException: Foreign key (FKk0e4770oa17lasc49nfp6mfxm:addresses [owner_object_type,owner_object_id])) must have same number of columns as the referenced primary key (contacts [id])
我想要它做的是“从地址中选择owner_object_id =?和owner_object_type ='COMPANY'”来构建列表。在地址方面,当owner_object_type ='COMPANY'时,它应“从id = addresses.owner_object_id的公司中选择”
表定义
create table addresses(
id bigint,
owner_object_type varchar(40),
owner_object_id bigint,
address ...
)
create table companies(
id bigint,
legal_name varchar(80),
name ...
)
通用接口:
public interface AddressableDomainObject {
public String getObjectType();
public long getId();
}
package-info.java
@AnyMetaDef(
name = "AddressableDomainObjectMetaDef",
metaType = "string",
idType = "long",
metaValues = {
@MetaValue(value = "CONTACT", targetEntity = Contact.class),
@MetaValue(value = "COMPANY", targetEntity = Company.class)
}
)
Company.java
@Entity
@Table(name = "companies")
@SequenceGenerator(name = "seq_companies", initialValue = 1)
@DiscriminatorValue("COMPANY")
public class Company implements AddressableDomainObject {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_companies")
long id;
String name;
String legalName;
String companyType;
Long parentCompanyId;
@Override
public final String getObjectType(){
return "COMPANY";
}
@OneToMany(
mappedBy = "ownerObject",
cascade = CascadeType.REMOVE)
List<Address> addresses;
}
Address.java
@Entity
@Table(name = "addresses")
@SequenceGenerator(name = "seq_addresses", initialValue = 1)
public class Address implements UsableDomainObject {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_addresses")
private long id;
private String usage;
private String address;
private String address2;
private String city;
private String state;
private String postalCode;
private String country;
private String gpsCoordinates;
private String notes;
@Any(
metaDef = "AddressableDomainObjectMetaDef",
metaColumn = @Column(name="owner_object_type")
)
@JoinColumn(name = "owner_object_id")
private AddressableDomainObject ownerObject;
}