0

I have a class Term.java and it maps to a table in mysql. I want to use metamodel statements in query like the following, but it complains that "Term_" is not an available variable.

public List<Term> findAllOrderedByName() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Term> criteria = cb.createQuery(Term.class);
        Root<Term> term = criteria.from(Term.class);
         criteria.select(term).orderBy(cb.asc(term.get(Term_.name)));
        return em.createQuery(criteria).getResultList();
}

I have this dependency in my POM:

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <scope>provided</scope>
        </dependency>

And the container is JBoss. What's the problem here? Thank you.

Edited:

In Eclipse, click "project->JPA->canonical metamodel (JPA2.0)" and specify the source folder, and I got the class automatically generated in Eclipse. Is this the right way?

@Generated(value="Dali", date="2016-02-29T14:56:11.460-0800")
@StaticMetamodel(Term.class)
public class Term_ {
    public static volatile SingularAttribute<Term, Integer> id;
    public static volatile SingularAttribute<Term, String> name;
    public static volatile ListAttribute<Term, Context> contexts;
}
user697911
  • 10,043
  • 25
  • 95
  • 169
  • Please post your complete pom.xml (probably a good idea to post the calculated, effective pom), as it's not visible what's wrong with your build process. Also, please state whether you are using an IDE like Eclipse or not, because maven-Eclipse integration is a whole new universe of things that can go wrong besides maven itself. – Nándor Előd Fekete Feb 29 '16 at 23:59
  • I right click my project->property->JPA, and specify the source folder src/main/java under "Canonical metamodel(JPA2.0)", then a class "Term_" is automatically generated. Is supposed to be the way to generate motamodel class in Eclipse? Please see updated. – user697911 Mar 01 '16 at 00:05
  • Where are they generated? I guess they end up in a different source folder then your `src/main/java` source folder, something like `${project.build.directory}/generated-sources/annotations`. Is that folder end up being added to your source folders in Eclipse? – Nándor Előd Fekete Mar 01 '16 at 00:09
  • Strange!. I have three entities classes in two packages, and i found one "Member_" is generated in ${project.build.directory}/generated-sources/annotations, but I can't see the other two in generated-sources/annotations. I don't know where are they, including the "Term_". However, all the 3 are found in the same packages as the original classes, "Member", "Context" and "Term". – user697911 Mar 01 '16 at 00:17
  • Try to do a full build (Project -> Clean...) for your project so all compiled classes are deleted and Eclipse will do a full compilation on your sources. You probably had only incremental compilation on the source files you actually changed after enabling annotation processing, so you don't have all the metamodel source files generated yet. – Nándor Előd Fekete Mar 01 '16 at 00:19
  • Strange. Still only one seen. – user697911 Mar 01 '16 at 00:22
  • Are the other two of your entity classes properly annotated as `@Entity`? The jpa metamodel generator will only pick up properly annotated entity classes (`@Entity` or `@MappedSuperclass`). – Nándor Előd Fekete Mar 01 '16 at 00:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104935/discussion-between-user697911-and-nandor-elod-fekete). – user697911 Mar 01 '16 at 00:53

0 Answers0