为什么编译器无法识别元模型属性?

java se 6项目是否支持eclipselink jpa2的标准api? 如果没有,那就是我的问题。 我是否需要在persistence.xml中为条件api指定任何特殊内容?

这是我的标准查询:

final EntityType Meaning_ = em.getMetamodel().entity(Meaning.class); final CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(Integer.class); final Root meng = cq.from(Meaning.class); cq.where(meng.get(Meaning_.lastPublishedDate)); //this attributes are not recognized/found cq.select(meng.get(Meaning_.objId)); // " " TypedQuery q = em.createQuery(cq); return q.getResultList(); 

这是我的意义实体:

 @Entity public class Meaning implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int objId; public int getObjId() { return objId; } @Temporal(javax.persistence.TemporalType.DATE) private Date lastPublishedDate = null;//never public Date getLastPublishedDate(){ return lastPublishedDate; } } 

关于你的代码

我没有检查条件查询本身的正确性,但正如Chris所提到的,您将静态元模型类与不暴露您正在寻找的内容的EntityType混合在一起。 假设您已生成元模型类,请删除第一行并导入生成的Meaning_

 // final EntityType Meaning_ = em.getMetamodel().entity(Meaning.class); final CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(Integer.class); final Root meng = cq.from(Meaning.class); cq.where(meng.get(Meaning_.lastPublishedDate)); // add the appropriate import cq.select(meng.get(Meaning_.objId)); TypedQuery q = em.createQuery(cq); return q.getResultList(); 

关于静态(规范)元模型类的生成

这是我用来生成带有EclipseLink的规范元模型类的Maven设置:

  ...    EclipseLink Repo http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo/  ...  ...    maven-annotation-plugin http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo   ...   org.eclipse.persistence eclipselink 2.1.0    org.eclipse.persistence javax.persistence 2.0.2   ...    org.bsc.maven maven-processor-plugin 1.3.1   process  process  generate-sources   -Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml   org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor   ${project.build.directory}/generated-sources/meta-model      org.apache.maven.plugins maven-compiler-plugin true  1.6 1.6 -proc:none   ...    

一些评论:

  • EclipseLink注释处理器是由主工件提供的,没有额外的依赖关系来添加。
  • 由于未知原因,未发现注释处理器,我必须将其明确列为
  • 如果没有-Aeclipselink.persistencexml ,注释处理器会抱怨persistence.xml不存在并失败。
  • 我更喜欢在target下生成源代码(我想要一个clean的清理它)。

使用此配置,可以适当地生成和编译静态元模型类。

你在用什么IDE?

JPA 2.0中的Criteria API支持基于字符串的属性引用,以及必须通过某些工具编译的类型检查常量。

你可以使用字符串API而不做任何特殊的事情,即

 cq.where(meng.get("lastPublishedDate")); cq.select(meng.get("objId")); 

要使用类型检查的常量,您需要以某种方式生成这些静态类。 如果您使用的是Eclipse IDE,则Eclipse JPA 2.0支持(Dali)可以自动为您生成这些类。

EclipseLink还提供了一个可以与ant,javac一起使用或与IDE集成的生成器。

请参阅http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG%29

看起来您正在将静态元模型类与EntityType类混合使用。 EntityType没有任何元模型的属性 – 您必须使用getSingularAttributegetCollection方法访问它们,即:

 meng.get(Meaning_.getSingularAttribute("someString", String.class)) 

或者您可以直接使用静态元模型,但您必须手动创建_类或使用生成器,如http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG%29所述

如果您在Eclipse IDE中尝试此操作,并在您的pom.xml中放置:

src/main/generated-java

对于metalmodel类,解决这个问题的一种方法是选择New / Source Folder并在这里放置相同的rute src / main / generated-java,自动创建带有生成的类的文件夹。

试着认识这个课程,对我来说还可以!