没有发现与hibernate.hbm2ddl.auto创建表 [英] Table not found with hibernate.hbm2ddl.auto create
问题描述
我正在试验Spring MVC和Spring Security,并遇到以下问题:我使用H2并设置了hibernate.hbm2ddl.auto来创建,但是当我尝试添加用户记录时,我得到 org.h2.jdbc.JdbcSQLException:找不到表USER
。
这是我的数据库配置:
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
公共类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);
返回数据源;
$ b $Be
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());
返回factoryBean;
$ b @Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
返回transactionManager;
私有属性additionalProperties(){
属性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);
返回属性;
}
}
这是模型: p>
@Entity
public class User实现UserDetails {
@Id
private Long ID;
私人字符串用户名;
私人字符串密码;
@ElementCollection
私人列表< Role> authority = new ArrayList< Role>();
私有布尔accountNonExpired;
私人布尔accountNonLocked;
私有布尔凭证NonExpired;
私有布尔启用;
public User(){
this.accountNonExpired = true;
this.accountNonLocked = true;
this.credentialsNonExpired = true;
this.enabled = true;
}
// ...
}
这是安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig扩展了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,boolean,password varchar(255),username varchar(255),primary key(id))
Hibernate:create table User_authorities(User_id bigint not null,authority binary(255))
Hibernate:alter table User_authorities添加约束FK_6yei1bmvdwkqgfn4hw53bvqus外键(User_id)引用用户
[2014-12-04 03:20:32,045]神器网页:战争爆炸:神器部署成功
但是当调用 init()
方法时,我得到这个:
org.h2.jdbc.JdbcSQLException:未找到表格USER; SQL语句:
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)
$ b
datasource.setUrl(> ) jdbc:h2:mem:web; INIT = CREATE SCHEMA IF NOT EXISTS< yourschema>);
请参阅 http://www.h2database.com/html/features.html 以获取更多信息。
I'm experimenting with Spring MVC and Spring Security, and encountered the following issue: I'm using H2 and have set hibernate.hbm2ddl.auto to create, but when I try to add a user record I get org.h2.jdbc.JdbcSQLException: Table "USER" not found
.
This is my database configuration:
@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;
}
}
This is the model:
@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;
}
// ...
}
This is the security configuration:
@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();
}
}
And this is how I try to add a user for testing purposes:
@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'}";
}
Apparently the tables are created:
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
But when calling the init()
method I get this:
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)
Try setting up your Url to.
datasource.setUrl("jdbc:h2:mem:web;INIT=CREATE SCHEMA IF NOT EXISTS<yourschema>");
See http://www.h2database.com/html/features.html for more info.
这篇关于没有发现与hibernate.hbm2ddl.auto创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!