I'm struggling to get hibernate working with Spring Boot. I'm getting the following exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' defined in com.xxx.MegLabApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.hibernate5.HibernateTransactionManager]: Factory method 'txManager' threw exception; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
The only thing I can find similar to this is the lack of c3p0 connection pooling, but I have the dependencies for c3p0 in my gradle build file and the properties are passed into hibernates configuration.
Can anyone advise on how I can fix this configuration?
Thanks
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());
}
}