can*_*man 95 hibernate java-ee mappingexception
尽管其他所有人都发布了,但我无法在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")
Run Code Online (Sandbox Code Playgroud)
这里的代码:
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
}
Run Code Online (Sandbox Code Playgroud)
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) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
JB *_*zet 111
消息很明确:映射中有重复的列.这意味着您将相同的数据库列映射两次.事实上,你有:
@Column(nullable=false)
private Long customerId;
Run Code Online (Sandbox Code Playgroud)
并且:
@ManyToOne(optional=false)
@JoinColumn(name="customerId",referencedColumnName="id_customer")
private Customer customer;
Run Code Online (Sandbox Code Playgroud)
(同样适用于productId
/ product
).
您不应该通过其ID引用其他实体,而应通过直接引用该实体.删除该customerId
字段,它没用.并做同样的事情productId
.如果您想要销售的客户ID,您只需要这样做:
sale.getCustomer().getId()
Run Code Online (Sandbox Code Playgroud)
小智 59
如果您遇到遗留数据库,其中有人已经放置了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;
Run Code Online (Sandbox Code Playgroud)
这允许您访问关系.但是,要添加/更新关系,您将直接通过其定义的@Column值操作外键.这不是一个理想的情况,但如果您处于这种情况,至少可以定义关系,以便您可以成功使用JPQL.
小智 17
用这个,对我有用:
@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;
Run Code Online (Sandbox Code Playgroud)
@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;
}
Run Code Online (Sandbox Code Playgroud)
如果你已经映射了一个列并且意外地为@JoinColumn中的name和referencedColumnName设置了相同的值,那么 hibernate给出了同样的愚蠢错误
错误:
引起:org.hibernate.MappingException:实体映射中的重复列:com.testtest.SomeCustomEntity列:COLUMN_NAME(应使用insert ="false"update ="false"映射)
归档时间: |
|
查看次数: |
166577 次 |
最近记录: |