6

eclipselink jpa2の基準APIは、Java se 6プロジェクトでサポートされていますか?そうでなければ、それが私の問題です。永続性.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メソッドを使用getSingularAttributeしてそれらにアクセスする必要があります。getCollection

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を使用していますか?

JPA2.0のCriteriaAPIは、文字列ベースの属性参照と、何らかのツールを使用してコンパイルする必要がある型チェック定数の両方をサポートします。

特別なことを何もせずに文字列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クラスの場合、この問題を解決する1つの方法は、[新規/ソースフォルダー]を選択し、ここに同じルートsrc / main/generated-javaを配置します。フォルダーは自動的に生成されたクラスで作成されます。

このクラスを認識してみてください。私にとっては大丈夫です。

于 2011-04-27T17:55:39.050 に答える