sim*_*ico 6 java eclipselink criteria-api jpa-2.0
java se 6项目是否支持eclipselink jpa2的标准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();
Run Code Online (Sandbox Code Playgroud)
这是我的意义实体:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有检查条件查询本身的正确性,但是,正如 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();
Run Code Online (Sandbox Code Playgroud)
以下是我用来通过 EclipseLink 生成规范元模型类的 Maven 设置:
<project>
...
<repositories>
<!-- Repository for EclipseLink artifacts -->
<repository>
<id>EclipseLink Repo</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&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>
Run Code Online (Sandbox Code Playgroud)
一些评论:
<processor>.-Aeclipselink.persistencexml,注释处理器会抱怨persistence.xml不存在并失败。target我更喜欢在(我想要clean清理它)下生成源代码。通过此配置,可以正确生成并编译静态元模型类。