0

I have the following problem when i try to do the findCliente method. I have the class Cliente in the package model and when i run the findCliente controller I get this exception here. I don't know where I'm wrong because I don't have much skill with JPA and Spring Suite. Can someone tell me how to fix this exception?

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Unable to locate persister: it.myshop.orm.model.Cliente
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.IllegalArgumentException: Unable to locate persister: it.myshop.orm.model.Cliente
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3416)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3357)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362)
    com.sun.proxy.$Proxy41.find(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
    com.sun.proxy.$Proxy41.find(Unknown Source)
    it.myshop.orm.impl.ClienteServiceImpl.getById(ClienteServiceImpl.java:23)
    it.myshop.orm.controller.ClienteController.findCliente(ClienteController.java:37)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

org.hibernate.UnknownEntityTypeException: Unable to locate persister: it.myshop.orm.model.Cliente
    org.hibernate.metamodel.internal.MetamodelImpl.locateEntityPersister(MetamodelImpl.java:727)
    org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:3019)
    org.hibernate.internal.SessionImpl.access$1800(SessionImpl.java:201)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2715)
    org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2698)
    org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1180)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3380)
    org.hibernate.internal.SessionImpl.find(SessionImpl.java:3357)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362)
    com.sun.proxy.$Proxy41.find(Unknown Source)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:311)
    com.sun.proxy.$Proxy41.find(Unknown Source)
    it.myshop.orm.impl.ClienteServiceImpl.getById(ClienteServiceImpl.java:23)
    it.myshop.orm.controller.ClienteController.findCliente(ClienteController.java:37)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

AppConfig

package it.myshop.orm.config;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import it.myshop.orm.dao.ClienteService;
import it.myshop.orm.impl.ClienteServiceImpl;

@EnableWebMvc
@Configuration
@ComponentScan("it.myshop.orm.controller")
public class AppConfig {
    
    
    @Bean
    public ViewResolver getViewResolver() {
        
        InternalResourceViewResolver iwv = new InternalResourceViewResolver();
        iwv.setPrefix("/WEB-INF/view/");
        iwv.setSuffix(".jsp");
        iwv.setViewClass(JstlView.class);
        
        return iwv;
    }


//BEAN CONNESSIONE AL DB
@Bean
public DataSource getDbConnection() {
    
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/corso-spring-ud?serverTimezone=CET");
    ds.setUsername("root");
    
    return ds;
}


@Bean
public LocalContainerEntityManagerFactoryBean getEntityManager() {

HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.MYSQL);
adapter.setGenerateDdl(true); 

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(getDbConnection());
factory.setJpaVendorAdapter(adapter); //passo adapter
factory.setPackagesToScan(getClass().getPackage().getName()); 

return factory;

}


@Bean
public ClienteService getClienteServiceImpl() {
    return new ClienteServiceImpl();
}

App Initializer

package it.myshop.orm.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import it.myshop.orm.config.AppConfig;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[] {AppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}

ClienteController

package it.myshop.orm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import it.myshop.orm.dao.ClienteService;
import it.myshop.orm.impl.ClienteServiceImpl;
import it.myshop.orm.model.Cliente;

@Controller
@RequestMapping("/cliente")
public class ClienteController {
    
    
    @Autowired
    private ClienteService cs;

    @ResponseBody
    @GetMapping("/add")
    public String add() {
        
//      cs.add(null);
        System.out.println("Sei nella pagina di aggiunta");
        
        return null;
    }
    
    
    
    @ResponseBody
    @GetMapping("/findCliente")
    public String findCliente() {
        
        Cliente c = cs.getById(1);
        System.out.println("Sei nella pagina di find del Cliente");
        
        return c.getNome() + " " + c.getCognome();
    }

Cliente

package it.myshop.orm.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="cliente")
public class Cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    @Column
    private String nome;
    @Column
    private String cognome;
    @Column
    private String codiceFiscale;
    @Column
    private String email;
    @Column
    private String telefono;
    @Column
    private String username;
    @Column
    private String password;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getCognome() {
        return cognome;
    }
    public void setCognome(String cognome) {
        this.cognome = cognome;
    }
    public String getCodiceFiscale() {
        return codiceFiscale;
    }
    public void setCodiceFiscale(String codiceFiscale) {
        this.codiceFiscale = codiceFiscale;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTelefono() {
        return telefono;
    }
    public void setTelefono(String telefono) {
        this.telefono = telefono;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

Interface ClienteService

package it.myshop.orm.dao;

import it.myshop.orm.model.Cliente;

public interface ClienteService {

    public Cliente add(Cliente c);
    
    public Cliente getById(int id);
}

ClienteServiceImpl

package it.myshop.orm.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import it.myshop.orm.dao.ClienteService;
import it.myshop.orm.model.Cliente;

public class ClienteServiceImpl implements ClienteService {

    
    @PersistenceContext
    public EntityManager em;
    
    @Override
    public Cliente add(Cliente c) {
        em.persist(c);
        return c;
    }

    @Override
    public Cliente getById(int id) {
        return em.find(Cliente.class, id);
    }

}

1 Answer 1

2

In order to solve this problem you have two options. You can choose either of which . 1- you must have all you entity classes declared in you hibernate configuration file. for this you must add you entity classes specially "Cliente" in hibernate.cfg.xml file like this :

<mapping class="entity.Client"/>

or

2-another way is adding this entity to session configuration like this :

SessionFactory factory = new Configuration().configure("hibernate.cfg.xml") .addAnnotatedClass(Cliente.class) .buildSessionFactory();

1
  • Thank you, I solved the error with the second option!
    – Andrew
    Oct 5, 2022 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.