我正在努力让hibernate使用Spring Boot。我得到以下例外:
org.springframework.beans.factory.BeanCreationException:创建名称为' txManager'的bean时出错在com.xxx.MegLabApplication中定义:通过工厂方法的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.orm.hibernate5.HibernateTransactionManager]:工厂方法' txManager'抛出异常;嵌套异常是org.hibernate.service.UnknownUnwrapTypeException:无法解包到请求的类型[javax.sql.DataSource]
我唯一能找到类似的东西就是缺少c3p0连接池,但是我的gradle构建文件中有c3p0的依赖项,属性被传递给hibernates配置。
有人可以就如何修复此配置提出建议吗?
由于
import java.util.Properties;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@SpringBootApplication
public class MegLabApplication
{
public static void main(String[] args)
{
SpringApplication.run(MegLabApplication.class, args);
}
@Value("${spring.datasource.driverClassName}")
private String databaseDriverClassName;
@Value("${spring.datasource.url}")
private String datasourceUrl;
@Value("${spring.datasource.username}")
private String databaseUsername;
@Value("${spring.datasource.password}")
private String databasePassword;
@Bean
public SessionFactory sessionFactory()
{
Configuration configuration = new Configuration();
configuration.setProperties(getHibernateProperties());
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.xxx.models");
return builder.buildSessionFactory(serviceRegistry);
}
private Properties getHibernateProperties()
{
Properties prop = new Properties();
prop.put("hibernate.connection.pool_size", "10");
prop.put("hibernate.c3p0.min_size", "5");
prop.put("hibernate.c3p0.max_size", "20");
prop.put("hibernate.c3p0.timeout", "300");
prop.put("hibernate.c3p0.max_statements", "50");
prop.put("hibernate.c3p0.idle_test_period", "60");
prop.put("hibernate.format_sql", "true");
prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
prop.put("hibernate.hbm2ddl.auto", "update");
return prop;
}
@Bean(name = "dataSource")
public DataSource dataSource()
{
DataSource ds = new DataSource();
ds.setDriverClassName(databaseDriverClassName);
ds.setUrl(datasourceUrl);
ds.setUsername(databaseUsername);
ds.setPassword(databasePassword);
return ds;
}
@Bean
public HibernateTransactionManager txManager()
{
return new HibernateTransactionManager(sessionFactory());
}
}
答案 0 :(得分:2)
好的,所以我终于把这一切都搞定了......
Spring启动使用hibernate 4.3.11。我原始配置中的HibernateTransactionManager类使用的是Hibernate5,因此我将类版本更改为4.我的C3P0依赖项也是版本5,因此我将其降级为4。
我还必须将连接细节添加到hibernate属性,并通过指定类明确告诉它使用C3P0连接提供程序。
prop.put("hibernate.connection.provider_class", "org.hibernate.c3p0.internal.C3P0ConnectionProvider")
希望这可以帮助任何人稍后遇到这个问题,直到Spring启动支持Hibernate 5。