AYY
我正在制作一个简单的Spring MVC Web应用程序。我可以运行应用程序就好了,但如果我尝试登录,我会得到以下内容(我修剪了所有我认为不相关的消息):
INFO: HHH000412: Hibernate Core {5.2.6.Final}
org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
INFO: HCANN000001: Hibernate Commons Annotations{5.0.1.Final}
org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
org.springframework.security.authentication.InternalAuthenticationServiceException: PreparedStatementCallback; bad SQL grammar [SELECT username, password, enabled FROM Users WHERE username=?]; nested exception is java.sql.SQLSyntaxErrorException: Table 'ElsLoggerSchema.Users' doesn't exist
我使用spring security来验证用户身份。我希望Hibernate自动生成表,我的架构存在,但它没有表。这是弹簧安全性的配置:
@Configuration
@EnableGlobalMethodSecurity
@EnableWebSecurity
@Import({SpringConfiguration.class})
public class SecurityContext extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
// authorizeRequests() -> use-expresions = "true"
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/createaccount","/error", "/register", "/login", "/newaccount", "/resources/**").permitAll()
.antMatchers("/**", "/*", "/").authenticated()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").defaultSuccessUrl("/dashboard").loginProcessingUrl("/j_spring_security_check")
.usernameParameter("username").passwordParameter("password").failureUrl("/login?error=true")
.and()
.logout().logoutUrl("/logout").logoutSuccessUrl("/login").invalidateHttpSession(true);
}
// Equivalent of jdbc-user-service in XML
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("SELECT username, password, enabled FROM Users WHERE username=?")
.authoritiesByUsernameQuery("SELECT username, authority FROM authorities where username=?");
}
}
我的hibnerate持久性配置就在这里,它包括设置Hibernate属性:
@Configuration
@EnableTransactionManagement
@PropertySource({ "/WEB-INF/persistence-mysql.properties" })
@ComponentScan({ "com.LearnersLogger" })
@Import({SpringConfiguration.class})
public class PersistenceConfig {
@Autowired
private Environment env;
@Autowired
private DataSource dataSource;
@Bean(name="sessionFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan("com.LearnersLogger");
try {
sessionFactory.setHibernateProperties(hibernateProperties());
} catch (Exception e){
System.out.println("Error with instantiating session factory");
throw new RuntimeException(e);
}
return sessionFactory;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory){
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory);
return htm;
}
@Bean
@Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory){
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
return hibernateTemplate;
}
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.hbm2dll.auto", this.env.getProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.dialect", this.env.getProperty("hibernate.dialect"));
properties.put("hibernate.show", this.env.getProperty("hibernate.show_sql"));
return properties;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
}
我的用户模型如下所示:
@Entity
@Table(name = "Users")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 5729727545813781294L;
public User(){
}
// various attributes typical to a User object model
}
我尝试过更改hibernate.hbm2ddl.auto来更新,创建和创建 - 但它没有任何效果。 我的问题是,我错过了什么或可能导致我的应用程序不自动生成表的问题?
答案 0 :(得分:2)
这里有一个拼写错误,prop名称应为hibernate.hbm2d d l.auto:
properties.put("hibernate.hbm2dll.auto", this.env.getProperty("hibernate.hbm2ddl.auto"));
答案 1 :(得分:1)
您可以尝试以下语句:properties.put("hibernate.hbm2ddl.auto", "create");
来解决您的问题