javax.persistence.TransactionRequiredException:对Spring Data JPA中的自定义查询执行更新/删除失败

时间:2019-12-17 18:56:26

标签: java spring hibernate spring-boot spring-data-jpa

我正在使用Spring Data JPA执行自定义更新/删除查询。我在DAO中用@Query@Modifying注释了查询,并用@Transactional注释了服务类,但仍然遇到此错误。我正在使用org.springframework.transaction.annotation.Transactional导入语句。

代码段供参考

服务等级

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

    @Service
    @Transactional
    public class HeimdallService {

        @Autowired
        private HeimdallDAO dao;

        @Autowired
        private MultipleDatasourcesRouting router;

        @Transactional
        public String createUser(User user) {
            setRepository(user.getDatabase());
            if(dao.checkIfUserExists(user.getCorpID()) == null){
                dao.createUser(user.getCorpID());
                return MessageFormat.format("User with corpID {0} created successfully",user.getCorpID());
            }
            else return MessageFormat.format("User with corpID {0} already exists",user.getCorpID());
        }

        public boolean checkIfUserExists(String corpID,String database) {
            System.out.println(MessageFormat.format("Setting database as {0} for checking user existence",database));
            setRepository(database);
            if(dao.checkIfUserExists(corpID)!=null) {
                return true;
            }
            else {
                return false;
            }
        }

        public void setRepository(String url){
            dao = router.getJpaFactory(url).getRepository(HeimdallDAO.class);
         }

道课

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import antlr.collections.List;

@Repository
public interface HeimdallDAO extends CrudRepository<User,String>{

    @Transactional
    @Modifying
    @Query(value="create user ?1 identified by qwert123 DEFAULT TABLESPACE ADHOC TEMPORARY TABLESPACE TEMP PROFILE DEFAULT",nativeQuery=true)
    int createUser(String userName);


    @Modifying
    @Transactional
    @Query(value="drop user ?1",nativeQuery=true)
    int dropUser(String userName);

    // do NOT enclose ?1 with single quotes, hibernate will take care of it itself
    @Query(value="select username from all_users where username = ?1", nativeQuery=true)
    List checkIfUserExists(String username);

}

编辑1:当应用程序加载uo时,我在输出中注意到的一件事是

Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

这是否提供有关可能出问题的上下文?

0 个答案:

没有答案