【问题标题】:JPA: Error: Attempt to recreate a file for type <MyClass>JPA:错误:尝试为 <MyClass> 类型重新创建文件
【发布时间】:2019-05-07 10:13:36
【问题描述】:

每次我运行我的代码时,我都会收到以下错误:

 java.lang.RuntimeException: javax.annotation.processing.FilerException: Attempt to recreate a file for type domein.ClubLes_
    [javac]     at org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor.process(CanonicalModelProcessor.java:407)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:964)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:881)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.access$2100(JavacProcessingEnvironment.java:110)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1202)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1311)
    [javac]     at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1250)
    [javac]     at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:928)
    [javac]     at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:306)
    [javac]     at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:165)
    [javac]     at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:57)
    [javac]     at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:43)
    [javac] Caused by: javax.annotation.processing.FilerException: Attempt to recreate a file for type domein.ClubLes_
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacFiler.checkNameAndExistence(JavacFiler.java:727)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacFiler.createSourceOrClassFile(JavacFiler.java:489)
    [javac]     at jdk.compiler/com.sun.tools.javac.processing.JavacFiler.createSourceFile(JavacFiler.java:426)
    [javac]     at org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor.generateCanonicalModelClass(CanonicalModelProcessor.java:98)
    [javac]     at org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor.generateCanonicalModelClasses(CanonicalModelProcessor.java:226)
    [javac]     at org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor.process(CanonicalModelProcessor.java:403)
    [javac]     ... 11 more

BUILD FAILED
E:\ProjectJava\java-g11\Taijitan\nbproject\build-impl.xml:1134: The following error occurred while executing this line:
E:\ProjectJava\java-g11\Taijitan\nbproject\build-impl.xml:381: Compile failed; see the compiler error output for details.

起初我以为是因为我不小心忘记添加子类,但它仍然给我同样的错误。 ClubLes 继承自 Les,这是一个只包含 getter 的接口。我还需要使接口可序列化吗?我不知道出了什么问题。

ClubLes

@Entity
public class ClubLes implements Serializable, Les {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "LesId")
    private int id;
    @Column(name = "Naam")
    String naam;
    @Column(name = "Soort")
    String soort;
    @ManyToOne
    @JoinColumn(name = "GraadNumeriek")
    Graad graad;
    @Column(name = "FotoUrl")
    private String fotoUrl;
    @Column(name = "Beschrijving")
    private String beschrijving;
    @Column(name = "VideoUrl")
    private String videoUrl;

    public ClubLes() {
    }

界面

public interface Les {

    int getId();
    String getNaam();
    String getSoort();
    //More getters
}

PU
我只有一个持久化单元,看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="TaijitanPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>domein.Graad</class>
    <class>domein.Locatie</class>
    <class>domein.ClubPersoon</class>
    <class>domein.ClubActiviteit</class>
    <class>domein.ClubLes</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=Taijitan"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.password" value="Wachtwoord2019"/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

我还注意到,当我从 JPA 中删除 clublesson 时,它会为 ClubPerson 引发相同的错误,它遵循相同的方案

【问题讨论】:

  • 您确定您的persistence.xml 中只有一个持久性单元吗?

标签: java jpa compiler-errors


【解决方案1】:

我也有这个错误,这是因为我在 pom.xml 中有依赖项和元模型插件配置,所以它们被生成了两次。尝试删除其中一个。

我的依赖配置:

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

我的插件配置:

<plugin>
    <groupId>org.bsc.maven</groupId>            
    <artifactId>maven-processor-plugin</artifactId>
    <version>4.5</version>
    <executions>            
        <execution>         
          <id>process</id>          
              <goals>           
                <goal>process</goal>            
              </goals>          
              <phase>generate-sources</phase>           
              <configuration>           
                <!-- source output directory -->            
                <outputDirectory>target/metamodel</outputDirectory>         
              </configuration>          
            </execution>            
        </executions>           
</plugin>           
<plugin>            
    <groupId>org.codehaus.mojo</groupId>            
    <artifactId>build-helper-maven-plugin</artifactId>  
    <executions>            
      <execution>           
        <id>add-source</id>         
        <phase>generate-sources</phase>         
        <goals>         
          <goal>add-source</goal>           
        </goals>            
        <configuration>         
          <sources>         
            <source>target/metamodel</source>           
          </sources>            
        </configuration>
    </execution>            
  </executions>         
