sydncall
sydncall

Reputation: 43

hibernate gives two rows the same instead of two distinct rows

I have a user dao

@Entity
@Table(name="EBIGUSERTIM")
public class EbigUser {


private String id;
private Integer source;
private String entryscheme;
private String fullName;
private String email;

private Long flags;
private String status;
private String createdBy;
private Date createdStamp;
private String modifiedBy;
private Date modifiedStamp;

@Id
@Column(name="ID")
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
@Id
@Column(name="SOURCE")
public Integer getSource() {
    return source;
}
public void setSource(Integer source) {
    this.source = source;
}
@Column(name="ENTRYSCHEME")
public String getEntryscheme() {
    return entryscheme;
}
public void setEntryscheme(String entryscheme) {
    this.entryscheme = entryscheme;
}
@Column(name="FULLNAME")
public String getFullName() {
    return fullName;
}
public void setFullName(String fullName) {
    this.fullName = fullName;
}
@Column(name="EMAIL")
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
@Column(name="FLAGS")
public Long getFlags() {
    return flags;
}
public void setFlags(Long flags) {
    this.flags = flags;
}
@Column(name="STATUS")
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
@Column(name="CREATEDBY")
public String getCreatedBy() {
    return createdBy;
}
public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}
@Column(name="CREATEDSTAMP")
public Date getCreatedStamp() {
    return createdStamp;
}
public void setCreatedStamp(Date createdStamp) {
    this.createdStamp = createdStamp;
}
@Column(name="MODIFIEDBY")
public String getModifiedBy() {
    return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
    this.modifiedBy = modifiedBy;
}
@Column(name="MODIFIEDSTAMP")
public Date getModifiedStamp() {
    return modifiedStamp;
}
public void setModifiedStamp(Date modifiedStamp) {
    this.modifiedStamp = modifiedStamp;
}

i am selecting 2 rows out of the db. The sql works

select * from ebigusertim where id='blah'

It returns 2 distinct rows. When i query the data using hibernate, it appears that the object memory is not being allocated for each entry in the list. Thus, i get 2 entries in the list with the same object.

        Criteria userCriteria = session.createCriteria(EbigUser.class);
        userCriteria.add(Restrictions.eq("id", id));
        userlist = userCriteria.list();

Upvotes: 0

Views: 408

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34367

Why are you defining two id columns(both id and source are mapped with annotation @Id)?

    @Id
    @Column(name="ID")
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @Id
    @Column(name="SOURCE")
    public Integer getSource() {
        return source;
    }

Please remove one if it is by mistake. If both together make composite key, map them accordingly e.g.

  @Embeddable
  public class UserPK implements Serializable {

    @Column(name = "ID", nullable = false)
    private String id;

    @Column(name = "SOURCE", nullable = false)
    private Integer source;

    .....
    .....
   }


Use this new class in you original class as Id as below:

   @EmbeddedId
   private UserPK userPK;

Hope this helps.

Upvotes: 1

Related Questions