我有一个运行在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