【问题标题】:Parameters are not being passed in the JPA @query in spring boot [duplicate]Spring Boot中的JPA @query中没有传递参数[重复]
【发布时间】: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


    【解决方案1】:

    替代解决方案:

    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;

    结束$$

    1. 在 REST API 方法中调用过程。
      调用 INSURANCE_P_CIF_BACKUP();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-25
      • 2019-10-08
      • 2022-01-12
      • 2016-08-08
      • 2015-05-23
      • 1970-01-01
      • 2019-11-13
      • 2019-06-06
      相关资源
      最近更新 更多