0

我有以下实体类和另一个用于复合主键的类

实体类

@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

4 回答 4

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;
}
于 2013-03-18T11:50:19.503 回答
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; 
}
于 2013-03-18T14:52:42.737 回答
1

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

@Entity
public class Project {

    @EmbeddedId
    private ProjectPK projectPK;
    ...

}
于 2014-01-30T01:43:23.360 回答
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
于 2013-03-18T18:13:18.310 回答