5

I want to use JPA with Spring on Wildfly. I tried this configuration:

application.properties:

spring.jmx.enabled=false
spring.datasource.jndi-name=java:/global/production
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.hibernate.ddl-auto = create-drop

POM file:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>   
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>                
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>       
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

Configuration:

@Configuration
@ComponentScan("org.rest.api.server.*")
public class AppConfig {

    @Bean
    public EntityManager entityManager(EntityManagerFactory emf) {
        return emf.createEntityManager();
    }
}

But when I try to perform query I get:

Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: org.rest.api.server.repository.Terminals
10:28:27,539 ERROR [stderr] (default task-1)    at org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:642)

What is the proper way to configure Entity? probably I need to map it manually?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

4 Answers4

4

This error is thrown When entities aren't being picked up, and database tables aren't being created or mapped by spring boot auto configuration. In order to solve this problem you need to add @EntityScan(basePackages = {"**entities_package_name"}) below @SpringBootApplication in Spring Boot application class.

package com.bill.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan( basePackages = {"com.bill.entity"} ) // entities package name
public class BillWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(BillWebApplication.class, args);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vader
  • 159
  • 7
3

This error may occur when your persistence.xml file is missing some entities, that you trying to use.

KarolR
  • 213
  • 1
  • 10
1

This is how I would configure an entityManager. The dataSource you pass is another bean in the configuration. You could have a look at BasicDataSource.

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource);
    em.setJpaDialect(new HibernateJpaDialect());
    em.setPackagesToScan("org.rest.api.server.folder");
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); // JPA implementation
    em.setJpaVendorAdapter(vendorAdapter);
    return em;
}
MatMat
  • 878
  • 3
  • 8
  • 24
1

You can use the @EntityScan() annotation to let spring find the Entity-classes. Usage is similar to @ComponentScan

@EntityScan docs