更新现有对象无法在Hibernate中运行

时间:2018-11-07 02:41:45

标签: java hibernate hibernate-mapping hibernate-criteria

在下面的代码中,我尝试更新CommunityUserandRoles对象(如果已存在),如果不插入新对象。该代码对于本地环境中的插入和更新都可以正常工作,但是在生产中它不会更新已经存在的对象,而且我也不会遇到任何异常。

请让我知道问题出在哪里。

模型类:

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity
@Table(name = "CommunityUsersAndRoles")
@org.hibernate.annotations.Entity(dynamicUpdate = true, dynamicInsert = true)
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CommunityUserRoles implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Lithium user id.
     */
    @Id
    private long lithiumId;

    /**
     * Lithium user email.
     */
    @Column(unique = true)
    private String email;

    /**
     * Lithium user roles.
     */
    private String roles;

    /**
     * Lithium user lastModifiedDate.
     */
    private Date lastModifiedDate;

    /**
     * 
     */
    public CommunityUserRoles() {
    }

    /**
     * 
     */
    public CommunityUserRoles(long lithiumId, String email, String roles) {
        this.lithiumId = lithiumId;
        this.email = email;
        this.roles = roles;
        this.lastModifiedDate = new Date();
    }

    /**
     * Returns lithiumId.
     * 
     * @return Returns the lithiumId.
     */
    public long getLithiumId() {
        return lithiumId;
    }

    /**
     * Sets lithiumId.
     * 
     * @param lithiumId
     *            The lithiumId to set.
     */
    public void setLithiumId(long lithiumId) {
        this.lithiumId = lithiumId;
    }

    /**
     * Returns email.
     * 
     * @return Returns the email.
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets email.
     * 
     * @param email
     *            The email to set.
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Returns roles.
     * 
     * @return Returns the roles.
     */
    public String getRoles() {
        return roles;
    }

    /**
     * Sets roles.
     * 
     * @param roles
     *            The roles to set.
     */
    public void setRoles(String roles) {
        this.roles = roles;
    }

    /**
     * Returns lastModifiedDate.
     * 
     * @return Returns the lastModifiedDate.
     */
    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    /**
     * Sets lastModifiedDate.
     * 
     * @param lastModifiedDate
     *            The lastModifiedDate to set.
     */
    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "CommunityUserAndRole [lithiumId=" + lithiumId + ", email=" + email + ", roles=" + roles
                + ", lastModifiedDate=" + lastModifiedDate + "]";
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((lastModifiedDate == null) ? 0 : lastModifiedDate.hashCode());
        result = prime * result + (int) (lithiumId ^ (lithiumId >>> 32));
        result = prime * result + ((roles == null) ? 0 : roles.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CommunityUserRoles other = (CommunityUserRoles) obj;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (lastModifiedDate == null) {
            if (other.lastModifiedDate != null)
                return false;
        } else if (!lastModifiedDate.equals(other.lastModifiedDate))
            return false;
        if (lithiumId != other.lithiumId)
            return false;
        if (roles == null) {
            if (other.roles != null)
                return false;
        } else if (!roles.equals(other.roles))
            return false;
        return true;
    }

}

DAO类:

import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.xyz.am.exception.PersistenceLayerException;
import com.xyz.am.persistence.foundation.AbstractGenericHibernateDAO;
import com.xyz.zbc.efg.model.CommunityUserRoles;

public class CommunityUserRolesDAO extends AbstractGenericHibernateDAO<CommunityUserRoles>
        implements ICommunityUserRolesDAO {

    @Override
    public boolean isLithiumIdExist(long lithiumId) throws PersistenceLayerException {
        final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(getPersistentClass())
                .add(Restrictions.eq("lithiumId", lithiumId));
        if (findByCriteria(detachedCriteria).size() == 1) {
            return true;
        }
        return false;
    }


    @Override
    public CommunityUserRoles getByEmail(String email) throws PersistenceLayerException {
        final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(getPersistentClass())
                .add(Restrictions.eq("email", email));
        if (findByCriteria(detachedCriteria).size() == 1) {
            return findByCriteria(detachedCriteria).get(0);
        }
        return null;
    }


    @Override
    public void insert(final CommunityUserRoles communityUserRoles) throws PersistenceLayerException {
        super.saveOrUpdate(communityUserRoles);
    }

}

服务类方法:

CommunityUserRoles communityUserRoles = null;
                          if(communityUserRolesDAO.isLithiumIdExist(lithiumUserId)) {
                              communityUserRolesDAO.getByEmail(email)
                              communityUserRoles.setRoles(roles);
                              communityUserRoles.setLastModifiedDate(new Date());


                          }else {
                              communityUserRoles = new CommunityUserRoles();
                              communityUserRoles.setLithiumId(lithiumUserId);
                              communityUserRoles.setEmail(lithiumUsers.get(lithiumUserId).getEmail());
                              communityUserRoles.setRoles(roles);
                              communityUserRoles.setLastModifiedDate(new Date());
                              log.info("New User ID "+lithiumUserId+" Email "+lithiumUsers.get(lithiumUserId).getEmail()+" Roles "+roles+" Date "+new Date());
                          }
                          communityUserRolesDAO.insert(communityUserRoles);

0 个答案:

没有答案