尝试使用AttributeOverride注释映射继承的属性时,OpenJPA会抛出一个错误,指出它无法在超类中找到该属性。我不确定如何以正确的方式映射。
错误
Embedded property "Order.mailingAddress" declares a mapping override for "addressLine", but that is not a persistent property in the embedded type.
代码
@Embeddable
public class Address{
private String addressLine;
private String city;
private String state;
private String zip;
//getters and setters
}
@Embeddable
public class ExtendedAddress extends Address{
private String additionalAddressLine;
//getters and setters
}
@Entity
public class Order {
@Id
private id;
@OneToOne
private Customer customer;
@Embedded
@AttributeOverrides(value={
@AttributeOverride(name="addressLine",
column=@Column(name="mailingAddressLine")),
@AttributeOverride(name="additionalAddressLine",
column=@Column(name="mailingAddressLine2")),
@AttributeOverride(name="city",
column=@Column(name="mailingAddressCity")),
@AttributeOverride(name="state",
column=@Column(name="mailingAddressState")),
@AttributeOverride(name="zip",
column=@Column(name="mailingAddressZip")),
})
private ExtendedAddress mailingAddress;
@Embedded
@AttributeOverrides(value={
@AttributeOverride(name="addressLine",
column=@Column(name="billingAddressLine")),
@AttributeOverride(name="city",
column=@Column(name="billingAddressCity")),
@AttributeOverride(name="state",
column=@Column(name="billingAddressState")),
@AttributeOverride(name="zip",
column=@Column(name="billingAddressZip")),
})
private Address billingAddress;
//getters and setters
//hashcode
//equals
}
SQL
CREATE TABLE Orders (
id INT PRIMARY KEY GENERATED ALWAYS,
mailingAddressLine VARCHAR(45) DEFAULT NULL,
mailingAddressLine2 VARCHAR(45) DEFAULT NULL,
mailingAddressCity VARCHAR(45) DEFAULT NULL,
mailingAddressState CHAR(2) DEFAULT NULL,
mailingAddressZip CHAR(9) DEFAULT NULL,
billingAddressLine VARCHAR(45) DEFAULT NULL,
billingAddressCity VARCHAR(45) DEFAULT NULL,
billingAddressState CHAR(2) DEFAULT NULL,
billingAddressZip CHAR(9) DEFAULT NULL
)
答案 0 :(得分:2)
将您的Embeddables(Address和ExtendedAddress)中的访问级别从private更改为protected或default。