Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 没有可用的事务性EntityManager_Jpa_Spring Transactions_Spring Test - Fatal编程技术网

Jpa 没有可用的事务性EntityManager

Jpa 没有可用的事务性EntityManager,jpa,spring-transactions,spring-test,Jpa,Spring Transactions,Spring Test,我是jpa和spring world的新手,目前正在用一个简单的方法进行一些单元测试,但只有在单元测试模式下运行测试类时,才会收到此错误消息: java.lang.IllegalStateException: No transactional EntityManager available at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(Sh

我是jpa和spring world的新手,目前正在用一个简单的方法进行一些单元测试,但只有在单元测试模式下运行测试类时,才会收到此错误消息:

java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
at $Proxy19.unwrap(Unknown Source)
at com.gemstone.integration.PersonDao.getPersonByUserNamePassword(PersonDao.java:59)
at com.gemstone.integration.PersonDaoTest.getPersonByUserNamePassword_Exist(PersonDaoTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
然而,当我运行我的项目时,代码工作正常,它正确地检索数据(不是在单元测试模式下)

请查看以下我的单元测试方法:

@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
public boolean getPersonByUserNamePassword(String firsName, String password) {

    String hql = "from Person p where p.firstName = :firsName and p.password =:password";
    Session mysession = entityManager.unwrap(Session.class);

    Query query = mysession.createQuery(hql);
    query.setParameter("firsName", firsName);
    query.setParameter("password", password);
    List<Person> results = query.list();

    if (results != null && results.size() > 0) {

        return true;
    }
    return false;

}
您知道为什么我没有收到事务性EntityManager可用错误吗(


提前谢谢。

我认为EntityManager是空的。像这样试试,可能有用吗

private EntityManager entityManager = null;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}
宣布

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
您已完全(尽管无意)禁用了对测试管理事务的支持。原因是您已从声明的侦听器列表中忽略了
TransactionalTestExecutionListener

但是,请注意,
TransactionalTestExecutionListener
在默认情况下是透明声明的。因此,当您从测试类中删除
@TestExecutionListeners
声明时,
TransactionalTestExecutionListener
将再次启用

你当然可以找到细节

问候,


Sam(SpringTestContext框架的作者;)

当我尝试使用IntelliJ进行单元测试时,我遇到了类似的问题,尽管它使用的是
maven surefire插件的
cli

事实证明IntelliJ build使用的是javac编译器,尽管它在pox.xml中指定使用ajc编译器,但在

这使得
@Transactional
无法工作 并为您提供
没有可用的事务性EntityManager


如果是这种情况,您需要将java编译器更改为使用
ajc
,而不是项目结构中的
javac
post-compile-weave模式
,您是否尝试删除
@TestExecutionListeners
?另外,演示如何为响应配置JPA、事务管理器等。正如@axtavt removing TestExecutionListeners前面所述,我的问题已得到解决。但是我不知道为什么。:(你能告诉我们为什么annotation TestExecutionListeners触发了前面提到的错误吗?
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })