【发布时间】:2019-03-08 16:11:53
【问题描述】:
我正在尝试在 Spring Boot 中通过 JPA 备份现有表。 为此,我在下面的查询中将表名日期作为参数传递,但是,它没有在查询中设置。如果无法动态修改表名,那么任何人都可以为此提出替代解决方案。
预期结果: INSURANCE_P_CIF_08/03/2019 实际结果: INSURANCE_P_CIF_@P0
InsuranceCIFRepository.java
package com.utility.snapp.crud.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.utility.snapp.entities.InsuranceCIF;
@Repository
public interface InsuranceCIFRepository extends JpaRepository<InsuranceCIF, Long>{
@Query(value = "select * into INSURANCE_P_CIF_?1 from INSURANCE_P_CIF where 1=1", nativeQuery = true)
public void takeInsurancePCIFBackup(String currentMonthAndYear);
}
TableBackupService.java
package com.utility.service;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.utility.snapp.crud.repository.InsuranceCIFRepository;
@Component
public class TableBackupService {
@Autowired
InsuranceCIFRepository insuranceCIFRepository;
public void takeBackupOfExistingTables() {
String currentMonthAndYear=getCurrentMonthAndYear();
System.out.println(currentMonthAndYear+insuranceCIFRepository);
try {
insuranceCIFRepository.takeInsurancePCIFBackup(currentMonthAndYear);
}
catch(Exception e) {
}
}
public static String getCurrentMonthAndYear() {
LocalDateTime currentTime=LocalDateTime.now();
String month=currentTime.getMonth().toString();
int year=currentTime.getYear();
return (month+year+"");
}
}
【问题讨论】:
标签: hibernate spring-boot jpa