Search code examples
javahibernateormcriteriahibernate-criteria

Hibernate Criteria giving invalid result


I have a function like :

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

and my PortalUsers.java class like this:

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

But when I execute the above function, I get wrong result.. In Portal users table with the above criteria only one row is there. But I am getting result as list of 3 same records from portalUser.

Why this is happening? Maybe as In SubsctioptionHasUsers table I am having 3 records.

What is the solution for this problem?


Solution

  • Try adding this to your criteria:

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    

    Because your OneToMany associations are set to EAGER, Hibernate will try to fetch those as well.

    So if you have 1 parent with 3 children, your SQL query will output 3 rows and the default Criteria result transformer doesn't regroup the children by their parents, hence you get 3 references to the same parent.