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持久性配置就在這裏,它包括設置休眠特性:
@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來更新,創建和創建,但它沒有任何作用。 我的問題是,我錯過了什麼或者什麼可能導致我的應用程序不會自動生成表的問題?
嘛。該死的。這解決了它。你知道什麼有趣嗎?我記得在StackOverflow上遇到一個線程,OP在這裏犯了同樣的錯誤,解決方案就是這個錯字。我嘲笑它,不理會我會犯同樣的錯誤並轉向下一個線程。非常感謝您爲我發現xD –
不客氣。 :)屬性的JPA版本較長,但可能更難以錯誤輸入:'jpaProperties.put(「javax.persistence.schema-generation.database.action」,「create」);'' – Arthur