org.hibernate.MappingException:实体映射中的重复列

时间:2013-03-18 11:09:45

标签: java hibernate jpa jpa-2.0

我有以下Entity类和复合主键的另一个类

实体类

@Entity
@Table(name = "PROJECTS")
public class Project {
private Integer SlNo;
private Long projectNo;
private Date projectDate;

@EmbeddedId
ProjectPK projectPK;

主要密钥类

public class ProjectPK implements Serializable {

private Integer SlNo;
@Column(name = "project_no", insertable = false, updatable = false)
private Long projectNo; 

public ProjectPK(){
}
// with getters and setters and equals and hashCode implementation 

问题是我得到以下例外

:org.hibernate.MappingException:Repeated column in mapping for entity
: test.Project column: projectNo (should be mapped with
insert="false" update="false")

我在Project Entity课程中添加了以下内容,但我得到了相同的异常

@Column(name = "project_no", insertable = false, updatable = false)

修改1

CriteriaBuilder cb = entityManager.getCriteriaBuilder();

CriteriaQuery<Long> countQ = cb.createQuery(Long.class);
Root<Project> empCount = countQ.from(Project.class);
countQ.select(cb.count(empCount));

TypedQuery<Long> countquery = entityManager.createQuery(countQ);// error in this line

4 个答案:

答案 0 :(得分:3)

为什么要两次声明SlNoprojectNo字段?只需将它们从Project类中删除即可。

或者,更好的是,删除ProjectPK类并将Project类修改为以下内容(请注意,此方法仅适用于Hibernate):

@Entity
@Table(name = "PROJECTS")
public class Project implements Serializable {

  @Id
  private Integer SlNo;

  @Id
  private Long projectNo;

  private Date projectDate;
}

答案 1 :(得分:1)

要实现所需的行为,可以使用@IdClass注释,如下所示 -

http://docs.oracle.com/javaee/5/api/javax/persistence/IdClass.html

以下是修改后的代码 -

@Entity
@Table(name = "PROJECTS")
@IdClass(ProjectPK.class)
public class Project {
   @Id
   private Integer SlNo;

   @Id
   private Long projectNo;

   private Date projectDate;
}


public class ProjectPK implements Serializable {

    @Column(name="sl_no")
    private Integer SlNo;

    @Column(name = "project_no")
    private Long projectNo; 
}

答案 2 :(得分:1)

已经有一段时间了。你的最后一个代码似乎不对。您创建了一个复合键,但没有使用它。它应该如下:

@Entity
public class Project {

    @EmbeddedId
    private ProjectPK projectPK;
    ...

}

答案 3 :(得分:0)

我已通过以下方式解决了这个问题

@Entity
@Table(name = "PROJECTS")
public class Project {
@Column(name = "SL_NO" , insertable = false, updatable = false)
private Integer SlNo;
@Column(name = "PROJECT_NO" , insertable = false, updatable = false)
private Long projectNo;
private Date projectDate;

@EmbeddedId
ProjectPK projectPK;

和主键类

@Embeddable
public class ProjectPK implements Serializable {
@Column(name = "SL_NO")
private Integer SlNo;
@Column(name = "PROJECT_NO")
private Long projectNo; 

//with hashCode and equals implementation