</plugin>

【讨论】:

    【解决方案2】:

    尝试在你的 web.xml 中设置:

    &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;

    错误也可能来自元模型生成器。

    解决方案:

    • 在再次构建之前清理您的构建目录*,或者
    • 在您的元模型插件的配置中启用覆盖。

    (* 对于 maven,这仅表示 mvn clean install - 但前提是生成的元模型文件最终位于 build 文件夹中,而不是在您的普通文件旁边)

    解释: 此工具从您的实体生成元模型文件。如果您有一个实体Car.java,它将生成一个名为Car_.java 的文件(注意下划线)。例如,此生成的元模型文件包含供您在标准构建器中引用的实体字段。

    【讨论】:

    • 清理构建目录对我有用,使用 Gradle 而不是 Maven
    【解决方案3】:

    尝试将&lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt; 添加到persistence.xml。我认为由于缺少这条语句,这些类被处理了两次。

    【讨论】:

      【解决方案4】:

      我最近遇到了同样的问题。 就我而言,我正在开发一个工具(.jar),用于使用 CSV 作为源和 Oracle 数据库作为数据的最终命运来执行一些导入任务。在 Application Server 中运行的其他应用程序中,我们利用它的优势进行数据库交互。

      我使用这个依赖:

      • EclipseLink 2.6.2
      • ojdbc8-18.3.0.0

      在我的例子中,我在 persistence.xml 文件中有两个持久化单元,在这个过程中可以动态地使用一个或另一个。

      首先我尝试遵循一些建议来解决这个问题,例如在持久性单元中设置属性 exclude-unlisted-classes,如下所示:

      <exclude-unlisted-classes>true</exclude-unlisted-classes>
      

      我还将实体类添加到第一个持久性单元中,因为一个类只能在配置的一个持久性单元中列出。 有了这个编译工作,但在运行时实用程序没有找到实体类。对于使用 IDE 进行的测试(与我使用 Netbeans 无关)它也可以工作,但缺点是我必须在持久性单元中使用一些棘手的属性“jar-file”来定位实体类。

      最后我决定用其他方式处理这个问题,因为之前的解决方案并不能完全解决所有问题。

      以下步骤对我有用。 在 persistence.xml 文件中,我将行更改如下:

      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      

      这些是我的 PU:

      <persistence-unit name="source1" transaction-type="RESOURCE_LOCAL">
          <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
          <jta-data-source>jdbc/source1</jta-data-source>
          <exclude-unlisted-classes>false</exclude-unlisted-classes>
          <shared-cache-mode>NONE</shared-cache-mode>
          <properties/>
      </persistence-unit>
      <persistence-unit name="source2" transaction-type="RESOURCE_LOCAL">
          <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
          <jta-data-source>jdbc/source2</jta-data-source>
          <exclude-unlisted-classes>false</exclude-unlisted-classes>
          <shared-cache-mode>NONE</shared-cache-mode>
          <properties/>
      </persistence-unit>
      

      在 pom.xml 文件中,我使用以下插件:

      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
              <configuration>
                  <source>${maven.compiler.source}</source>
                  <target>${maven.compiler.target}</target>
                  <compilerArguments>
                      <proc:none/>
                  </compilerArguments>
              </configuration>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.6</version>
              <configuration>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                  <archive>
                      <manifest>
                          <addClasspath>true</addClasspath>
                          <mainClass>${mainClass}</mainClass>
                      </manifest>
                  </archive>
              </configuration>
              <executions>
                  <execution>
                      <id>make-assembly</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
      

      这样会生成一个包含所有依赖项的胖 .jar。 作为最后的注释,我可以说我不使用元模型类,而是使用实体类本身,这样我在编译和运行时都没有任何问题。

      【讨论】:

        【解决方案5】:

        我遇到了问题,因为在项目的 target-dev 文件夹中创建了相同的类。一旦我删除了映射器的所有 .java 文件,然后我执行了“无效并缓存/重新启动”,然后我的服务器启动了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          • 1970-01-01
          • 2022-06-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多