java - Hibernate 标准给出无效结果

标签 java hibernate orm criteria hibernate-criteria

我有一个类似的功能:

public List<PortalUsers> getUsersForYSSSync() {
    List<PortalUsers> usersList = null;
    LOGGER.info("getUsersForYSSSync()..... Start");

    try {
        Criteria criteria = getSession().createCriteria(PortalUsers.class);
        criteria.add(Restrictions.eq("isSynchronizedtoYSS", "N"));
        usersList = criteria.list();

        LOGGER.info("getUsersForYSSSync()..... End");
    } catch (HibernateException ex) {
        LOGGER.error("Error in getUsersForYSSSync()", ex);
    }

    return usersList;
}

和我的 PortalUsers.java 类如下:

@Entity
@Table(name = "portalusers")
public class PortalUsers implements java.io.Serializable {

    private String portalUsersId;
    private UserRoles userRoles;
    private String email;
    private String openId;
    private String firstName;
    private String middleName;
    private String lastName;
    private String address;
    private String zipCode;
    private String city;
    private Date dateOfBirth;
    private String telephone;
    private String mobile;
    private String gender;
    private Date syncUpdateDateTime;
    private Character status;
    private Date createdDate;
    private Date lastUpdateDate;
    private String reminderMailCount;
    private Set<SubscriptionHasUsers> subscriptionhasuserses = new HashSet<SubscriptionHasUsers>(
            0);
    private Integer externalId;
    private String proposedOpenId;
    private Set<Subscriptionproductplan> subscriptionproductplans = new HashSet<Subscriptionproductplan>(0);
    private String createdBy;
    private String updatedBy;
    private String accountingFirmId;

    private String invitationId;
    private Subscriptongroup subscriptongroup;

    private String userName;
    private String password;
    private Character isSynchronizedtoYSS;

    public PortalUsers() {
    }

    public PortalUsers(Date syncUpdateDateTime) {
        this.syncUpdateDateTime = syncUpdateDateTime;
    }

    public PortalUsers(UserRoles userroles, Subscriptongroup subscriptongroup,String email, String openId,
            String firstName, String middleName, String lastName,
            String address, String zipCode, String city, Date dateOfBirth,
            String telephone, String mobile, String gender,
            Date syncUpdateDateTime, Character status, Date createdDate , Date lastUpdateDate, String reminderMailCount,String proposedOpenId,
            Set<SubscriptionHasUsers> subscriptionhasuserses,String userName,String password) {
        this.userRoles = userroles;
        this.email = email;
        this.openId = openId;
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.address = address;
        this.zipCode = zipCode;
        this.city = city;
        this.dateOfBirth = dateOfBirth;
        this.telephone = telephone;
        this.mobile = mobile;
        this.gender = gender;
        this.syncUpdateDateTime = syncUpdateDateTime;
        this.status = status;
        this.createdDate = createdDate;
        this.lastUpdateDate = lastUpdateDate;
        this.reminderMailCount = reminderMailCount;
        this.subscriptionhasuserses = subscriptionhasuserses;
        this.proposedOpenId = proposedOpenId;
        this.subscriptongroup=subscriptongroup;
        this.userName=userName;
        this.password=password;
    }

    @Id
    @Column(name = "PortalUsers_id", unique = true, nullable = false)
    public String getPortalUsersId() {
        return this.portalUsersId;
    }

    public void setPortalUsersId(String portalUsersId) {
        this.portalUsersId = portalUsersId;
    }

    /**
     * @return the userRoles
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "UserRoles_Id")
    public UserRoles getUserRoles() {
        return userRoles;
    }

    /**
     * @param userRoles
     *            the userRoles to set
     */
    public void setUserRoles(UserRoles userRoles) {
        this.userRoles = userRoles;
    }

