No transactional EntityManager available

前端 未结 3 1875
轮回少年
轮回少年 2021-01-13 06:52

I am new to the jpa and spring world and I am currently doing some unit test on a simple method but keep getting this error message only when I run my test class in unit tes

相关标签:
3条回答
  • 2021-01-13 07:40

    I got a similar problem when I try the unit tests with IntelliJ, even though its working using cli of maven-surefire-plugin

    it turns out that IntelliJ build was using javac compiler and even though it specified in pox.xml to use ajc compiler there is a reported bug for IntelliJ at IDEA-135483

    that made @Transactional not working and give you No transactional EntityManager available

    if that is your case, you need to change java compiler to use ajc instead of javac and post-compile weave mode in project structure

    0 讨论(0)
  • 2021-01-13 07:49

    I think EntityManager is null.Try Like this,may be it work

    private EntityManager entityManager = null;
    
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    0 讨论(0)
  • 2021-01-13 07:51

    By declaring

    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
    

    you have completely (albeit unintentionally) disabled support for test-managed transactions. The reason is that you have omitted TransactionalTestExecutionListener from the list of declared listeners.

    Note, however, that TransactionalTestExecutionListener is declared transparently by default. Thus, when you delete your @TestExecutionListeners declaration from your test class, TransactionalTestExecutionListener is once again enabled.

    You can of course find details in the Spring Reference Manual here.

    Regards,

    Sam (author of the Spring TestContext Framework ;) )

    0 讨论(0)
提交回复
热议问题