休眠不自动生成表
Posted
技术标签:
【中文标题】休眠不自动生成表【英文标题】:Hibernate not auto generating tables 【发布时间】:2017-02-25 16:04:02 【问题描述】:年年
我正在制作一个简单的 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 Annotations5.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 自动生成表,我的模式存在但它没有表。这是spring security的配置:
@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=?");
我的 hibernate 持久化配置就在这里,它包括设置 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 以更新、创建和创建删除,但没有效果。 我的问题是,我遗漏了什么或者可能导致我的应用程序没有自动生成表的问题?
【问题讨论】:
【参考方案1】:这里有错别字,prop name应该是hibernate.hbm2ddl.auto:
properties.put("hibernate.hbm2dll.auto", this.env.getProperty("hibernate.hbm2ddl.auto"));
【讨论】:
好吧。该死。那解决了它。你知道什么好笑吗?我记得在 *** 上遇到了一个线程,其中 OP 犯了同样的错误,而解决方案正是这个错字。我对此嗤之以鼻,不考虑我会犯同样错误的任何机会,然后继续下一个线程。非常感谢您为我发现它xD 不客气。 :) 该属性的 JPA 版本更长,但可能更难打错:jpaProperties.put("javax.persistence.schema-generation.database.action", "create");
【参考方案2】:
你可以试试这个说法:properties.put("hibernate.hbm2ddl.auto", "create");
来解决你的问题
【讨论】:
以上是关于休眠不自动生成表的主要内容,如果未能解决你的问题,请参考以下文章