Marcos
Marcos

Reputation: 1265

JPA entity classes are not discovered automatically with Gradle

I'm new to Gradle. I'm trying to run some tests but they fail with messages showing that the entity classes are not mapped. When I explicitly map the classes in persistence.xml everything works fine. I did some research and found that adding theses lines to the build script would solve the problem:

sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
sourceSets.test.output.resourcesDir = sourceSets.test.output.classesDir

but it simply didn't work for me. I still get the errors if I don't specify the classes in the deployment descriptor.

What could be wrong?

UPDATE

My project structure:

My project structure

My persistence.xml:

<?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="bd">
        <class>br.desenvolvimento.teste.modelo.Cliente</class>
        <class>br.desenvolvimento.teste.modelo.Banco</class>
        <class>br.desenvolvimento.teste.modelo.AgenciaBancaria</class>
        <class>br.desenvolvimento.teste.modelo.ContaBancaria</class>
        <class>br.desenvolvimento.teste.modelo.Filho</class>
        <class>br.desenvolvimento.teste.modelo.Projeto</class>
        <class>br.desenvolvimento.teste.modelo.Telefone</class>

        <properties>
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

Upvotes: 1

Views: 782

Answers (2)

tkhm
tkhm

Reputation: 880

Which Gradle version do you use? If it is 5.x, you need to update your lines like the following:

sourceSets.main.output.resourcesDir = sourceSets.main.java.outputDir
sourceSets.test.output.resourcesDir = sourceSets.test.java.outputDir

Your sourceSets.main.output.classesDir is deprecated on Gradle 4.x and withdrawed on Gradle 5.x.

In my case, a portion of build.gradle is the following:

sourceSets {
  java {
    main {
      output.resourcesDir = java.outputDir
    }
  }
}

See: SourceSetOutput - Gradle DSL Version 5.5 https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSetOutput.html

Upvotes: 1

nemzsom
nemzsom

Reputation: 94

I had the same problem and adding the

sourceSets.test.output.resourcesDir = sourceSets.test.output.classesDir

line fixed the problem for me (only this line needed). So thank you for your tip :)

I have a same project structure as yours so I don't know why this not works for you.

Upvotes: 0

Related Questions