映射实体中的重复列

时间:2018-09-14 05:02:24

标签: spring jpa spring-data-jpa

我在项目中使用了JPA嵌入式实体。

我有这个可嵌入的课程

@Embeddable
public class Address {

private String addressLine1;
private String addressLine2;
private String city;
private String state;
private String zipCode;

public String getAddressLine1() {
    return addressLine1;
}

public void setAddressLine1(String addressLine1) {
    this.addressLine1 = addressLine1;
}

public String getAddressLine2() {
    return addressLine2;
}

public void setAddressLine2(String addressLine2) {
    this.addressLine2 = addressLine2;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getZipCode() {
    return zipCode;
}

public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
 }

}

我正在将此类嵌入在这里

@Entity
public class Customer implements   
DomainObject{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Version
private Integer version;

private String firstName;
private String lastName;
private String email;
private String phoneNumber;

@Embedded
private Address billingAddress;

@Embedded
private Address shippingAddress;

@OneToOne
private User user;

@Override
public Integer getId() {
    return id;
}

@Override
public void setId(Integer id) {
    this.id = id;
}

public Integer getVersion() {
    return version;
}

public void setVersion(Integer version) {
    this.version = version;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public Address getBillingAddress() {
    return billingAddress;
}

public void setBillingAddress(Address billingAddress) {
    this.billingAddress = billingAddress;
}

public Address getShippingAddress() {
    return shippingAddress;
}

public void setShippingAddress(Address shippingAddress) {
    this.shippingAddress =     shippingAddress;
}
}

运行项目后我就是这个错误

映射实体中的重复列:
theo.tziomakas.udemy.domain.Customer列:billing_address_line1(应使用insert =“ false” update =“ false”来映射

如果需要,您可以克隆我的project并亲自检查。

我也尝试过this的答案,但是什么也没得到。

更新

我通过使用AttributeOverride注释解决了此错误。

@Embedded
@AttributeOverrides({
        @AttributeOverride( name = "addressLine1",
        column = @Column(name = "billing_address_line1")
        ),
        @AttributeOverride( name = "addressLine2",
                column = @Column(name = "billing_address_line2")
        ),
        @AttributeOverride( name = "city",
                column = @Column(name = "billing_city")
        ),
        @AttributeOverride( name = "state",
                column = @Column(name = "billing_state")
        ),
        @AttributeOverride( name = "zipCode",
                column = @Column(name = "billing_zip_code")
        )
})
private Address billingAddress;

@Embedded
@AttributeOverrides({
        @AttributeOverride( name = "addressLine1",
                column = @Column(name = "shipping_address_line1")
        ),
        @AttributeOverride( name = "addressLine2",
                column = @Column(name = "shipping_address_line2")
        ),
        @AttributeOverride( name = "city",
                column = @Column(name = "shipping_city")
        ),
        @AttributeOverride( name = "state",
                column = @Column(name = "shipping_state")
        ),
        @AttributeOverride( name = "zipCode",
                column = @Column(name = "shipping_zip_code")
        )
})
private Address shippingAddress;

1 个答案:

答案 0 :(得分:1)

因此我克隆了您的存储库,并发现问题是由于您有2次嵌入式地址-shippingAddress中的billingAddressCustomer.java 。我建议使用@AttributeOverride批注来解决此问题。参见此thread

像这样更改实体字段。

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="addressLine1",column=@Column(name="billingAddressLine1")),
    @AttributeOverride(name="addressLine2",column=@Column(name="billingAddressLine2")),
    @AttributeOverride(name="city",column=@Column(name="billingCity")),
    @AttributeOverride(name="state",column=@Column(name="billingState")),
    @AttributeOverride(name="zipCode",column=@Column(name="billingZipCode")),
})
private Address billingAddress;

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="addressLine1",column=@Column(name="shippingAddressLine1")),
    @AttributeOverride(name="addressLine2",column=@Column(name="shippingAddressLine2")),
    @AttributeOverride(name="city",column=@Column(name="shippingCity")),
    @AttributeOverride(name="state",column=@Column(name="shippingState")),
    @AttributeOverride(name="zipCode",column=@Column(name="shippingZipCode")),
})
private Address shippingAddress;