我在Net-Beans IDE中有一个现有的JavaFX项目,我使用过JDBC。现在我想使用JPA。 Generel CRUD操作正常。但问题在于搜索时间。
我可以通过JPQL和Native Query进行搜索。作为JPA实现我正在使用Hibernate。但问题在于CriteriaBuilder
。我正在关注此链接:
Chapter 9. Criteria Queries。
在本文中,我发现我必须使用Metamodel
。正如文章所述,我已经阅读了这篇文章:Chapter 4. Metamodel
根据这篇文章,我创建了元模型类。
主要类
package com.vocubulary.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Model_E2B_WordList
{
@Id @Getter @Setter
private String e2b_word;
}
元模型类:
package com.vocubulary.model;
import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
@StaticMetamodel(Model_E2B_WordList.class)
public class Model_E2B_WordList_
{
public static volatile SingularAttribute<Model_E2B_WordList, String> e2b_word;
}
DAO课程:
@Test
public void dao_e2b_get_wordsList_afterSerach_clean()
{
String e2b_searchKey = "cat";
EntityManager entityManager_local = EntityManagerUtil.getEntityManager();
List<Model_E2B_WordList> list_e2b_wordList = null;
try
{
entityManager_local.getTransaction().begin();
CriteriaBuilder criteriaBuilder = entityManager_local.getCriteriaBuilder();
CriteriaQuery<Model_E2B_WordList> criteriaQuery = criteriaBuilder.createQuery(Model_E2B_WordList.class);
Root<Model_E2B_WordList> e2b_wordList_Root = criteriaQuery.from(Model_E2B_WordList.class);
criteriaQuery.where(criteriaBuilder.like(criteriaBuilder.lower(e2b_wordList_Root.get(Model_E2B_WordList_.getE2b_word())), "cat".toLowerCase() + "%"));
List<Model_E2B_WordList> e2b_wordList = entityManager_local.createQuery(criteriaQuery).getResultList();
System.out.println(" Worlist Length : "+e2b_wordList.size());
entityManager_local.getTransaction().commit();
}
catch (Exception e)
{
System.out.println("\n\n\n E2B word by Search Key.");
e.printStackTrace();
System.out.println("\n\n\n");
entityManager_local.getTransaction().rollback();
}
}
我的IDE显示此行中的错误,具体在Model_E2B_WordList_
这里:
criteriaQuery.where(criteriaBuilder.like(criteriaBuilder.lower(e2b_wordList_Root.get(Model_E2B_WordList_.getE2b_word())), "cat".toLowerCase() + "%"));
我也注意到有一个包生成了:
现在在我的主package com.vocubulary.model
包中,我还有一个名为Model_E2B_WordList_
的类。如果我正在编译它,则编译错误显示:
error: duplicate class: com.vocubulary.model.Model_E2B_WordList_
public abstract class Model_E2B_WordList_ {
如果我从主package com.vocubulary.model
中删除该类,则会显示以下错误消息:
Note: Hibernate JPA 2 Static-Metamodel Generator 4.3.1.Final
An exception has occurred in the compiler (1.8.0_102). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.IllegalStateException: endPosTable already set
at com.sun.tools.javac.util.DiagnosticSource.setEndPosTable(DiagnosticSource.java:136)
at com.sun.tools.javac.util.Log.setEndPosTable(Log.java:350)
at com.sun.tools.javac.main.JavaCompiler.parse(JavaCompiler.java:667)
at com.sun.tools.javac.main.JavaCompiler.parseFiles(JavaCompiler.java:950)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.<init>(JavacProcessingEnvironment.java:892)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.next(JavacProcessingEnvironment.java:921)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1187)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.main.Main.compile(Main.java:381)
at com.sun.tools.javac.main.Main.compile(Main.java:370)
at com.sun.tools.javac.main.Main.compile(Main.java:361)
at com.sun.tools.javac.Main.compile(Main.java:56)
at sun.reflect.GeneratedMethodAccessor539.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.taskdefs.compilers.Javac13.execute(Javac13.java:57)
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1344)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:1070)
at org.netbeans.modules.java.source.ant.JavacTask.execute(JavacTask.java:145)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.GeneratedMethodAccessor435.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.GeneratedMethodAccessor435.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.GeneratedMethodAccessor435.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
在互联网上搜索后,我发现将hibernate-jpamodelgen-1.3.0.final.jar
添加到类路径中可以解决问题。我添加了但结果是一样的。
我在哪里做错了?