问题描述
尽管所有其他人都发布了,但我无法在GlassFish上找到解决方案,在MacOSX上,NetBeans 7.2中。 $ c $>这里错误:SEVERE:调用类时的异常org.glassfish.persistence.jpa.JPADeployer
准备方法
SEVERE:准备应用程序时出现异常
SEVERE:[ PersistenceUnit:supmarket]无法构建EntityManagerFactory
...
引起:org.hibernate.MappingException:实体映射中的重复列:
com.supmarket .entity.Sale列:customerId
(应该使用insert =falseupdate =false)映射
这里代码:
Sale.java
@Entity
public class Sale {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private long idFromAgency;
私人浮动金额amountSold;
私人字符串机构;
@Temporal(javax.persistence.TemporalType.DATE)
private创建日期;
@Column(nullable = false)
private long productId;
@Column(nullable = false)
private long customerId;
@ManyToOne(可选= false)
@JoinColumn(name =productId,referencedColumnName =id_product)
private Product product;
@ManyToOne(可选= false)
@JoinColumn(name =customerId,referencedColumnName =id_customer)
私人客户客户;
$ b public void Sale(){} $ b $ public void Sale(Long idFromAgency,float amountSold,String agency
,Date createDate,long productId,long customerId){
...
}
//然后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;
私人字符串性别,
maritalState,
名字,
姓氏,
incomeLevel;
@OneToMany(mappedBy =customer,targetEntity = Sale.class,fetch = FetchType.EAGER)
私人收藏销售;
$ b $ public void Customer(){}
$ b public void Customer(Long idFromAgency,String gender,String maritalState,
String firstname,String lastname, String incomeLevel){
...
}
}
Product.java
public class Product {
@I $
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =id_product)
private long id_product;
@Column(nullable = false)
private long idFromAgency;
私人字符串名称;
@OneToMany(mappedBy =product,targetEntity = Sale.class,fetch = FetchType.EAGER)
私人收藏销售;
//构造函数+ getters + setters
}
消息很明确:映射中有一个重复的列。这意味着你映射了两次相同的数据库列。事实上,你有:
@Column(nullable = false)
private long customerId;
还有:
@ManyToOne(可选= false)
@JoinColumn(name =customerId,referencedColumnName =id_customer)
私人客户客户;
(和 productId
/ product
)。
您不应该通过其ID来引用其他实体,而是通过直接引用实体。删除 customerId
字段,这是无用的。并为 productId
做同样的事情。如果你想要一个销售的客户ID,你只需要这样做:
sale.getCustomer()。getId()
Despite all of the others post, I can't find a solution for 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 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
}
The message is clear: you have a repeated column in the mapping. That means you mapped the same database column twice. And indeed, you have:
@Column(nullable=false)
private Long customerId;
and also:
@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer")
private Customer customer;
(and the same goes for productId
/product
).
You shouldn't reference other entities by their ID, but by a direct reference to the entity. Remove the customerId
field, it's useless. And do the same for productId
. If you want the customer ID of a sale, you just need to do this:
sale.getCustomer().getId()
这篇关于另一个映射实体错误的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!