6

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

这是我的标准查询:

 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;
    }
}
4

4 回答 4

3

关于您的代码

我没有检查条件查询本身的正确性,但是正如 Chris 所提到的,您将静态元模型类与EntityType不公开您正在寻找的内容混合在一起。假设您的元模型类已经生成,删除第一行并导入生成的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 设置:

<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不存在并失败。
  • target我更喜欢在(我想clean清理它)下生成源代码。

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

于 2010-08-11T20:09:22.237 回答
2

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

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

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

于 2010-08-11T13:53:16.097 回答
1

你用的是什么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

于 2010-08-11T13:57:20.923 回答
0

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

<outputDirectory> src/main/generated-java </outputDirectory>

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

试着认出这个类,对我来说还可以!

于 2011-04-27T17:55:39.050 回答