2017-02-24 58 views
1

嗨,我是Spring Boot的新手。我嘗試連接到Oracle並列出相關記錄。我的代碼工作在一個存根環境,這是沒有連接到數據庫。當我試圖連接到從春分貝,我得到了錯誤在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); 
    } 
} 

至於我在db表:

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

p.s.我也試圖通過改變資源庫中的以下情況:

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

應用。性能

#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] 
+0

請張貼滿堆棧跟蹤..還你有什麼彈簧配置文件? java或xml? –

+0

感謝您的回覆@MaciejKowalski!請參閱編輯部分。 –

+0

好的,您可以添加您的@SpringBootApplication公共類應用程序{}實施嗎?與包放在它的地方 –

回答

0

似乎問題如下:

正是由於這一特性:

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

而且該帖子實體已更改爲用戶參考的事實,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映射爲你使用它按條款排序。

的醫生說:

This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar. It may only be specified for fields or properties of these types.

當您使用java.util.Date,那麼你需要補充一點:

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

我已經手動創建了相應的主鍵。現在,當我運行該應用程序時,它會卡在運行hbm2ddl架構更新的行中。 –

+0

這似乎也是在使用本機查詢時不允許Pageable類。當我再次嘗試查詢(@Query(「SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC」))時,我又出現了一些錯誤。要看到它們,請參閱上面的編輯2。 –

+0

看到我的更新,並嘗試一下 –