hibernate给出两行相同而不是两行

时间:2012-10-08 23:29:25

标签: java hibernate dao

我有一个用户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;
}

我从数据库中选择2行。 sql工作

select * from ebigusertim where id='blah'

返回2个不同的行。当我使用hibernate查询数据时,似乎没有为列表中的每个条目分配对象内存。因此,我在列表中获得了2个具有相同对象的条目。

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

1 个答案:

答案 0 :(得分:1)

为什么要定义两个id列(idsource都使用注释@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;
    }

如果是误,请删除一个。如果两者一起制作composite key,则相应地映射它们,例如

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

希望这有帮助。