我有一个用户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个不同的行。当我使用休眠查询数据时,似乎没有为列表中的每个条目分配对象内存。因此,我在列表中获得了2个具有相同对象的条目。

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

最佳答案

为什么要定义两个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;


希望这可以帮助。

关于java - hibernate 使两行相同而不是两行不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12790991/

10-12 01:31