在春季启动时,不会在JPA @query中传递参数

时间:2019-03-08 16:11:53

标签: hibernate spring-boot jpa

我正在尝试在Spring Boot中通过JPA备份现有表。 为此,我在下面的查询中将表名date作为参数传递,但是在查询中未设置它。如果表名不能动态修改,那么任何人都可以为此建议替代解决方案。

预期结果: 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+"");
    }

}

1 个答案:

答案 0 :(得分:0)

替代解决方案:

  1. 创建如下的数据库过程。

定界符$$

创建或替换过程INSURANCE_P_CIF_BACKUP()

开始

DECLARE createTable varchar(200);

SET createTable = CONCAT(“ CREATE TABLE INSURANCE_P_CIF”,DATE_FORMAT(SYSDATE(),'%​​d%m%y'),“ AS SELECT * FROM INSURANCE_P_CIF”);

立即执行createTable;

end $$

  1. 以REST API方法调用该过程。
    致电INSURANCE_P_CIF_BACKUP();