Another repeating column when matching entity error

Despite all the other posts, I canโ€™t find a solution to this error with Glassfish, on MacOSX, Netbeans 7.2.

Here the error : SEVERE: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method SEVERE: Exception while preparing the app SEVERE: [PersistenceUnit: supmarket] Unable to build EntityManagerFactory ... Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.supmarket.entity.Sale column: customerId (should be mapped with insert="false" update="false") 

Here is the code:

Sale.java

 @Entity public class Sale { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(nullable=false) private Long idFromAgency; private float amountSold; private String agency; @Temporal(javax.persistence.TemporalType.DATE) private Date createdate; @Column(nullable=false) private Long productId; @Column(nullable=false) private Long customerId; @ManyToOne(optional=false) @JoinColumn(name="productId",referencedColumnName="id_product") private Product product; @ManyToOne(optional=false) @JoinColumn(name="customerId",referencedColumnName="id_customer") private Customer customer; public void Sale(){} public void Sale(Long idFromAgency, float amountSold, String agency , Date createDate, Long productId, Long customerId){ ... } // then getters/setters } 

Customer.java

 @Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id_customer") private Long id_customer; @Column(nullable=false) private Long idFromAgency; private String gender, maritalState, firstname, lastname, incomeLevel; @OneToMany(mappedBy="customer",targetEntity=Sale.class, fetch=FetchType.EAGER) private Collection sales; public void Customer(){} public void Customer(Long idFromAgency, String gender, String maritalState, String firstname, String lastname, String incomeLevel) { ... } } 

Product.java

 public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id_product") private Long id_product; @Column(nullable=false) private Long idFromAgency; private String name; @OneToMany(mappedBy="product",targetEntity=Sale.class, fetch=FetchType.EAGER) private Collection sales; //constructors + getters +setters } 

Thanks in advance for your time!

+54
java-ee hibernate
Feb 25
source share
5 answers

The message is clear: you have a repeated column in the mapping. This means that you have mapped one database column twice. And really, you have:

 @Column(nullable=false) private Long customerId; 

as well as:

 @ManyToOne(optional=false) @JoinColumn(name="customerId",referencedColumnName="id_customer") private Customer customer; 

(and the same for productId / product ).

You should not refer to other objects by their identifier, but to a direct link to the object. Remove the customerId field, it is useless. And do the same for productId . If you need a customer identifier for a sale, you just need to do this:

 sale.getCustomer().getId() 
+67
Feb 25 '13 at 21:10
source share

If you are stuck in an outdated database where someone already posted JPA annotations but did NOT define relationships, and now you are trying to determine them for use in your code, then you will not be able to remove clientId @Column since other code may directly reference it . In this case, define the relationship as follows:

 @ManyToOne(optional=false) @JoinColumn(name="productId",referencedColumnName="id_product", insertable=false, updatable=false) private Product product; @ManyToOne(optional=false) @JoinColumn(name="customerId",referencedColumnName="id_customer", insertable=false, updatable=false) private Customer customer; 

This allows you to access relationships. However, to add / update relationships, you will manipulate foreign keys directly through their specific @Column values. This is not an ideal situation, but if you are given such a situation, at least you can define relationships to successfully use JPQL.

+31
May 13 '14 at 11:30
source share
 @Id @Column(name = "COLUMN_NAME", nullable = false) public Long getId() { return id; } @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = SomeCustomEntity.class) @JoinColumn(name = "COLUMN_NAME", referencedColumnName = "COLUMN_NAME", nullable = false, updatable = false, insertable = false) @org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.ALL) public List<SomeCustomEntity> getAbschreibareAustattungen() { return abschreibareAustattungen; } 

If you have already mapped a column and accidentally set the same values โ€‹โ€‹for name and referatedColumnName in @JoinColumn , hibernation gives the same nonsense error

Mistake:

Called: org.hibernate.MappingException: repeated column in the mapping for the object: com.testtest.SomeCustomEntity column: COLUMN_NAME (must be mapped to insert = "false" update = "false")

+7
Mar 03 '16 at 11:31
source share

use this, this is work for me:

 @Column(name = "candidate_id", nullable=false) private Long candidate_id; @ManyToOne(optional=false) @JoinColumn(name = "candidate_id", insertable=false, updatable=false) private Candidate candidate; 
+2
Jun 14 '17 at 8:13
source share

Take care of providing only one setter and getter for any attribute. The best way to get closer is to write down the definition of all the attributes, and then use the eclipse generate setter and getter, rather than doing it manually. The option comes with the right mouse button -> source -> Generate Getter and Setter.

0
Jun 20 '16 at 6:02
source share



All Articles