Search code examples
springjpaspring-data-jpa

Repeated column in mapping entity


I am using in my project JPA embedded entities. I have this embeddable class:

@Embeddable
public class Address {

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

    // getters and setters...
}

And I am embedding this class in here:

@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;

    // getters and setters...
}

After running the project, I got following error:

Repeated column in mapping entity:
theo.tziomakas.udemy.domain.Customer column: billing_address_line1 (should be mapped with insert = "false" update = "false"

How can I fix this error?


You can clone my project if you want and check it yourself. I have also tried this answer, but didn't get anything.


Solution

  • So I cloned your repository and found that the problem arises because you have Embedded Address 2 times - shippingAddress and billingAddress in Customer.java . I would recommend the using @AttributeOverride annotation to solve this issue. See this thread.

    Change the Entity Fields like this.

    @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;