我的功能如下:
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;
}
}
但是当我执行上面的函数时,结果出错了..
在具有上述标准的Portal用户表中,只有一行。
但我得到的结果是来自portalUser
的3个相同记录的列表。
为什么会这样?也许在SubsctioptionHasUsers
表中我有3条记录。
这个问题的解决方案是什么?
答案 0 :(得分:2)
尝试将此添加到您的条件中:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
因为你的OneToMany关联被设置为EAGER,所以Hibernate也会尝试获取它们。
因此,如果您有1个父级有3个子级,则您的SQL查询将输出3行,并且默认的Criteria结果转换器不会按子级重新组合子级,因此您将获得3个对同一父级的引用。