将Query转换为TypeQuery会产生java.lang.AbstractMethodError异常

时间:2016-03-28 08:07:25

标签: java jpa casting persistence

我试图SELECT createQuery这样:

Query query3 = em.createQuery("SELECT t FROM Tag t WHERE t.name IN (:tag1, :tag2)");
query3.setParameter("tag1", "tag1");
query3.setParameter("tag2", "tag2");
Set<Tag> tags = new HashSet<Tag>(query3.getResultList());   

此代码工作正常,但我收到此警告:

  

类型安全:List类型的表达式需要未经检查的转换以符合List

因此,根据this answer,我将QueryTypedQuery一起重复:

TypedQuery<Tag> query2 = em.createQuery("SELECT t FROM Tag t WHERE t.name IN (:tag1, :tag2)", Tag.class);
query2.setParameter("tag1", "tag1");
query2.setParameter("tag2", "tag2");
Set<Tag> tags = new HashSet<Tag>(query2.getResultList());

警告消失了,但是当我运行它时,会产生如下运行时错误:

java.lang.AbstractMethodError: org.apache.openejb.persistence.JtaEntityManager.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery;
at ut.ConfTest.testCreatePost(ConfTest.java:269)
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:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我无法弄清楚我的代码有什么问题。我用TypedQuery搞砸了什么吗?

1 个答案:

答案 0 :(得分:0)

当我们使用缺少新interface方法的interface实现的旧版本时,通常会发生此异常。您可以检查代码中使用的旧版本方法,并使用最新版本更改此方法。

可能是由于某些version兼容性造成的。