【问题标题】:Table not found with hibernate.hbm2ddl.auto create使用 hibernate.hbm2ddl.auto create 找不到表
【发布时间】:2015-02-02 11:46:43
【问题描述】:

我正在尝试使用 Spring MVC 和 Spring Security,并遇到以下问题:我正在使用 H2 并已将 hibernate.hbm2ddl.auto 设置为创建,但是当我尝试添加用户记录时,我得到 org.h2.jdbc.JdbcSQLException: Table "USER" not found .

这是我的数据库配置:

@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class DatabaseConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setDriverClassName("org.h2.Driver");
        datasource.setUsername("sa");
        datasource.setPassword("");
        datasource.setUrl("jdbc:h2:mem:web");
        return datasource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
        LocalContainerEntityManagerFactoryBean factoryBean
                = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setPackagesToScan("mypackage.model");
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setJpaProperties(this.additionalProperties());
        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    private Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "false");
        properties.setProperty("hibernate.hbm2ddl.auto", "create");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        return properties;
    }

}

这是模型:

@Entity
public class User implements UserDetails {

    @Id
    private Long id;
    private String username;
    private String password;
    @ElementCollection
    private List<Role> authorities = new ArrayList<Role>();
    private Boolean accountNonExpired;
    private Boolean accountNonLocked;
    private Boolean credentialsNonExpired;
    private Boolean enabled;

    public User() {
        this.accountNonExpired = true;
        this.accountNonLocked = true;
        this.credentialsNonExpired = true;
        this.enabled = true;
    }

    // ...

}

这是安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/api/**")
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .httpBasic();
    }

}

这就是我尝试添加用户以进行测试的方式:

@Autowired
private CustomUserDetailsService customUserDetailsService;

@RequestMapping("/init")
@ResponseBody
public String init() {

    User user = new User();
    user.setId(0L);
    user.setUsername("user");
    user.setPassword("password");
    customUserDetailsService.save(user);

    return "{name:'init'}";
}

显然这些表已创建:

Hibernate: drop table User if exists
Hibernate: drop table User_authorities if exists
Hibernate: create table User (id bigint not null, accountNonExpired boolean, accountNonLocked boolean, credentialsNonExpired boolean, enabled boolean, password varchar(255), username varchar(255), primary key (id))
Hibernate: create table User_authorities (User_id bigint not null, authorities binary(255))
Hibernate: alter table User_authorities add constraint FK_6yei1bmvdwkqgfn4hw53bvqus foreign key (User_id) references User
[2014-12-04 03:20:32,045] Artifact web:war exploded: Artifact is deployed successfully

但是当调用init() 方法时,我得到了这个:

org.h2.jdbc.JdbcSQLException: Table "USER" not found; SQL statement:
select user0_.id as id1_0_0_, user0_.accountNonExpired as accountN2_0_0_, user0_.accountNonLocked as accountN3_0_0_, user0_.credentialsNonExpired as credenti4_0_0_, user0_.enabled as enabled5_0_0_, user0_.password as password6_0_0_, user0_.username as username7_0_0_ from User user0_ where user0_.id=? [42102-181]
    org.h2.message.DbException.getJdbcSQLException(DbException.java:345)

【问题讨论】:

    标签: java spring hibernate spring-security spring-data


    【解决方案1】:

    尝试将您的网址设置为。

    datasource.setUrl("jdbc:h2:mem:web;INIT=CREATE SCHEMA IF NOT EXISTS<yourschema>");
    

    请参阅http://www.h2database.com/html/features.html 了解更多信息。

    【讨论】:

    • 这并没有立即帮助,但我还没有研究文档。现在我已经切换到 HSQL,但是当我再次查看 H2 时我会回复。
    猜你喜欢
    • 2016-09-08
    • 2019-03-20
    • 2018-07-06
    • 2020-04-12
    • 2022-01-14
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多