    @Column(name = "Email", length = 50)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "OpenID", length = 200)
    public String getOpenId() {
        return this.openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    @Column(name = "FirstName", length = 50)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "MiddleName", length = 20)
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @Column(name = "LastName", length = 50)
    public String getLastName() {
        return this.lastName;
    }

    public String getMiddleName() {
        return middleName;
    }



    @Column(name = "Address", length = 250)
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "ZipCode", length = 10)
    public String getZipCode() {
        return this.zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Column(name = "City", length = 100)
    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "DateOfBirth", length = 10)
    public Date getDateOfBirth() {
        return this.dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    @Column(name = "Telephone", length = 20)
    public String getTelephone() {
        return this.telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Column(name = "Mobile", length = 20)
    public String getMobile() {
        return this.mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Column(name = "Gender", length = 1)
    public String getGender() {
        return this.gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "SyncUpdateDateTime", nullable = false, length = 19)
    public Date getSyncUpdateDateTime() {
        return this.syncUpdateDateTime;
    }

    public void setSyncUpdateDateTime(Date syncUpdateDateTime) {
        this.syncUpdateDateTime = syncUpdateDateTime;
    }

    @Column(name = "Status", length = 1)
    public Character getStatus() {
        return this.status;
    }

    public void setStatus(Character status) {
        this.status = status;
    }


    @Column(name = "CreatedDate")
    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    @Column(name = "LastUpdateDate")
    public Date getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Date lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }
    @Column(name = "ReminderMailCount")
    public String getReminderMailCount() {
        return reminderMailCount;
    }

    public void setReminderMailCount(String reminderMailCount) {
        this.reminderMailCount = reminderMailCount;
    }

    @Column(name = "ProposedOpenId")
    public String getProposedOpenId() {
        return proposedOpenId;
    }

    public void setProposedOpenId(String proposedOpenId) {
        this.proposedOpenId = proposedOpenId;
    }

    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch=FetchType.EAGER, mappedBy = "portalusers")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
                org.hibernate.annotations.CascadeType.DELETE,
                org.hibernate.annotations.CascadeType.MERGE,
                org.hibernate.annotations.CascadeType.PERSIST,
                org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    public Set<SubscriptionHasUsers> getSubscriptionhasuserses() {
        return this.subscriptionhasuserses;
    }

    public void setSubscriptionhasuserses(
            Set<SubscriptionHasUsers> subscriptionhasuserses) {
        this.subscriptionhasuserses = subscriptionhasuserses;
    }

    @Column(name="ExternalId")
    public Integer getExternalId() {
        return this.externalId;
    }

    public void setExternalId(Integer externalId) {
        this.externalId = externalId;
    }
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="createdBy")
    public Set<Subscriptionproductplan> getSubscriptionproductplans() {
        return subscriptionproductplans;
    }

    public void setSubscriptionproductplans(
            Set<Subscriptionproductplan> subscriptionproductplans) {
        this.subscriptionproductplans = subscriptionproductplans;
    }

    @Column(name="CreatedBy")
    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Column(name="UpdatedBy")
    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    @Column(name="AccountingFirmId")
    public String getAccountingFirmId() {
        return accountingFirmId;
    }

    public void setAccountingFirmId(String accountingFirmId) {
        this.accountingFirmId = accountingFirmId;
    }

    @Column(name = "InvitationId")
    public String getInvitationId() {
        return invitationId;
    }

    public void setInvitationId(String invitationId) {
        this.invitationId = invitationId;
    }

    public void setSubscriptongroup(Subscriptongroup subscriptongroup) {
        this.subscriptongroup = subscriptongroup;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "GroupId")
    public Subscriptongroup getSubscriptongroup() {
        return subscriptongroup;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the userName
     */
    @Column(name = "UserName")
    public String getUserName() {
        return userName;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the password
     */
    @Column(name = "Password")
    public String getPassword() {
        return password;
    }

    /**
     * @param isSynchronizedtoYSS the isSynchronizedtoYSS to set
     */
    public void setIsSynchronizedtoYSS(Character isSynchronizedtoYSS) {
        this.isSynchronizedtoYSS = isSynchronizedtoYSS;
    }

    /**
     * @return the isSynchronizedtoYSS
     */
    @Column(name = "IsSynchronizedtoYSS")
    public Character getIsSynchronizedtoYSS() {
        return isSynchronizedtoYSS;
    }
}

但是当我执行上面的函数时,我得到了错误的结果.. 在具有上述条件的门户用户表中,只有一行。 但我从 portalUser 得到的结果是 3 个相同记录的列表。

为什么会发生这种情况?也许在 SubsctioptionHasUsers 表中我有 3 条记录。

这个问题的解决方案是什么?

最佳答案

尝试将此添加到您的标准中:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

因为您的 OneToMany 关联设置为 EAGER,Hibernate 也会尝试获取这些关联。

因此,如果您有 1 个父级和 3 个子级,您的 SQL 查询将输出 3 行,并且默认的 Criteria 结果转换器不会按父级对子级进行重新分组,因此您会获得对同一父级的 3 个引用。

关于java - Hibernate 标准给出无效结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23828073/

相关文章:

java - gPRC 截止日期的默认值是多少 (java)

java - Wildfly 8.0.0.Final - 定时器服务集群支持

java - 在 Spring MVC 中显示图像

java - 如何使用jboss在hibernate中配置缓存? ?并在 kumud 控制台中进行测试?

iphone - 多个平台的数据存储。我应该为我们的环境使用 CoreData 吗?

java - Hibernate 单向一对多。我有点困惑

java - 如何让背景框的JtextField成为焦点?

java - 在 Mac OS X 上配置 JNotify

java - Hibernate/JPA 错误 - 无法识别枚举中的某些字符串

hibernate - JPA 与 Hibernate - 合并性能问题