尽管有其他帖子,我在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")
这里的代码:
销售.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) {
...
}
}
产品.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
值直接操作外键。这不是一个理想的情况,但如果你遇到这种情况,至少你可以定义关系,这样你就可以成功地使用JPQL。
使用这个,对我来说就是工作:
@Column(name = "candidate_id", nullable=false)
private Long candidate_id;
@ManyToOne(optional=false)
@JoinColumn(name = "candidate_id", insertable=false, updatable=false)
private Candidate candidate;
@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中的name和referencedColumnName设置了相同的值,hibernate会给出同样愚蠢的错误
错误:
由:org.hubinate.MappingException引起:实体com.test.SomeCustomEntity列的映射中重复列:column_NAME(应使用insert="false"update="false(进行映射
希望这会有所帮助
@OneToOne(optional = false)
@JoinColumn(name = "department_id", insertable = false, updatable = false)
@JsonManagedReference
private Department department;
@JsonIgnore
public Department getDepartment() {
return department;
}
@OneToOne(mappedBy = "department")
private Designation designation;
@JsonIgnore
public Designation getDesignation() {
return designation;
}
注意为任何属性只提供1个setter和getter。最好的方法是写下所有属性的定义,然后使用eclipse生成setter和getter实用程序,而不是手动执行。该选项出现在右键单击->源->生成Getter和Setter。
这意味着您要在实体类中映射两次列。举例说明。。。
@Column(name = "column1")
private String object1;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "column1", referencedColumnName = "column1")
private TableClass object2;
上面代码片段中的问题是我们正在重复映射。。。
解决方案
由于映射是一个重要的部分,您不想删除它。相反,您将删除
@Column(name = "column1")
private String uniqueId;
您仍然可以通过创建TableClass的对象来传递object1的值,并在其中分配object1的String值。
这100%有效。我已经用Postgres和Oracle数据库测试过了。
我们通过映射Grails 4(GORM(中的子实体而不是父实体来解决循环依赖关系(父子实体(。
示例:
Class Person {
String name
}
Class Employee extends Person{
String empId
}
//Before my code
Class Address {
static belongsTo = [person: Person]
}
//We changed our Address class to:
Class Address {
static belongsTo = [person: Employee]
}
我错误地在两个不需要的地方添加了OneToMany注释,删除了它,从而解决了问题。
我遇到了同样的问题,只需要为id和列创建不同的名称例如
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_product")
private long id;