使用Spring JPA存储库的事务两种方法

时间:2014-09-05 08:05:42

标签: java spring hibernate jpa

@Transactional(readOnly=false)
    public Do12 saveReport(Do12 report) {
        try {
// This is where I get a do12 record.
            Do12 findReportByDate = do12Repository.findByDate(report
                    .getBeginPeriod()); 
            if (findReportByDate != null) {
// This is where I delete the do12 record.
                if (deleteReport(findReportByDate) != 0) 
                    throw new Exception("Не удалось удалить запись о ДО12");
            }
            if (report.getCancelByService() != null)
                for (CancelByService cancelByService : report
                        .getCancelByService())
                    cancelByService.setReport(report);
            if (report.getLateByService() != null)
                for (LateByService lateByService : report.getLateByService())
                    lateByService.setReport(report);
// Here I save the new one and get an Exception.
            return do12Repository.save(report); 
        } catch (Exception e) {
            logger.error("", e);
            return null;
        }
    }

    public int deleteReport(Do12 report) {
        try {
            do12Repository.delete(report);
            return 0;
        } catch (Exception e) {
            logger.error("", e);
            return -1;
        }
}

这是我的输出。它说我尝试在数据库中使用相同的beginPeriod保存do12,但它是一个独特的约束。

显然,第一个hibernate消息说他得到了12条记录,而第二条显示他试图保存新报告。

但删除查询在哪里?

  

Hibernate:从reports.do12 do12x0_中选择....   do12x0_.beginPeriod =?

     

Hibernate:插入reports.do12   (beginPeriod,countAreaCancelTrain,countCancelTrain,   countFullCancelTrain,countTrainFollow,countTrainLate,   countTrainOnGraph,countTrainOnSchedule,percentExecGraph,   percentExecWithoutOtherCause,timeLate,typeOfReportPeriod)值(?,   ?,?,?,?,?,?,?,?,?,?,?)

     11:50:49,546警告   http-bio-8080-exec-26 SqlExceptionHelper:logExceptions:144 - SQL   错误:0,SQLState:23505

     

11:50:49,547 ERROR http-bio-8080-exec-26   SqlExceptionHelper:logExceptions:146 - ОШИБКА:повторяющеесязначение   ключанарушаетограничениеуникальности“do12_beginperiod_key”   Подробности:Ключ“(beginperiod)=(2014-09-01)”ужесуществует。

0 个答案:

没有答案