我有一個運行在Tomcat和Mysql上的Spring和Hibernate的webapp。 我用我的電腦上的代碼:基於Windows 7的環境:webapp工作正常。將項目從Windows導出到Debian時發生的錯誤
然後我試圖將它導出到遠程的debian服務器上,所以我安裝了tomcat和mysql,然後從我的Windows mysql數據庫中導入了一個Dump。
即使我可以訪問我的數據庫MySQL控制檯,我的web應用程序不能給我這個錯誤:
20:35:03,020 DEBUG LogicalConnectionImpl:226 - Obtaining JDBC connection
20:35:03,021 DEBUG PoolableConnectionFactory:292 - Failed to validate a poolable connection
java.sql.SQLException: isValid() returned false
at org.apache.commons.dbcp2.PoolableConnection.validate(PoolableConnection.java:228)
at org.apache.commons.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:303)
at org.apache.commons.dbcp2.PoolableConnectionFactory.validateObject(PoolableConnectionFactory.java:288)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:488)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:119)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
然後我搜索,發現一些有關「hibernate.hbm2ddl.auto」所以我添加到我的conf: prop.put(「hibernate.hbm2ddl.auto」,「驗證」);
現在:
20:53:24,701 ERROR ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateMenuDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.meltdown.menu.infra.HibernateMenuDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class com.meltdown.config.ViewConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.hibernate.SessionFactory com.meltdown.config.ViewConfig.sessionFactory()] threw exception; nested exception is org.hibernate.HibernateException: Missing table: BARS
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
因此,看起來,我的應用程序不能訪問我的數據庫,但我不明白爲什麼...
這裏是我的conf文件: AppSecurityConfig @Configuration @EnableWebSecurity 公共類AppSecurityConfig擴展了WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("userDetailsService")
UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/bo/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/bo/bars**").access("hasRole('ROLE_SUPERADMIN')")
.and().formLogin();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
}
MVCInitializer 公共類MVCInitializer擴展AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ViewConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
ViewConfig
@EnableWebMvc
@Configuration
@ComponentScan({ "com.meltdown.*" })
@EnableTransactionManagement
@Import({ AppSecurityConfig.class })
public class ViewConfig extends WebMvcConfigurerAdapter {
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.meltdown").addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties prop = new Properties();
prop.put("hibernate.format_sql", "true");
prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
prop.put("hibernate.hbm2ddl.auto", "validate");
return prop;
}
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/meltdown2");
ds.setUsername("root");
ds.setPassword("myXP4NYYKF8");
return ds;
}
//Create a transaction manager
@Bean
public HibernateTransactionManager txManager() {
return new HibernateTransactionManager(sessionFactory());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/bo_theme/**");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(1000000);
return resolver;
}
}
你是對的!非常感謝! 我的實體中的@Table註釋中使用了小寫字母。 然後我刪除prop.put(「hibernate.hbm2ddl.auto」,「validate」);因爲他搞亂了列的類型(int而不是bigint等),它的工作原理! – Labe