不管其他人的帖子,我找不到一个解决这个错误的方法,在MacOSX,NetBeans 7.2上使用GlassFish。
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
}
发布于 2013-02-26 05:10:14
消息很清楚:映射中有一个重复的列。这意味着您将同一数据库列映射了两次。事实上,你有:
@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()
发布于 2014-05-13 19:30:39
如果您使用的是遗留数据库,其中有人已经在其上放置了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
值直接操作外键。这不是一种理想的情况,但如果您遇到这种情况,至少可以定义关系,以便能够成功地使用JPQL。
发布于 2017-06-14 16:13:56
使用这个,对我来说是很有用的:
@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;
https://stackoverflow.com/questions/15076463
复制相似问题