通过hibernate工具生成元模型但我无法使用它

时间:2015-01-06 17:27:29

标签: java hibernate maven jpa

之前我遇到过重复的问题

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile     (default-compile) on project WebAppTest: Compilation failure: Compilation failure:
[ERROR] \Users\Luppo\workWS\WebAppTest\target\generated-   sources\annotations\com\pl\war\ldap\config\model\AbstractEntity_.java:[9,16] error: duplicate class: c  om.pl.war.ldap.config.model.AbstractEntity_

我找到了solution

在生成mvn compile命令元模型类之后,但我无法使用它

我需要通过登录找到用户

public List<User> findByLogin(String login) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> criteriaQuery = builder.createQuery(User.class);
    Root<User> root = criteriaQuery.from(User.class);
    Predicate predicate = builder.conjunction();
    criteriaQuery.select(root).distinct(true);
    if (login != null) {
        predicate = builder.and(predicate,
                builder.equal(root.get("login"), login));
    }
    criteriaQuery.where(predicate);
    return entityManager.createQuery(criteriaQuery).getResultList();
}

当我用&#34;登录&#34; everythiong还可以,但如果我想使用User_.login。没有导入,没有提示,eclipse看不到任何课程。元模型位置

Project
 |_target
   |_generated-sources
     |_apt
      |_(and whole corectly package e.g com.pl.xxx)

有人知道为什么我不能使用元模型类?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我必须添加

<plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>2.1.0</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
                        </processors>
                        <outputDirectory>target/generated-sources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-jpamodelgen</artifactId>
                    <!--version>1.2.0.Final</version -->
                    <version>4.3.8.Final</version>
                </dependency>
            </dependencies>
        </plugin>

<resources>
        <resource>
            <directory>${basedir}/src/main/resources/</directory>
        </resource>
    </resources>

现在元模型正在生成但总是我不能在eclips中使用,但如果我添加我的目录

Properties -> Java build Path -> Link Source (target-> generate-sources)

一切正常