Spring Boot Jpa 配置错误
Posted
技术标签:
【中文标题】Spring Boot Jpa 配置错误【英文标题】:Spring Boot Jpa configuration error 【发布时间】:2017-06-24 19:20:12 【问题描述】:使用 Spring boot 1.4.3.and hibernate 5.0.11 对 Spring data jpa 进行以下配置 我得到 SQL 错误: -5501,SQL 状态:42501 用户缺少权限或找不到对象:ROLE 我的配置有什么问题?
pom.xml 提取:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
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
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();
@Bean
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource dataSource()
return (HikariDataSource) DataSourceBuilder.create()
.type(HikariDataSource.class).build();
服务:
@服务 公共类 InitDBService
@Autowired
private RoleRepository roleRepository;
@PostConstruct
public void init()
Role roleUser = new Role();
roleUser.setName("ROLE_USER");
roleRepository.save(roleUser);
控制台:
main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'io.boot.springs.entities'
main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: io.boot.springs.entities
main] org.hibernate.Version : HHH000412: Hibernate Core 5.0.11.Final
main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Started.
main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)
main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'io.boot.springs.entities'
Hibernate: insert into Role (id, name) values (default, ?)
main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: -5501, SQLState: 42501
main] o.h.engine.jdbc.spi.SqlExceptionHelper : user lacks privilege or object not found: ROLE
main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
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 [insert into Role (id, name) values (default, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'io.boot.springs.entities'
角色类:
package io.boot.spring.entities;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Role
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(mappedBy = "roles")
private List<User> users;
getters & setters...
configForJpa2:
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = "io.boot.spring.entities")
@EnableJpaRepositories(basePackages = "io.boot.spring.repositories")
@EnableTransactionManagement
public class ConfigForJPA
@Bean
@ConfigurationProperties("spring.datasource.hikari")
public HikariDataSource dataSource()
return (HikariDataSource) DataSourceBuilder.create()
.type(HikariDataSource.class).build();
控制台2:
main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [ame: default
main] org.hibernate.Version : HHH000412: Hibernate Core 5.0.11.Final
main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Started.
main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections.
main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
ibernate: alter table blog drop constraint FKpxk2jtysqn41oop7lvxcp6dqq
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table blog drop constraint
main] org.hibernate.tool.hbm2ddl.SchemaExport : user lacks privilege or object not found: PUBLIC.BLOG
ibernate: alter table item drop constraint FK60ndn1v2u4j38nfc5yahfkb7e
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table item drop constraint
main] org.hibernate.tool.hbm2ddl.SchemaExport : user lacks privilege or object not found: PUBLIC.ITEM
ibernate: alter table user_roles drop constraint FKj9553ass9uctjrmh0gkqsmv0d
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user_roles drop constraint
main] org.hibernate.tool.hbm2ddl.SchemaExport : user lacks privilege or object not found: PUBLIC.USER_ROLES
ibernate: alter table user_roles drop constraint FK7ecyobaa59vxkxckg6t355l86
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user_roles drop constraint
main] org.hibernate.tool.hbm2ddl.SchemaExport : user lacks privilege or object not found: PUBLIC.USER_ROLES
ibernate: drop table blog if exists
ibernate: drop table item if exists
ibernate: drop table role if exists
ibernate: drop table user if exists
ibernate: drop table user_roles if exists
ibernate: create table blog (id (start with 1), name varchar(255), url varchar(255), user_id integer, primary key (id))
ibernate: create table item (id (start with 1), description varchar(255), link varchar(255), published_date timestamp, title varchar(255), blog_id integer, primary key (id))
ibernate: create table role (id (start with 1), name varchar(255), primary key (id))
ibernate: create table user (id (start with 1), email varchar(255), name varchar(255), password varchar(255), primary key (id))
【问题讨论】:
角色对象是否映射到休眠中?您可以发布 Role 类。 我发布了角色类 Id 说 init 在创建表之前完成。您可以删除 PostConstruct 并运行它,然后尝试从控制台访问 Role 表吗?可以使用资源目录下的insert.sql来设置数据 您需要验证您在 application.properties 中提供的用户是否具有插入数据库的权限。 好的。但是我应该如何验证用户“sa”是否具有必要的权限? 【参考方案1】:这似乎是与 sql 相关的错误,而不是 JPA 错误。一个好的方法是使用与连接字符串相同的凭据登录到您的 rdbms,并检查与您的实体对应的表是否可以使用您当前的用户进行查询。
您可以使用 dbeaver 来执行此操作,因为它支持 hsqldb。
【讨论】:
好的。但是我应该如何验证用户“sa”是否具有必要的权限? 授予“sa”权限并确保发出关闭“SHUTDOWN” username "sa" 是 hsqlDB 中的默认用户,我将其用作“in memory DB”进行测试,它具有所有特权。当我最小化 configForJpa 并让 hibernate 创建它的魔术表时,但我得到另一个错误: Hibernate: alter table user_roles drop constraint 我发布了控制台和 configForJpa2 对我来说,同一组 application.yml 属性非常适合不同的项目。以上是关于Spring Boot Jpa 配置错误的主要内容,如果未能解决你的问题,请参考以下文章
Spring boot、mvc、hibernate 和 mysql 配置 - sessionFactory 错误
spring-boot-starter-data-jpa 依赖错误