spring boot hibernate查询无效用户错误

Posted

技术标签:

【中文标题】spring boot hibernate查询无效用户错误【英文标题】:spring boot hibernate query invalid user error 【发布时间】:2017-07-15 10:27:26 【问题描述】:

您好,我是 Spring Boot 新手。我尝试连接到 Oracle 并列出相关记录。我的代码在存根环境中工作,即没有连接到数据库。当我尝试从 Spring 连接到数据库时,我收到了 EDIT 2 中给出的错误:

HomeController

package blog.controllers;

import blog.models.Post;
import blog.services.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

@Controller
public class HomeController 

    @Autowired
    private PostService postService;

    @RequestMapping("/")
    public String index(Model model)

        List<Post> latest5Posts = postService.findLatest5();
        model.addAttribute("latest5posts", latest5Posts);

        List<Post> latest3Posts = latest5Posts.stream()
                .limit(3).collect(Collectors.toList());
        model.addAttribute("latest3posts", latest3Posts);

        return "index";

    

帖子实体类

package blog.models;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "POSTS")
public class Post 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 300)
    private String title;

    @Lob @Column(nullable = false)
    private String body;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private User author;

    @Column(nullable = false)
    private Date creation_date = new Date();

    public Long getId() return id;
    public void setId(Long id) this.id = id;
    public String getTitle() return title;
    public void setTitle(String title) this.title = title;
    public String getBody() return body;
    public void setBody(String body) this.body = body;
    public User getAuthor() return author;
    public void setAuthor(User author) this.author = author;
    public Date getCreation_date() return date;
    public void setCreation_date(Date creation_date) this.creation_date = creation_date;

    public Post()  

    public Post(Long id, String title, String body, User author) 

        this.id=id; this.title=title; this.body=body; this.author=author;
    

    @Override
    public String toString() 
        return "Post" + "id=" + id + ", title='" + title + '\'' +
                ", body='" + body + '\'' +
                ", author='" + author + '\'' +
                ", date=" + Creationdate +
                '';
    


用户实体类

package blog.models;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;


@Entity
@Table(name = "users")
public class User 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 30, unique = true)
    private String username;

    @Column(length = 60)
    private String passwordHash;

    @Column(length = 100)
    private String fullName;

    @OneToMany(mappedBy = "author")
    private Set<Post> posts = new HashSet<>();


    public Long getId() return id;

    public void setId(Long id) this.id = id;

    public String getUsername() return username;

    public void setUsername(String username) this.username = username;

    public String getPasswordHash() return passwordHash;

    public void setPasswordHash(String passwordHash) this.passwordHash = passwordHash;

    public String getFullName() return fullName;

    public void setFullName(String fullName) this.fullName = fullName;

    public Set<Post> getPosts() return posts;

    public void setPosts(Set<Post> posts) this.posts = posts;

    public User()  

    public User(Long id, String username, String fullName) 
        this.id = id; this.username = username; this.fullName = fullName;
    

    @Override
    public String toString() 
        return "User" + "id=" + id + ", username='" + username + '\'' +
                ", passwordHash='" + passwordHash + '\'' +
                ", fullName='" + fullName + '\'' + '';
    

存储库

package blog.repositories;

import blog.models.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import org.springframework.data.domain.Pageable;
import java.util.List;


@Repository
public interface PostRepository extends JpaRepository<Post, Long> 
    @Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.creation_date DESC")
    List<Post> findLatest5Posts(Pageable pageable);

服务

package blog.services;

import blog.models.Post;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public interface PostService 

    List<Post> findAll();
    List<Post> findLatest5();
    Post findById(Long id);
    Post create(Post post);
    Post edit(Post post);
    void deleteById(Long id);

服务实施

package blog.services;

import blog.models.Post;
import blog.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Primary
public class PostServiceJpaImpl implements PostService 
    @Autowired
    private PostRepository postRepository;

    @Override
    public List<Post> findAll() 
        return this.postRepository.findAll();
    
    @Override
    public List<Post> findLatest5() 
        return this.postRepository.findLatest5Posts(new PageRequest(0, 5));
    
    @Override
    public Post findById(Long id) 
        return this.postRepository.findOne(id);
    
    @Override
    public Post create(Post post) 
        return this.postRepository.save(post);
    
    @Override
    public Post edit(Post post) 
        return this.postRepository.save(post);
    
    @Override
    public void deleteById(Long id) 
        this.postRepository.delete(id);
    

至于我在数据库中的表:

create table users (
id number,
username varchar2(30),
passwordHash varchar2(60),
fullName varchar2(100),
constraint users_pk primary key (id));

create table posts (
id number,
author_id number,
title varchar2(300),
body clob,
creation_date timestamp,
primary key (id));

附言我还通过更改存储库尝试了以下操作:

@Query(value = "SELECT * FROM POSTS aa LEFT JOIN USERS bb ON aa.AUTHOR_ID=bb.ID", nativeQuery = true)

这次我遇到了以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at ...
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users]
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:59) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:431) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:420) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:386) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:214) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:60) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:134) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:101) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:470) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    ... 27 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: ORA-02268: referenced table does not have a primary key

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1814) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1779) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:277) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at org.hibernate.tool.schema.internal.TargetDatabaseImpl.accept(TargetDatabaseImpl.java:56) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    ... 37 common frames omitted

