有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java hibernate不创建sessionFactory bean

我是hibernate新手,在spring 5中使用它,我有一个配置类,创建sessionFactory bean,但它不工作(创建),我在运行我的项目时遇到以下错误:

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) 个答案

  1. # 1 楼答案

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

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

    删除此项。因为hibernate与POJO一起工作。和POJO没有构造函数。现在应该可以了

    您需要为hibernate提供模型(实体类)路径,如下所示(使用setPackagesToScan

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

    并将hibernate配置类更改为gerRootConfigClasses()方法。因为我也遇到过这样的问题,在我把它放到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