Hibernate 将旧记录状态置为N,然后插入新记录,但是旧记录时间也更新了

我要是实现的功能是:
一个用户会上传照片,并且可以更新;
每次更新,不删除数据库记录,只把表中的 "isAvailable"字段 状态从"Y" 改为"N",不更新时间戳字段 "initTime",然后往数据库中查入一条新纪录。

然后我出现的问题是,每次更新照片的时候,旧记录的时间戳字段 "initTime"也会被修改,而且时间比新纪录要晚20秒左右。

项目中用了spring托管Hibernate事务

application.xml配置如下:
    <!-- 配置session factory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
   <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!--配置拦截器-->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager"></property>
   <property name="transactionAttributes">
       <props>
           <prop key="*">PROPAGATION_REQUIRED</prop>
       </props>
   </property>
</bean>

<!-- 配置bean自动代理 -->
<bean id="autoproxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="proxyTargetClass" value="true"/>
   <property name="interceptorNames">
       <list>
           <value>transactionInterceptor</value>
       </list>
   </property>
   <property name="beanNames">
       <list>
           <value>*Service</value>
       </list>
   </property>
</bean>

service层代码(部分,去掉生成文件名,路径等跟问题无关的代码)
public ServiceResult imageUploadByUserId(FileVO fileVO, UserLoginInfo loginInfo) throws BaseException {

    if (null == fileVO || null == loginInfo) {
        setServiceResult(REP_USER_PARAM_ERROR, MSG_USER_PARAM_EMPTY, null);
        return mServiceResult;
    }
    int userRecordId = loginInfo.getUserRecordId();
    String imageType = fileVO.getFileType();

    //更新原有图片记录
    List<BaseModel> imagesResult = mImageInfoDAO
            .getImageInfoByUserIdAndType(userRecordId, imageType);
    if (null != imagesResult) {
        for (BaseModel item : imagesResult) {
            ((TImageInfo) item).setIsNew(IS_NEW_VALUE_NO);
            mImageInfoDAO.saveOrUpdateImageInfoByInstance(((TImageInfo) item));
        }
    }
            //保存数据库
    TImageInfo imageInfo = new TImageInfo();
    imageInfo.setOwnerRecordId(userRecordId);
    imageInfo.setImagePath(targetFileRelativePath);
    imageInfo.setThumbnailPath(targetThumbnailRelativePath);
    imageInfo.setImageType(imageType);
    imageInfo.setImagePurpose(fileVO.getFilePurpose());
    imageInfo.setContentType(fileVO.getFileContentType());
    imageInfo.setIsNew(IS_NEW_VALUE_YES);
    imageInfo.setInitTime(new Timestamp(timestamp));

    if (mImageInfoDAO.saveOrUpdateImageInfoByInstance(imageInfo)) {
        setServiceResult(REP_SERVER_HANDLE_SUCCESS, null, null);
    } else {
        setServiceResult(REP_SERVER_HANDLE_ERROR, null, null);
    }
    return mServiceResult;
}

    Dao层查询方法
    public List<BaseModel> getImageInfoByUserIdAndType(int userRecordId, String imageType) throws BaseException {
    try {
        List<BaseModel> resultList = getSession().createCriteria(TImageInfo.class)
                .add(Restrictions.eq(OWNER_RECORD_ID_KEY, userRecordId))
                .add(Restrictions.eq(IMAGE_TYPE_KEY, imageType))
                .list();

        if (null == resultList || resultList.isEmpty()) {
            return null;
        }
        return resultList;

    } catch (RuntimeException re) {
        BaseException be = ExceptionFactory.getException(DAOException.class,
                "save or update Image Info RTException", re);
        throw be;
    }

            Dao层更新方法
            public boolean saveOrUpdateImageInfoByInstance(TImageInfo imageInfo) throws BaseException {
    try {
        getSession().saveOrUpdate(imageInfo);
        return true;

    } catch (RuntimeException re) {
        BaseException be = ExceptionFactory.getException(DAOException.class,
                "save or update Image Info RTException", re);
        throw be;
    }
}
查看全部
u010818027
FuBiaoshan
2015/07/31 03:14
  • string
  • 数据库
  • hibernate
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复