6

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?

This is my criteria query:

 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();

And here is my Meaning entity:

@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;
    }
}
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
simpatico
  • 10,709
  • 20
  • 81
  • 126

4 Answers4

3

Looks like you are mixing static metamodel classes with the EntityType class. The EntityType will not have any of your metamodel's attributes on it - you will have to access them using the getSingularAttribute and getCollection methods i.e.:

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

Or you can use a static metamodel directly, but you will have to create the _ classes manually or use a generator as described at http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG%29

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Chris
  • 41
  • 1
  • and : his own example depends on pre-built metamodel classes .... and hes using string-based attribute lookups which make sense whatsoever IF you already have a static metamodel ... im guessing he was quite confused about all of this – specializt Sep 12 '15 at 14:24
3

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();

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>

Some comments:

  • 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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • The wierd thing is that if you use the eclipselink repo link i get a compiler error complaining about the = . I guess that's a netbeans issue. – simpatico Aug 11 '10 at 21:28
  • @simpatico: Can't confirm but your code compiled under Maven with the suggested change. – Pascal Thivent Aug 11 '10 at 21:37
  • When I try mvn install (using the official eclipselink repo) I get: org.apache.maven.reactor.MavenExecutionException: Parse error reading POM. mvn --version Apache Maven 2.2.0 (r788681; 2009-06-26 16:04:01+0300) Java version: 1.6.0_20 Why's not that the case with you too? – simpatico Aug 14 '10 at 11:31
  • @simpatico I've updated the EclipseLink repo URL in the above pom.xml (using `&` instead of `&` now). Maybe that was the problem. By the way, I suggest to use Maven 2.2.1. – Pascal Thivent Aug 14 '10 at 17:05
  • For some reason if I build with dependencies (i.e. mvn install) the parent project before manually building (mvn install) the persistence module the processor will not go, and the build will fail. Any idea why that might be? Any link to what should work so that I test it? – simpatico Oct 24 '10 at 04:39
  • @simpatico This is strange and clearly unexpected. I have a project with a similar structure (i.e. annotation processing in a submodule) and a reactor build just works fine. If you can create a small project allowing to reproduce (and maybe open an issue), I could have a look. – Pascal Thivent Oct 24 '10 at 13:24
  • I've posted the code with which there is an issue here: https://memorizeasy.googlecode.com/svn/branches/processorBug. My guess is that you could delete all modules but persistence and all classes but MeaningsDatabase, Database and Meaning and still have the problem. I tried to verify that without screwing my project scm configuration, and after several hours (and bug reports) I gave up. – simpatico Oct 24 '10 at 21:43
  • 1
    @simpatico: Ok, I reproduced the problem and here is my analysis. EclipseLink's annotation processor uses the following parameter `-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml` to find the `persistence.xml` using a **relative** path. When running a reactor build, this relative path is wrong (it points on `MemoPlatform/src/main/...`) and the `persistence.xml` isn't found. I tried to use an absolute path (using `-Aeclipselink.persistencexml=${basedir}/src/main/resources/META-INF/persistence.xml`) but then the annotation processor complains about a non relative path. – Pascal Thivent Oct 25 '10 at 03:13
  • @simpatico There is definitively something wrong with the way it builds the location of the `persistence.xml`, it should allow to use an absolute path (I don't see how to get things working under Maven without this). The only workaround I can think of would be to put a copy of the persistence.xml in the root module. Ugly :/ – Pascal Thivent Oct 25 '10 at 03:16
  • Thank you for trying and for the analysis. I've submitted a bug here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=328560 with a link to your comments. – simpatico Oct 25 '10 at 04:31
  • @Pascal: how do I specify the processor argument from the command line? I.e. I can do javac -Aeclipselink.persistencexml=.., is that equivalent to mvn install -D-eclipselink.persistencexml=..? – simpatico May 20 '11 at 12:49
1

What IDE are you using?

The Criteria API in JPA 2.0 supports both string based attribute references, and type checked constants that must be compiled through some tool.

You can use the string API without doing anything special, i.e.

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

To use the type checked constants you need to generate these static classes somehow. If you are using the Eclipse IDE, the Eclipse JPA 2.0 support (Dali) can auto generate these classes for you.

EclipseLink also provides a generator that can be used with ant, javac, or integrate with an IDE.

See, http://wiki.eclipse.org/UserGuide/JPA/Using_the_Canonical_Model_Generator_%28ELUG%29

James
  • 17,965
  • 11
  • 91
  • 146
0

If you try this in Eclipse IDE, and in your pom.xml put :

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

for the metalmodel classes, one way to solve this issues is select New/Source Folder and here put the same rute src/main/generated-java, automatically the folder create with the classes generated.

Try to recognize this classes, for me is OK!

Vodo-Siosk Baas
  • 183
  • 3
  • 12
  • 27