1
votes

I'm new to hibernate and using it with spring 5, I've a configuration class which creates sessionFactory bean but it does not work(create), I get this error when running my project :

exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.t4b.project.priceBuy.configuration.HibernateConfig: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] from ClassLoader [ParallelWebappClassLoader: priceBuy

// configuration class

    @Configuration
    public class HibernateConfig {


        @Bean
        public LocalSessionFactoryBean sessionFactory() {

        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();

        sessionFactoryBean.setHibernateProperties(properties());
        sessionFactoryBean.setAnnotatedClasses(Tarif.class);

        return sessionFactoryBean;

        }

        @Bean
        public Properties properties() {

            Properties properties = new Properties();

            properties.setProperty(AvailableSettings.URL, "jdbc:mysql://localhost:3306/SPRING-LEARN");
            properties.setProperty(AvailableSettings.USER, "root");
            properties.setProperty(AvailableSettings.PASS, "carrow");
            properties.setProperty(AvailableSettings.DIALECT, MySQL5Dialect.class.getName());
            properties.setProperty(AvailableSettings.SHOW_SQL, String.valueOf(true));
            properties.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");


            return properties;
        }

    }


    public class PriceBuyWebApplicationInitializer 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[] {PriceBuyWebApplicationConfiguration.class, ConverterConfig.class, HibernateConfig.class};
        }

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


    }


    // controller


    @RestController
    @RequestMapping("/tarif")
    public class TarifController {

        @Autowired
        TarifDao TarifDao;

        @RequestMapping(method =  RequestMethod.GET)
        public String saveTarif(Model model) {
            Tarif tarif = new Tarif("CK09", 1234);

            TarifDao.insertTarif(tarif);

            return "tarif";
        }
    }

    // tarif class
package com.t4b.project.priceBuy.entities;

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

@Entity
@Table(name = "tarif")
public class Tarif {

    @Id
    @Column(name ="code")
    private String code;

    @Column(name ="tax")
    private double tax;

    public Tarif(String code, double tax) {
        this.code = code;
        this.tax  = tax; 
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public double getTax() {
        return tax;
    }

    public void setTax(double tax) {
        this.tax = tax;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        long temp;
        temp = Double.doubleToLongBits(tax);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Tarif other = (Tarif) obj;
        if (code == null) {
            if (other.code != null)
                return false;
        } else if (!code.equals(other.code))
            return false;
        if (Double.doubleToLongBits(tax) != Double.doubleToLongBits(other.tax))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Tarif [code=" + code + ", tax=" + tax + "]";
    }






}
1
can you share the exact version of hibernate and spring along with stack trace?atti
I'm using spring 5.0.2.RELEASE and hibernate 5carrow

1 Answers

1
votes

Final edit: Yout need to delete constructor in the Tarif.class

  public Tarif(String code, double tax) {
        this.code = code;
        this.tax  = tax; 
    }

Delete this. because hibernate work with POJO. and POJO do not have constructors. it should work now

You need to give the models(entity class) path to hibernate, like that (with setPackagesToScan

  @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("models");
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

and Change hibernate config class to gerRootConfigClasses() method. because I also faced this kind of issue and it solved after I put it into rootConfig.

public class PriceBuyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

        @Override
        protected Class<?>[] getRootConfigClasses() {
             return new Class[]{HibernateConfig.class};
        }

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

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


    }

Root Config Classes are actually used to Create Beans which are Application Specific and which needs to be available for Filters (As Filters are not part of Servlet). Servlet Config Classes are actually used to Create Beans which are DispatcherServlet specific such as ViewResolvers, ArgumentResolvers, Interceptor, etc.

here is the photo link