我错过了什么?

提前致谢!

编辑:

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>softuni</groupId>
    <artifactId>mvc-blog</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jackson-databind</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.10.0</version>
        </dependency>


        <dependency>
            <groupId>cosine-lsh</groupId>
            <artifactId>cosinelsh</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <spark.version>1.4.1</spark.version>
        <!--<scala.version>2.10.0</scala.version>-->
    </properties>


</project>

application.properties

#Turn off Thymeleaf cache
spring.thymeleaf.cache = false

spring.datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@...
spring.datasource.username = usrnm
spring.datasource.password = psswrd
# Configure Hibernate DDL mode: create / update
spring.jpa.properties.hibernate.hbm2ddl.auto = update

#hibernate config
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.data.jpa.repositories.enabled=true

# Show or not log for each sql query
spring.jpa.show-sql = true

用户存储库

package blog.repositories;

import blog.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> 

用户服务

package blog.services;

import blog.models.User;

import java.util.List;

public interface UserService 

    List<User> findAll();
    User findById(Long id);
    User create(User user);
    User edit(User user);
    void deleteById(Long id);

用户服务实现

package blog.services;

import blog.models.User;
import blog.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Primary
public class UserServiceJpaImpl implements UserService 
    @Autowired
    private UserRepository userRepository;
    @Override
    public List<User> findAll() 
        return this.userRepository.findAll();
    
    @Override
    public User findById(Long id) 
        return this.userRepository.findOne(id);
    
    @Override
    public User create(User user) 
        return this.userRepository.save(user);
    
    @Override
    public User edit(User user) 
        return this.userRepository.save(user);
    
    @Override
    public void deleteById(Long id) 
        this.userRepository.delete(id);
    

@SpringBootApplication

package blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class BlogMvcApplication 

    public static void main(String[] args) 

        SpringApplication.run(BlogMvcApplication.class, args);

    

编辑 2:

JPA 查询的错误行(不使用本机查询时)

java.sql.SQLSyntaxErrorException: ORA-01747: invalid user.table.column, table.column, or column specification

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491) ~[ojdbc6-11.2.0.4.jar:11.2.0.1.0]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2117) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1900) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1876) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:919) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2617) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2600) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2424) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1326) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87) ~[hibernate-core-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483) ~[hibernate-entitymanager-5.0.9.Final.jar:5.0.9.Final]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:114) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:102) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:92) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.12.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280) ~[spring-tx-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.10.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at com.sun.proxy.$Proxy87.findLatest5Posts(Unknown Source) ~[na:na]
    at blog.services.PostServiceJpaImpl.findLatest5(PostServiceJpaImpl.java:23) ~[classes/:na]
    at blog.controllers.HomeController.index(HomeController.java:26) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_31]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_31]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_31]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_31]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]

【问题讨论】:

请发布完整的堆栈跟踪..还有你有任何弹簧配置文件吗? java还是xml? 感谢您的回复@MaciejKowalski!请参阅编辑部分。 好的,你能添加你的@SpringBootApplication public class Application 实现吗?与放置它的包 啊,是的,我刚刚添加了它。 ok.. 最后你能添加完整的 org.springframework.beans.factory.UnsatisfiedDependencyException: Error created .. exception 【参考方案1】:

看来问题如下:

因为这个属性:

spring.jpa.properties.hibernate.hbm2ddl.auto = update

而且 Posts 实体已经被更改为引用 User,Hibernate 尝试添加外键约束:

alter table posts add constraint FK6xvn0811tkyo3nfjk2xvqx6ns foreign key (author_id) references users

但是得到这个错误:

ORA-02268: referenced table does not have a primary key

不确定为什么 hibernate 不向该表添加主键,因为 @Id 注释明显存在。

尝试手动向 Users.id 列添加主键约束:

ALTER TABLE users
 ADD CONSTRAINT users_pk PRIMARY KEY (id);

更新

问题可能与 Post.data 映射有关,因为您在 order by 子句中使用它。

文档说:

必须为持久字段或属性指定此注解 java.util.Date 和 java.util.Calendar 类型的。它可能只是 为这些类型的字段或属性指定。

由于您使用的是 java.util.Date,所以您需要添加以下内容:

@Temporal(TemporalType.DATE)
private Date date = new Date();

【讨论】:

我已经手动创建了相应的主键。现在,当我运行应用程序时,它卡在 Running hbm2ddl schema update 行中。 似乎在使用本机查询时也不允许使用 Pageable 类。当我再次尝试查询时(@Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC")),我又遇到了一些错误。要查看它们,请参阅上面的 EDIT 2。

以上是关于spring boot hibernate查询无效用户错误的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中向本机查询添加参数会导致“org.hibernate.exception.SQLGrammarException”,

在 Spring Boot、Hibernate 中使用 @Query 注解以 JSON 格式(键值对)查询结果

spring boot + hibernate 在我获得带有命名查询的项目后,我可以使用 entityMnager.persist(item) 吗?

在 Spring Boot 中编写自定义查询

使用 spring boot JPA hibernate 存储库检查用户名和密码的 Mysql 查询不起作用。我该怎么办?

列名在spring boot中无效