Jacob
Jacob

Reputation: 14731

org.hibernate.MappingException:Repeated column in mapping for entity

I have the following Entity class and another class for composite primary keys

Entity class

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

@EmbeddedId
ProjectPK projectPK;

Primary Key class

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 

The problem is I am getting the following exceptions

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

I have added the following in Project Entity class, but I get the same exception

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

Edit 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

Upvotes: 0

Views: 6595

Answers (4)

vic
vic

Reputation: 2818

It has been for awhile. Your last code doesn't seem to be right. You created a compound key, but didn't use it. It shall be the following:

@Entity
public class Project {

    @EmbeddedId
    private ProjectPK projectPK;
    ...

}

Upvotes: 1

Jacob
Jacob

Reputation: 14731

I have resolved the issue by the following manner

@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;

and Primary Key class

@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

Upvotes: 0

Sashi
Sashi

Reputation: 2415

To achieve your desired behavior, you can use the @IdClass annotation as shown below -

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

Here is the modified code -

@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; 
}

Upvotes: 1

overmeulen
overmeulen

Reputation: 1158

Why are you declaring SlNo and projectNo fields twice ? Just remove them from from the Project class and it should be fine.

Or, even better, remove the ProjectPK class and modify the Project class to the following (note that this method only works with Hibernate) :

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

  @Id
  private Integer SlNo;

  @Id
  private Long projectNo;

  private Date projectDate;
}

Upvotes: 3

Related Questions