JPA: Error: Attempt to recreate a file for type <MyClass>
Asked Answered
A

9

11

Every time I run my code, I get the following error:

 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.

At first I thought it was because I accidently forgot to add the subclass, but it's still giving me the same error. ClubLes inherits from Les, which is an interface which contains only getters. Do I need to make the interface serializable aswell? I have no idea what's going wrong.

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 interface

public interface Les {

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

PU
I only have one persistence unit, which looks like this

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

I also noticed that when I remove clublesson from JPA, it throws the same error for ClubPersoon, which follows the same scheme

Alton answered 7/5, 2019 at 10:13 Comment(2)
Are you sure you've got only one persistence unit in your persistence.xml?Clinic
clean your build directory #17562326Yang
H
6

I had this error also, and it was because I had the dependency and the metamodel plugin configuration in the pom.xml, so they were being generated twice. Try removing one of them.

My dependency configuration:

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

My plugin configuration:

<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>
Honegger answered 18/1, 2021 at 19:59 Comment(1)
This helped abit, although for my case i had to remove both the dependency and the metamodel plugin and it worked fine.Capitate
C
3

Try adding <exclude-unlisted-classes>true</exclude-unlisted-classes> to the persistence.xml. I think the classes are being processed twice due do this missing statement.

Clinic answered 22/11, 2019 at 12:26 Comment(0)
A
3

Try to set in your web.xml:

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

The error could also come from the metamodel generator.

Solution:

  • Clean your build directory* before you build again, or
  • enable overwriting in the config from your metamodel plugin.

(* with maven this simply means mvn clean install - but only if the generated metamodel files end up in the build folder and not next to your ordinary files)

Explanation: This tool generates metamodel files from your Entites. If you have an entity Car.java it will generate a file called Car_.java (note the underscore). This generated metamodel file contains the fields of the entity for you to reference in the criteria builder, for example.

Annular answered 18/11, 2020 at 16:33 Comment(1)
Cleaning build directory worked for me, using Gradle instead of MavenOctonary
S
1

In my case resetting the SDK for the project in Intellij idea helped only.

Also in other case it helped when /src/generated folder was deleted together with /build (with build artifacts). So for some reason src/generated folder wasn't actually updated or cleaned up on gradle's clean command

Schema answered 3/8, 2022 at 14:12 Comment(0)
T
1

Cleaning my build directory (target folder for maven) helped me.

Torruella answered 16/10, 2022 at 5:53 Comment(0)
S
0

I recently faced the same issue. In my case I was developing a tool(.jar) for executing some imports tasks using CSV as sources and Oracle database as final destiny of data. In others apps that run in the Application Server we use the advantages of it for database interaction.

I use this dependencies:

  • EclipseLink 2.6.2
  • ojdbc8-18.3.0.0

In my case I have two persistence-unit in the persistence.xml file, and in the process could be used dynamically one or another.

First I tried to follow some suggestions to solve this problem like set in the persistence units the property exclude-unlisted-classes as follow:

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

And also I added the entities classes to the first of the persistence units, because one class can be only listed in one of the persistence unit configured. With this the compilation works, but at runtime the utility don't found the entity classes. For the testing using the IDE(not relevant by I used Netbeans) it also works but with the drawback that I had to had some tricky property "jar-file" to the persistence unit also for locating the entity classes.

Finally I decided deal with the problem in other way because the previous solution did not quite solve all the problems.

The following steps works for me. In the persistence.xml file I changed the line as follow:

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

These are my 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>

At the pom.xml file I use the following plugins:

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

With this is generated a fat .jar with all dependencies. As a final annotation I can say that I don't use metamodel classes, instead I used the entity classes themselves, and with this I don't have any problem neither in the compilation fase nor in the runtime fase.

Shriner answered 24/2, 2021 at 16:40 Comment(0)
R
0

I faced problem because same classes created inside target-dev folder of the project. Once I deleted all .java files of mappers then I did "Invalidate and cache / restart" then my server started.

Rafaelrafaela answered 24/4, 2021 at 9:44 Comment(0)
I
0

Came across this today using the Maven Processor Plugin with EclipseLink JPA Model Generator.

Adding the Disable annotation processors during normal compilation-Block as mentioned in Example 1 fixed it for me.

Immoderation answered 15/5, 2024 at 7:1 Comment(0)
P
0

For me in intelliJ, I fixed this error by doing a maven sync (Maven tab -> reload all Maven projects)

Perpendicular answered 2/9, 2024 at 9:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.