[英]JPARepository is not saving to DB
說來話長,但本周末我不得不重新設計一個應用程序。 從 spring boot 應用程序到 spring 批處理應用程序。 這個過程始終是一個批處理過程,但我試圖制作這個批處理引擎,但它變得太復雜了,我不得不停止正在做的事情。 我相信我們都去過那里。 無論如何,一切正常!! 除了我試圖保留原始代碼的一段代碼。 我正在嘗試使用 JPARepository 保存方法,但它不起作用!! 我可以調用 save 方法,我覺得 Repo 被實例化了,因為我沒有收到空指針異常。 事實上,我沒有收到任何異常。 我只是在數據庫中沒有看到任何內容。 我知道這段代碼有效,因為我在之前的設計中運行了它。 無論如何,這是我的課程......
數據對象:
@Data
@Entity
@Table(name="PAYEE_QUAL_LS")
public class PayeeList {
@EmbeddedId
private PayeeListPK payeeListPK = new PayeeListPK();
@Column(name = "PAYEE_QUAL_CD")
private String payeeQualCode;
@Column(name = "ETL_TS")
private Timestamp etlTimestamp;
}
主鍵數據類...
@Data
@Embeddable
public class PayeeListPK implements Serializable {
@Column(name = "PAYEE_NM")
private String payeeName;
@Column(name = "BAT_PROC_DT")
private Date batchProcDate;
}
回購類...
@Repository
public interface PayeeListRepo extends JpaRepository<PayeeList,String> {}
我的服務類...
public class OracleService {
private static final Logger logger = LoggerFactory.getLogger(OracleService.class);
@Autowired
PayeeListRepo payeeListRepo;
public void loadToPayeeListTable(PayeeList payeeList) {
payeeListRepo.save(payeeList);
}
我有一個 Tasklet 的實現,我從我的批處理步驟中調用它...
public class PayeeListTableLoad implements Tasklet {
private static final Logger logger = LoggerFactory.getLogger(PayeeListTableLoad.class);
private java.sql.Date procDt;
private String inputFile;
private Timestamp time;
private int safeRecordCount = 0;
private int blockRecordCount = 0;
private int safeRejectRecordCount = 0;
private int blockRejectRecordCount = 0;
private ArrayList<String> rejectRecordList = new ArrayList<>();
@Autowired
OracleService oracleService;
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
java.util.Date parsed = format.parse(System.getenv("procDt"));
procDt = new java.sql.Date(parsed.getTime());
inputFile = Constants.filePath;
time = new Timestamp(System.currentTimeMillis());
logger.info("Running data quality checks on input file and loading to Oracle");
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile))) {
String line = reader.readLine();
while (line != null) {
if (dataQuality(line)) {
PayeeList payeeList = buildPayeeListObject(line);
oracleService.loadToPayeeListTable(payeeList);
logger.info("Record loaded: " + line);
} else {
rejectRecordList.add(line);
try {
if (line.split("\\|")[1].equals("B")) {
blockRejectRecordCount++;
} else if (line.split("\\|")[1].equals("S")) {
safeRejectRecordCount++;
}
logger.info("Record rejected: " + line);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Safe record count is: " + safeRecordCount);
logger.info("Block record count is: " + blockRecordCount);
logger.info("Rejected records are: " + rejectRecordList);
SendEmail sendEmail = new SendEmail();
sendEmail.sendEmail(Constants.aegisCheckInclearingRecipient,Constants.aegisCheckInclearingSender,Constants.payeeListFileSuccessEmailSubject,Constants.payeeListFileSuccessEmailBodyBuilder(safeRecordCount,blockRecordCount,safeRejectRecordCount,blockRejectRecordCount,rejectRecordList));
logger.info("Successfully loaded to Oracle and sent out Email to stakeholders");
return null;
}
在我的批處理配置中....
@Bean
public OracleService oracleService() { return new OracleService(); }
@Bean
public PayeeListTableLoad payeeListTableLoad() {
return new PayeeListTableLoad();
}
@Bean
public Step payeeListLoadStep() {
return stepBuilderFactory.get("payeeListLoadStep")
.tasklet(payeeListTableLoad())
.build();
}
@Bean
public Job loadPositivePayFile(NotificationListener listener, Step positivePayLoadStep) {
return jobBuilderFactory.get("loadPositivePayFile")
.incrementer(new RunIdIncrementer())
.listener(listener)
.start(positivePayDataQualityStep())
.next(initialCleanUpStep())
.next(positivePayLoadStep)
.next(metadataTableLoadStep())
.next(cleanUpGOSStep())
.build();
}
最終,我們的步驟是運行 Tasklet 的實現,我們正在自動裝配 OracleService 類,然后調用該類,然后調用 Repo 方法。 我正在使用 Oracle 服務類方法,我正在調用我的 Autowired Repository 的 save 方法,但同樣什么也沒有發生!!
編輯!!!
我想出了另一種方法,那就是使用 EntityManager 並使用persist 和flush 方法。 下面是我的 Oracle 服務類中的 loadToPayeeListTable 方法...
public void loadToPayeeListTable(PayeeList payeeList) throws ParseException {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(payeeList);
entityManager.flush();
transaction.commit();
entityManager.close();
}
您可以嘗試通過 Spring 測試通過存儲庫嗎? 我從來沒有遇到過這個問題,但我不確定數據庫類型。 是 MySQL 還是 Oracle? 因為我從未將它與@EmbeddedId 一起使用。 如果你通過了單元測試,你應該通過調試檢查你的服務邏輯。 相反,你應該先通過測試。
將您的 jpa 存儲庫更改為
@Repository
public interface PayeeListRepo extends JpaRepository<PayeeList, PayeeListPK>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.