我是冬眠的新手,在春季5时使用它,我有一个配置类,它创建了sessionFactory bean,但是它不起作用(创建),运行项目时出现此错误:
例外是org.springframework.beans.factory.BeanCreationException:错误 创建名称为“ sessionFactory”的bean com.t4b.project.priceBuy.configuration.HibernateConfig: 合并的bean定义的后处理失败; 嵌套异常 是java.lang.IllegalStateException:无法自省类 来自的[org.springframework.orm.hibernate5.LocalSessionFactoryBean] 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 + "]";
}
}
答案 0 :(得分:1)
最终编辑:您需要删除Tarif.class中的构造函数
public Tarif(String code, double tax) {
this.code = code;
this.tax = tax;
}
删除此。因为休眠与POJO一起工作。和POJO没有构造函数。它应该现在可以工作
您需要给模型(实体类)提供休眠路径,就像这样(使用setPackagesToScan
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("models");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
,将休眠配置类更改为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类实际上用于创建Bean,它们是 特定于应用程序的,哪些需要过滤器(如 过滤器不是Servlet的一部分)。 Servlet Config类实际上是 用于创建DispatcherServlet特定的Bean,例如 ViewResolvers,ArgumentResolvers,Interceptor等。