Search code examples
javaspring-bootjpaspring-data-jpaentitymanager

JPA Entity : Unable to locate persister


I get this error

Caused by: java.lang.IllegalArgumentException: Unable to locate persister: model.Author

When trying to use my EntityManager inside a Spring Boot application. I have no idea whatsoever why it isnt recognizing it.

The Main Class:

@SpringBootApplication
@ComponentScan(basePackages = {"config"})
@EntityScan(basePackages = {"model"})
public class SpielwieseJpaJdbcApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpielwieseJpaJdbcApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("sks");
        EntityManager em = emf.createEntityManager();
        Author author = em.find(Author.class,1);
    }
}

The entity itself:

@Data
@NoArgsConstructor
@AllArgsConstructor

@Entity
@Table(name = "authors")
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;
}

Finally my persistence.xml file where i define the persistence unit:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2.xsd"
             version="2.2">
    <persistence-unit name="sks" transaction-type="RESOURCE_LOCAL">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sks?serverTimezone=Europe/Vienna" />
            <property name="javax.persistence.jdbc.user" value="sks" />
            <property name="javax.persistence.jdbc.password" value="technikum1" />
            <property name="hibernate.show_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

My project structure is straight forward

main/java// main/java//Author.java

Any ideas ?

At this point im basicly desperate.


Solution

  • I can pretty much confirm that it is a gradle issue. I have tried it several times now, and creating the project through gradle causes this issue.

    Maybe somebody sees something that i need to add to the automated (spring initializer) build.gradle:

    plugins {
        id 'org.springframework.boot' version '2.4.1'
        id 'io.spring.dependency-management' version '1.0.10.RELEASE'
        id 'java'
    }
    
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '15'
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        compileOnly 'org.projectlombok:lombok'
        runtimeOnly 'mysql:mysql-connector-java'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    test {
        useJUnitPlatform()
    }
    

    Otherwise i guess the solution is... use Maven :/