当前位置: 首页 > 知识库问答 >
问题:

Spring BootJpa配置错误

樊烨烨
2023-03-14

使用Spring boot 1.4.3.和hibernate 5.0.11对Spring data jpa进行以下配置,我得到SQL错误:-5501,SQLState:42501用户缺乏特权或找不到对象:角色
我的配置有什么问题?

<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>     

应用程序.属性:

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();
}

}

@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...
@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))

共有1个答案

卫阳炎
2023-03-14

这似乎是一个与sql相关的错误,而不是一个JPA错误。一个好的方法是使用与连接字符串相同的凭据登录到rdbms,并检查与您的实体相对应的表是否可以与您的当前用户一起查询。

您可以使用dbeaver来执行此操作,因为它支持HSQLDB。

 类似资料:
  • 版本介绍 百度移动统计提供两种版本的Crash错误统计功能:标准版、详细Crash版。 标准版:采集的Crash日志内容较少,包含关键的LastBacktrace等信息。 详细Crash版:采集的Crash日志内容丰富,接近iOS系统完整的Crash格式,包含LastBacktrace、thread等信息。 由于加入详细的Crash统计功能会使SDK的体积增大,所以请结合自身业务场景,合理选择SD

  • 百度移动统计提供了错误统计功能。包括错误报告,错误路径等。 由于加入详细的错误统计上报功能,会导致SDK的整体大小变大。故我们针对那些只需要基本的错误次数统计功能的用户提供了标准的java错误统计功能,集成在了应用分析(无埋点)和应用分析(手动埋点)SDK中;针对需要native crash错误信息的用户,提供了附加的Crash版本。

  • 我正在尝试启动我的hadoop应用程序,但是在启动时我在日志文件中看到了这一点,有没有人知道问题是什么? 正在为HDFS创建文件系统://10.170.4.141:9000 java.io.ioException:config()在org.apache.hadoop.conf.configuration(configuration.java:229)在org.apache.hadoop.conf.

  • 我有一个JSON: 我创建了以下pojo 在我的控制器中,我有一个@PostMapping方法,我尝试检索DocumentToSignRestRequest: 并使用模型映射器将其传递给DocumentDto类:DocumentDto DocumentDto=ModelMapper。地图(documentToSignRestRequest,DocumentDto.class); 这个类的不同属性与

  • 我在Visual Studio 2015中使用.NET Framework 4.6创建了一个新的Azure WebJob项目。 在 app.config 中,我设置了三个连接字符串: AzureWebJobsDashboard AzureWebJobsStorage MyDatabaseConnectionString AzureWebJobsDashboard和AzureWebJobsStora

  • 我在运行spring项目时出现了这个错误 Java-cp“parser.jar”hu.daniel.hari.learn.spring.orm.main.SpringOrmMain 违规资源:类路径资源[spring.xml] My Beans.xml http://www.springframework.org/schema/beans http://www.springframework.or