Hibernate开始改变没有创建的表

时间:2017-02-03 16:13:48

标签: java spring hibernate spring-boot

我正在使用带有Spring Boot和Hibernate 5.0.11 Final的HSQLDB。 当我在嵌入式Tomcat 8.5上运行它时,Hibernate开始改变 未创建的表并给出错误:

  

用户缺少未找到的权限或对象

为什么会这样?

文件 application.properties

spring.datasource.hikari.jdbc-url=jdbc:hsqldb:mem:test
spring.datasource.hikari.username=sa
spring.datasource.hikari.password=
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true

控制台:

INFO  main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.11.Final}
    INFO  main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
    INFO  main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
    INFO  main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    INFO  main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
    INFO  main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
    Hibernate: alter table blog drop constraint FKpxk2jtysqn41oop7lvxcp6dqq
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table blog drop constraint 
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.BLOG
    Hibernate: alter table item drop constraint FK60ndn1v2u4j38nfc5yahfkb7e
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table item drop constraint 
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.ITEM
    Hibernate: alter table user_roles drop constraint FKj9553ass9uctjrmh0gkqsmv0d
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint 
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.USER_ROLES
    Hibernate: alter table user_roles drop constraint FK7ecyobaa59vxkxckg6t355l86
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000389: Unsuccessful: alter table user_roles drop constraint 
    ERROR  main] org.hibernate.tool.hbm2ddl.SchemaExport  : user lacks privilege or object not found: PUBLIC.USER_ROLES


    Hibernate: drop table blog if exists
    Hibernate: drop table item if exists
    Hibernate: drop table role if exists
    Hibernate: drop table user if exists
    Hibernate: drop table user_roles if exists
    Hibernate: create table blog 
    Hibernate: create table item 
    Hibernate: create table role 
    Hibernate: create table user 
    Hibernate: create table user_roles    

ConfigForJPA

@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = {"io.boot.spring.entities"})
@EnableJpaRepositories(basePackages = {"io.boot.spring.repositories"})
@EnableTransactionManagement
public class ConfigForJPA {
    @Bean
    public LocalContainerEntityManagerFactoryBean EntityManagerFactory(
            EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(dataSource())
                .packages("io.boot.spring")
                .persistenceUnit("io.boot.springs.entities")
                .build();
    }

控制台

j.LocalContainerEntityManagerFactoryBean : 
Initialized JPA EntityManagerFactory for persistence unit 'io.boot.springs.entities'

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: -5501, SQLState: 42501

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'initDBService':
Invocation of init method failed;
nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException:
could not prepare statement; SQL [call next value for hibernate_sequence];
nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

@服务 公共类InitDBService {

@Autowired
private RoleRepository roleRepository;

@PostConstruct
public void init(){
    Role roleUser = new Role();
    roleUser.setName("ROLE_USER");
    roleRepository.save(roleUser);

@Entity 公共类角色{

@Id
@GeneratedValue
private Integer id;
private String name;


console:

Hibernate: insert into Role (id, name) values (default, ?)
SQL Error: -5501, SQLState: 42501
user lacks privilege or object not found: ROLE
Error creating bean with name 'initDBService': 
Invocation of init method failed; 
nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException:
could not prepare statement; SQL [insert into Role (id, name)

0 个答案:

没有答案