为什么编译器无法识别元模型属性? [英] Why the compiler doesn't recognize the metamodel attributes?

查看:110
本文介绍了为什么编译器无法识别元模型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is the criteria api of eclipselink jpa2 supported for java se 6 projects? If not, that's my problem. Do I need to specify anything special to the criteria api in the persistence.xml?

这是我的条件查询:

 final EntityType<Meaning> Meaning_ = em.getMetamodel().entity(Meaning.class);
    final CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Integer> cq = cb.createQuery(Integer.class);
    final Root<Meaning> meng = cq.from(Meaning.class);
    cq.where(meng.get(Meaning_.lastPublishedDate)); //this attributes are not recognized/found
    cq.select(meng.get(Meaning_.objId));            // "                "                   
    TypedQuery<Integer> 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混合在一起不会暴露您正在寻找的东西。假设您的元模型类已生成,请删除第一行并导入生成的含义_

About your code

I didn't check the correctness of the criteria query itself but, as Chris mentioned, you are mixing static metamodel classes with the EntityType that doesn't expose what you're looking for. Assuming your metamodel classes have been generated, remove the first line and import the generated Meaning_:

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



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



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

About generation of the static (canonical) metamodel classes

Here is the Maven setup I'm using to generate the canonical metamodel classes with EclipseLink:

<project>
  ...
  <repositories>
    <!-- Repository for EclipseLink artifacts -->
    <repository>
      <id>EclipseLink Repo</id>
      <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo/</url>
    </repository>    
    ...
  </repositories>
  ...
  <pluginRepositories>
    <!-- For the annotation processor plugin -->
    <pluginRepository>
      <id>maven-annotation-plugin</id>
      <url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url>
    </pluginRepository>
  </pluginRepositories>
  ...
  <dependencies>
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>eclipselink</artifactId>
      <version>2.1.0</version>
    </dependency>
    <!-- optional - only needed if you are using JPA outside of a Java EE container-->
    <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>javax.persistence</artifactId>
      <version>2.0.2</version>
    </dependency>
  <dependencies>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>1.3.1</version>
        <executions>
          <execution>
            <id>process</id>
            <goals>
              <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <!-- Without this, the annotation processor complains about persistence.xml not being present and fail -->
              <compilerArguments>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</compilerArguments>
              <!-- For an unknown reason, the annotation processor is not discovered, have to list it explicitly -->
              <processors>
                <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
              </processors>
              <!-- source output directory -->
              <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <inherited>true</inherited>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <compilerArgument>-proc:none</compilerArgument>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</project>

一些评论:


  • EclipseLink注释处理器由主工件提供,没有额外的依赖关系来添加。

  • 由于未知原因,未发现注释处理器,我必须将其明确列为< processor>

  • 没有 -Aeclipselink.persistencexml ,注释处理器会抱怨 persistence.xml 不存在且失败。

  • 我更喜欢在目标下生成源代码(我想要清理来清理它。)。

  • EclipseLink annotation processor is provided by the main artifact, there is no extra dependency to add.
  • For an unknown reason, the annotation processor is not discovered, I have to list it explicitly as a <processor>.
  • Without the -Aeclipselink.persistencexml, the annotation processor complains about the persistence.xml not being present and fail.
  • I prefer to generate source code under target (I want a clean to clean it).

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

With this configuration, the static metamodel classes get generated and compiled appropriately.

这篇关于为什么编译器无法识别元模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