FuBiaoshan 2015-07-31 03:14 采纳率: 100%
浏览 1506
已采纳

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;
    }
}
  • 写回答

1条回答 默认 最新

  • devmiao 2015-07-31 14:54
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)