无法使用Hibernate在spring-boot应用程序中访问HSQLDB数据库

时间:2014-10-29 12:47:21

标签: spring hibernate spring-mvc hsqldb spring-boot

在我目前的spring-boot应用程序中,我正在尝试使用hibernate来访问HSQLDB数据库。我有这个用于hibernate的配置类:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.spring.app" })
public class HibernateConfig {

   @Bean
   public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

      sessionFactory.setDataSource(restDataSource());
      sessionFactory.setPackagesToScan(new String[] { "com.spring.app.model" });
      sessionFactory.setHibernateProperties(hibernateProperties());

      return sessionFactory;
   }

   @Bean
   public DataSource restDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      String db_url = System.getProperty("user.home")+File.separator+"testedb";

      dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
      dataSource.setUrl("jdbc:hsqldb:file:"+db_url);
      dataSource.setUsername("sa");
      dataSource.setPassword("");

      return dataSource;
   }

   @Bean
   @Autowired
   public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
      HibernateTransactionManager txManager = new HibernateTransactionManager();
      txManager.setSessionFactory(sessionFactory);
      return txManager;
   }

   @Bean
   public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
      return new PersistenceExceptionTranslationPostProcessor();
   }

   Properties hibernateProperties() {
      return new Properties() {
         /**
         * 
         */
        private static final long serialVersionUID = 1L;

        {
            setProperty("hibernate.hbm2ddl.auto", "create");
            setProperty("hibernate.hbm2ddl.import_files", "import.sql");
            setProperty("hibernate.show_sql", "false");
            setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
         }
      };
   }
}

但是当我运行应用程序时,我会在导出数据库方案时在控制台中看到此消息:

2014-10-29 09:38:26.251  INFO 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
2014-10-29 09:38:26.261 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table role_members drop constraint FK_1r16pyyykpv1v6s973ahavdf8
2014-10-29 09:38:26.261 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.ROLE_MEMBERS
2014-10-29 09:38:26.261 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table role_members drop constraint FK_r6a4sbd3nbgk795exs9p5s6sr
2014-10-29 09:38:26.261 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.ROLE_MEMBERS
2014-10-29 09:38:26.262 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table role_permissions drop constraint FK_ipe67mj8dm7kxw2hcdxrixfaa
2014-10-29 09:38:26.262 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.ROLE_PERMISSIONS
2014-10-29 09:38:26.262 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table role_permissions drop constraint FK_cpxi8h0vi43j90938fdt729p7
2014-10-29 09:38:26.263 ERROR 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.ROLE_PERMISSIONS
2014-10-29 09:38:26.270  INFO 16405 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete

我尝试使用saSA作为用户名,但没有成功。另外,我在类路径上有这个import.sql文件:

INSERT INTO role VALUES (1, 'admin');
INSERT INTO permission VALUES (1, 'admin');
INSERT INTO role_permissions VALUES (1,1);
INSERT INTO usuario VALUES (1, 'klebermo', 'pwd', 'Kleber', 'Mota', 'kleber@mail');
INSERT INTO role_members VALUES (1,1);

但数据库中没有插入数据(当我尝试在应用程序中登录时,收到“未找到用户”的消息。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

我同意Bohuslav。如果它有帮助,我只需要在application.properties中添加以下内容来配置对MySQL的访问:

#
## Database configuration
#
spring.datasource.url: jdbc:mysql://localhost:3306/spring
spring.datasource.username: spring
spring.datasource.password: spring
spring.datasource.driverClassName: com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto: update
spring.jpa.database: MYSQL
#spring.jpa.hibernate.naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.show-sql: true