另一个映射实体错误的重复列

尽pipe所有其他人张贴,我无法find与Glassfish,在MacOSX,Netbeans 7.2这个错误的解决scheme。

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") 

这里代码:

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 } 

预先感谢您的时间!

消息很明确:映射中有一个重复的列。 这意味着你映射了相同的数据库列两次。 事实上,你有:

 @Column(nullable=false) private Long customerId; 

并且:

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

(对于productId / product也是一样)。

您不应该通过他们的ID引用其他实体,而是直接引用实体。 删除customerId字段,这是没用的。 并为productId做同样的事情。 如果你想要一个销售的客户ID,你只需要这样做:

 sale.getCustomer().getId() 

如果您遗留的遗留数据库中有人已经放置了JPA批注但尚未定义关系,而您现在正在试图定义它们以用于您的代码,那么您可能无法删除customerId @Column,因为其他代码可能已经直接引用它了。 在这种情况下,定义如下的关系:

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

这使您可以访问关系。 但是,要添加/更新关系,您将通过已定义的@Column值直接操作外键。 这不是一个理想的情况,但是如果你处在这种情况下,至less你可以定义关系,这样你就可以成功地使用JPQL。

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

如果你已经映射了一个列,并且意外地在@JoinColumn中namereferencedColumnName设置了相同的值,hibernate会给出同样的愚蠢的错误

错误:

由于:org.hibernate.MappingException:实体映射中的重复列:com.testtest.SomeCustomEntity列:COLUMN_NAME(应使用insert =“false”update =“false”映射)

使用这个,是为我工作的:

 @Column(name = "candidate_id", nullable=false) private Long candidate_id; @ManyToOne(optional=false) @JoinColumn(name = "candidate_id", insertable=false, updatable=false) private Candidate candidate; 

小心提供任何属性只有一个setter和getter。 最好的方法是写下所有属性的定义,然后使用eclipse generate setter和getter工具,而不是手工完成。 选项来右键单击 – >源 – >生成Getter和Setter。