将查询转换为typequery会导致java.lang.abstractmethoderror异常

tzxcd3kk  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(384)

我在试着 SELECTcreateQuery 这样地:

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());

此代码工作正常,但我收到以下警告:
类型安全:类型列表的表达式需要未选中的转换才能符合列表
所以,根据这个答案,我回答 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搞砸了吗?

7xllpg7q

7xllpg7q1#

当我们使用旧版本的 interface 缺少新的 interface 方法。您可以检查代码中使用的旧版本方法,并使用最新版本更改此方法。
可能是因为 version 兼容性。

相关问题