How does Hibernate Metamodel Generator work?

梦想与她 提交于 2021-02-08 10:06:10

问题


Recently just making a move from Hibernate Session api to JPA2. I am assuming it should be some setup problem. But basically my eclipse does not recognize Metamodel attribute.

For example: builder.like( root.get(Book_.name) , search) Book_ can not be resolved as variable.

Now I have followed this http://hibernate.org/orm/tooling/ and added

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jpamodelgen</artifactId>
  <version>5.2.6.Final</version>
</dependency>

As it stated here https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html

In most cases the annotation processor will automatically run provided the processor jar is added to the build classpath

I have checked my build path it included all maven dependency including jpamodelgen, but my eclipse still say Book_ can not be resolved.

What am I doing wrong here?

This is my Dao...

CriteriaBuilder builder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Book> criteria = builder.createQuery(Book.class);

    Root<Book> root = criteria.from(Book.class);

    criteria.select(root);

    if( search != null && !search.isEmpty() ){

        criteria.where(builder.add(
                builder.like( root.get(Book_.name) , search),  // Book_ cannot be resolved a variable
                builder.like(root.get(Book_.ispn), search)    //  ""
                )
        );

    }

    List<Book> books = entityManager.createQuery(criteria).getResultList();

    return books;

My dependency

   <properties>
    <spring-version>4.3.5.RELEASE</spring-version>
    <hibernate-version>5.2.6.Final</hibernate-version> 
   </properties>  
   <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-orm-modules</artifactId>
        <version>${hibernate-version}</version>
        <type>pom</type>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-jpamodelgen</artifactId>
      <version>${hibernate-version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.4.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

Thank you


回答1:


if your target doesn't contains the generated classed you need to configure maven processor like in the documentation that you provided.

If your target contains the generated classes, then all you have to to do is to add them your classpath : if you are using eclipse:

  1. Right click on your project
  2. Properties
  3. Java build path
  4. Add folder
  5. choose the folder under target/generated-sources that contains the generated MetaModel



回答2:


Rafik solution does work but I just wanted to add another method that also worked for me is to enable annotation processing in your eclipse project. The doc from hibernate https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html wasn't really clear to me as it doesn't have step by step setup. So here is how I did it.

This is project specific.

  1. Click on project.
  2. Right click and chose properties
  3. Java Compiler> Annotation Processing :: enable this
  4. Java Compiler> Annotation Processing> FactoryPath select add External JARS
  5. Add hibernate-jpamodelgen-x.x.x.Final.jar
  6. Click okey / clean project

This should work as well.

Hope this helps others

Update I stumbled upon this site that has pictures might be easier to follow. https://docs.jboss.org/hibernate/orm/5.0/topical/html/metamodelgen/MetamodelGenerator.html



来源:https://stackoverflow.com/questions/44772701/how-does-hibernate-metamodel-generator-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!