Peter Penzov
Peter Penzov

Reputation: 1754

UnknownEntityTypeException: Unable to locate persister

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?

Upvotes: 11

Views: 35210

Answers (6)

Anshuman Singh
Anshuman Singh

Reputation: 1

Check your method which is doing the persistence operation mostly merge(insert) and confirm you configured it correctly.

you can copy the below code just to check it's working in your

package com.learn_jpa.course.jpa;

import org.springframework.stereotype.Repository;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.transaction.Transactional;

@Repository
@Transactional
public class CourseJpaRepository {

@PersistenceContext
private EntityManager entityManager;

public void insert(Course course) {
    entityManager.merge(course);
}

public void delete(int Id) {
    Course course = entityManager.find(Course.class, Id);
    entityManager.remove(course);
}

public Course findById(long Id) {
    // Result set -> Bean >>RowMapper
    return entityManager.find(Course.class, Id);
}

}

Model class: Course

package com.learn_jpa.course.jpa;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Course {

@Id
private int id;
private String name;
private String author;

public Course() {
    super();
    // TODO Auto-generated constructor stub
}
public Course(int id, String name, String author) {
    super();
    this.id = id;
    this.name = name;
    this.author = author;
}
public int getId() {
    return id;
}
public String getName() {
    return name;
}
public String getAuthor() {
    return author;
}


public void setId(int id) {
    this.id = id;
}
public void setName(String name) {
    this.name = name;
}
public void setAuthor(String author) {
    this.author = author;
}
@Override
public String toString() {
    return "Course [id=" + id + ", name=" + name + ", author=" + author + "]";
}



}

//Query runner class: CourseJpaCommandLineRunner

package com.learn_jpa.course.jpa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CourseJpaCommandLineRunner  implements CommandLineRunner {

@Autowired
CourseJpaRepository courseJpaRepository;

@Override
public void run(String... args) throws Exception {
    // TODO Auto-generated method stub
    courseJpaRepository.insert(new Course(2,"Learn Java jpa", "HighMode"));
    courseJpaRepository.insert(new Course(3,"Learn AWS jpa", "inHighMode"));
    courseJpaRepository.insert(new Course(4,"Learn AZure jpa", "High"));
    courseJpaRepository.insert(new Course(5,"Learn Python jpa", "inHighMode"));
    
    courseJpaRepository.findById(5);
    System.out.println(courseJpaRepository.findById(2));
        System.out.println(courseJpaRepository.findById(4));
    
}

}

Note: you can use h2(in memory database) just to test it.

Upvotes: 0

Akshita Agarwal
Akshita Agarwal

Reputation: 11

I was getting the same error. The mistake I doing was importing javax.persistence.Entity;

We need to import jakarta.persistence.Entity; and not javax.persistence.Entity; This is because JPA has now been changed to Jakarta Persistence API and is not Java Persistence API anymore.

Upvotes: 1

Vader
Vader

Reputation: 179

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);
    }
}

Upvotes: 4

KarolR
KarolR

Reputation: 263

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

Upvotes: 8

Otto Touzil
Otto Touzil

Reputation: 56

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

@EntityScan docs

Upvotes: 1

MatMat
MatMat

Reputation: 908

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

Upvotes: 1

Related Questions