Java JPA:错误:尝试为类型<;重新创建文件;MyClass>;

Java JPA:错误:尝试为类型<;重新创建文件;MyClass>;,java,jpa,compiler-errors,Java,Jpa,Compiler Errors,每次运行代码时,都会出现以下错误: 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(CanonicalM

每次运行代码时,都会出现以下错误:

 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() {
    }
Les接口

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>

org.eclipse.persistence.jpa.PersistenceProvider
多明,格拉德
多梅因位置
圆顶木棍
多梅因·克鲁巴蒂维特
多梅因·克拉布尔斯

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

尝试将
true
添加到
persistence.xml
。我认为由于缺少此语句,类被处理了两次。

请尝试在web.xml中设置:

true

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

解决方案:

  • 再次生成之前,请清理生成目录*或
  • 从元模型插件在配置中启用覆盖
(*对于maven,这仅仅意味着
mvn clean install
——但仅当生成的元模型文件最终位于构建文件夹中而不是普通文件旁边时)

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

我也有此错误,这是因为我在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>

org.hibernate
冬眠
假如
我的插件配置:

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

org.bsc.maven
maven处理器插件
4.5
过程
过程
生成源
目标/元模型
org.codehaus.mojo
构建助手maven插件
添加源
生成源
添加源
目标/元模型

我最近也遇到了同样的问题。 在我的例子中,我正在开发一个工具(.jar),用于执行一些导入任务,使用CSV作为源,使用Oracle数据库作为最终的数据源。在应用服务器上运行的其他应用程序中,我们将其优势用于数据库交互

我使用以下依赖项:

  • 日食2.6.2
  • ojdbc8-18.3.0.0
在我的例子中,persistence.xml文件中有两个持久性单元,在这个过程中可以动态地使用其中一个

首先,我尝试按照一些建议来解决这个问题,比如在持久性单元中设置属性exclude unlisted class,如下所示:

<exclude-unlisted-classes>true</exclude-unlisted-classes>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
true
我还将entities类添加到第一个持久性单元中,因为一个类只能在配置的持久性单元中列出。 这样编译就可以工作了,但在运行时实用程序找不到实体类。对于使用IDE的测试(与我使用的Netbeans无关),它也可以工作,但缺点是我必须对持久化单元使用一些棘手的属性“jar文件”,以定位实体类

最后我决定用另一种方式来处理这个问题,因为之前的解决方案并没有完全解决所有的问题

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

<exclude-unlisted-classes>true</exclude-unlisted-classes>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
false
这些是我的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>

org.eclipse.persistence.jpa.PersistenceProvider
jdbc/source1
假的
没有一个
org.eclipse.persistence.jpa.PersistenceProvider
jdbc/source2
假的
没有一个
在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>

org.apache.maven.plugins
maven编译器插件
3.1
${maven.compiler.source}
${maven.compiler.target}
org.apache.maven.plugins
maven汇编插件
2.6
带有依赖项的jar
真的
${mainClass}
组装
包裹
单一的
由此生成一个包含所有依赖项的fat.jar。 作为最后一个注释,我可以说我没有使用元模型类,而是使用了实体类本身,因此无论是在编译阶段还是在运行时阶段,我都没有任何问题。

I fa