如何从Java SpringBoot项目中的任何类对数据库执行本机查询



在删除数据库中的记录时,我试图删除一个表,但它给了我以下错误:

Error logging in: Request processing failed; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

我读了几篇文章,甚至在Stack overflow中读了一些问题,这个问题是关于错误的,但没有一个答案有效,我看到的一个可能最有帮助的是添加了注释@Transactional,我把它放在了方法executeDropTable()上,但它给了我同样的错误。,这是我的代码:

package com.ssc.test.cb3.service;
import com.ssc.test.cb3.dto.ReportRequestDTO;
import com.ssc.test.cb3.dto.mapper.ReportRequestMapper;
import com.ssc.test.cb3.repository.ReportRequestRepository;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ssc.test.cb3.model.ReportRequest;
import com.ssc.test.cb3.repository.ReportTableRepository;
import java.util.Map;
import javax.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

/**
* Class to prepare the services to be dispatched to the database upon request.
*
* @author ssc
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class ReportRequestService {
private final ReportRequestRepository reportRequestRepository;
private final EntityManager entityManager;
private final ReportTableRepository reportTableRepository;
private static String SERVER_LOCATION = "D:\JavaProjectsNetBeans\sscb3Test\src\main\resources\";

/**
* Function to delete a report from the database
*
* @param id from the report request objet to identify what is the specific
* report
*/
public void delete(int id) {
ReportRequest reportRequest = reportRequestRepository.findById(id).orElse(null);
ReportTable reportTable = 
String fileName = reportRequest.getFileName();
if (reportRequest == null || reportRequest.getStatus() == 1) {
log.error("It was not possible to delete the selected report as it hasn't been processed yet or it was not found");
} else {
reportRequestRepository.deleteById(id);
log.info("The report request {} was successfully deleted", id);
new File(SERVER_LOCATION + reportRequest.getFileName()).delete(); // Delete file
log.info("The file {} was successfully deleted from the server", fileName);
// DROP created tables with file name without extention
executeDropTable(fileName);
log.info("The table {} was successfully deleted from the data base", fileName);

}
}
/**
* Service to Drop report request tables created on the database when a
* report request is generated and serviced to be downloaded This method
* will be called when a user deletes in the fron-end a report request in
* finished status.
*
* @param tableName will be the name of the table that was created on the
* database
*/
@Transactional
public void executeDropTable(String tableName) {
int substract = 4;
tableName = tableName.substring(0, tableName.length() - substract);
System.out.println("Table name: " + tableName);
String query = "DROP TABLE :tableName"; // IF EXISTS
entityManager.createNativeQuery(query)
.setParameter("tableName", tableName)
.executeUpdate();
}
}

有人能帮我解决这个问题吗?

本机查询的字面意思是";在数据库上执行该SQL语句";,但是您正在尝试使用JPL或其他具有变量扩展的东西。

您的SQL字符串无效,请尝试:

String query = "DROP TABLE " + tablename;
entityManager.executeNativeQuery(query);

相关内容

  • 没有找到相关文章

最新更新