Spring4和Hibernate的新手,我正在尝试集成两个framework.Codes:
@Configuration
@PropertySource("classpath:META-INF/persistence_sqlite.properties")
public class DataConfig {
@Autowired
Environment environment;
@Bean
public DriverManagerDataSource dataSource(){
DriverManagerDataSource driverManagerDataSource=new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
driverManagerDataSource.setUrl(environment.getProperty("jdbc.url"));
return driverManagerDataSource;
}
@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean localSessionFactoryBean=new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource());
localSessionFactoryBean.setPackagesToScan("spittr.model");
localSessionFactoryBean.setHibernateProperties(hibernatePropertities());
return localSessionFactoryBean;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager transactionManager=new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor(){
return new PersistenceExceptionTranslationPostProcessor();
}
Properties hibernatePropertities(){
Properties properties=new Properties();
properties.setProperty("hibernate.hbm2ddl.auto",environment.getProperty("hibernate.hbm2ddl.auto"));
properties.setProperty("hibernate.dialect", environment.getProperty("hibernate.dialect"));
properties.setProperty("hibernate.globally_quoted_identifiers","true");
properties.setProperty("hibernate.show_sql",environment.getProperty("hibernate.show_sql"));
return properties;
}
}
persistence_sqlite.properties
(没有拼写错误,因为我可以在IDE中找到相关的类)
jdbc.driverClassName=org.sqlite.JDBC
jdbc.url=jdbc:sqlite:///home/stack/Documents/code/java/spittr/db/spittr.sqlite
hibernate.dialect=org.hibernate.dialect.SQLiteDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
Junit4:
@ContextConfiguration(classes = {DataConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class DataTest {
@Autowired
DataSource source;
@Autowired
Environment environment;
@Test
public void dataSourceTest(){
Assert.assertNotNull(source);
}
}
根异常:
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.SQLiteDialect] as strategy [org.hibernate.dialect.Dialect]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:126)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:194)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:152)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:139)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:133)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
... 73 more
我不知道出了什么问题。我们将不胜感激。
答案 0 :(得分:0)
我更改了sqlite方言类,现在它可以正常工作。