2017-12-18 4 views
0

私はherokuの無料アカウントにSpringブートアプリケーションを配備する方法を学んでいます。私が本当のPostgresデータベースをHerokuの可能なアドオンの1つとして提供するまでは、すべてうまくいっています。 私のアドオンを追加してDBに接続するためのすべての設定を取得できますが、JpaRepositoriesとPojosをdbにマップするように作成すると、データベースを作成できません。Heroku Postgres with Spring boot JPAが動作しない

私のHerokuダッシュボードは、DBにアクセスするためのすべての資格情報を与えてくれます。私は、Herokuが定期的に環境変数を使用してそれらを上書きしても、春の起動アプリケーションで使用します。ここで

application.properties:

spring.jpa.hibernate.ddl-auto=create-drop 
spring.datasource.url=jdbc:postgres://xxxxxxxx:xxxxxx 
spring.datasource.username=xxxxxxxx 
spring.datasource.password=xxxxxxxx 

私はHerokuのは、環境変数を使用してデータソースのデータを上書きすることを読んだとしても。ここで

package com.quicktutorialz.learnmicroservices.DemoHeroku; 

import com.quicktutorialz.learnmicroservices.DemoHeroku.daos.AuthorDao; 
import com.quicktutorialz.learnmicroservices.DemoHeroku.daos.BookDao; 
import com.quicktutorialz.learnmicroservices.DemoHeroku.entities.Author; 
import com.quicktutorialz.learnmicroservices.DemoHeroku.entities.Book; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

import javax.annotation.PostConstruct; 
import javax.validation.Valid; 
import java.util.List; 

@RestController 
@SpringBootApplication 
public class DemoHerokuApplication { 

public static void main(String[] args) { 
    SpringApplication.run(DemoHerokuApplication.class, args); 
} 

@Autowired AuthorDao authorDao; 
@Autowired BookDao bookDao; 

@RequestMapping("/") 
public String hello(){ 
    return "Hello!"; 
} 

@RequestMapping("/books") 
public List<Book> getBooks(){ 
    return bookDao.findAll(); 
} 

@RequestMapping("/books/{id}") 
public Book getOneBook(@PathVariable(name = "id") Integer id){ 
    return bookDao.findOne(id); 
} 

@RequestMapping("/authors") 
public List<Author> getAuthors(){ 
    return authorDao.findAll(); 
} 

@RequestMapping("/authors/{id}") 
public Author getOneAuthor(@PathVariable(name = "id") Integer id){ 
    return authorDao.findOne(id); 
} 

@RequestMapping("/books/save") 
public Book saveBook(@Valid Book book){ 
    return bookDao.save(book); 
} 

@RequestMapping("/books/delete/{id}") 
public List<Book> deleteBook(@PathVariable(name = "id") Integer id){ 
    bookDao.delete(id); 
    return bookDao.findAll(); 
} 

@RequestMapping("/authors/save") 
public Author saveAuthor(@Valid Author author){ 
    return authorDao.save(author); 
} 

@RequestMapping("/authors/delete/{id}") 
public List<Author> deleteAuthor(@PathVariable(name = "id") Integer id){ 
    authorDao.delete(id); 
    return authorDao.findAll(); 
} 

@PostConstruct 
private void fillDatabase(){ 
    authorDao.save(new Author(null, "Gino Camarda", "[email protected]")); 
    authorDao.save(new Author(null, "Attilia Nomeldini", "[email protected]")); 

    bookDao.save(new Book(null, "Il basket per me", "Saggio sportivo", null, 1)); 
    bookDao.save(new Book(null, "W il basket", "Saggio sportivo", null, 1)); 

    bookDao.save(new Book(null, "Giornali e pareri", "Saggio giornalistico", null, 2)); 
    bookDao.save(new Book(null, "Versioni della verita", "Saggio giornalistico", null, 2)); 

} 
} 

私の最初のPOJO:

package com.quicktutorialz.learnmicroservices.DemoHeroku.entities; 

import lombok.AllArgsConstructor; 
import lombok.Getter; 
import lombok.NoArgsConstructor; 
import lombok.Setter; 
import org.hibernate.validator.constraints.NotBlank; 

import javax.persistence.*; 

@Entity @Table(name="authors") @AllArgsConstructor @NoArgsConstructor 
@Getter @Setter 
public class Author { 

    @Id @Column(name="ID") @GeneratedValue 
    private Integer id; 
    @Column(name="NAME") @NotBlank 
    private String name; 
    @Column(name="EMAIL") @NotBlank 
    private String email; 
} 

ここでは私の第二POJO:

package com.quicktutorialz.learnmicroservices.DemoHeroku.entities; 

import lombok.AllArgsConstructor; 
import lombok.Getter; 
import lombok.NoArgsConstructor; 
import lombok.Setter; 
import org.hibernate.validator.constraints.NotBlank; 

import javax.persistence.*; 
import javax.validation.constraints.NotNull; 
import java.util.Date; 

@Entity @Table(name="books") 
@AllArgsConstructor @NoArgsConstructor @Getter @Setter 
public class Book { 
    @Id @Column(name="ID") @GeneratedValue 
    private Integer id; 
    @Column(name="TITLE") @NotBlank 
    private String title; 
    @Column(name="TITLE") @NotBlank 
    private String description; 
    @Column(name="TITLE") 
    private Date dateOfRelease; 
    @Column(name="TITLE") @NotNull 
    private int authorId; 

    @PrePersist 
    private void setDate(){ 
    this.dateOfRelease = new Date(); 
    } 
} 
ここ

は私の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>com.quicktutorialz.learnmicroservices</groupId> 
<artifactId>DemoHeroku</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 

<name>DemoHeroku</name> 
<description>Demo project for Spring Boot</description> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.9.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.8</java.version> 
</properties> 

<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>org.postgresql</groupId> 
     <artifactId>postgresql</artifactId> 
     <scope>runtime</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.projectlombok</groupId> 
     <artifactId>lombok</artifactId> 
     <optional>true</optional> 
     <version>1.16.10</version> <!-- added --> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 
</project> 

は私のコントローラであります

これはJpaRepositoriesです。

AuthorDao.java:

package com.quicktutorialz.learnmicroservices.DemoHeroku.daos; 

import com.quicktutorialz.learnmicroservices.DemoHeroku.entities.Author; 
import org.springframework.data.jpa.repository.JpaRepository; 

public interface AuthorDao extends JpaRepository<Author, Integer>{ 

} 

BookDao.java

package com.quicktutorialz.learnmicroservices.DemoHeroku.daos; 

import com.quicktutorialz.learnmicroservices.DemoHeroku.entities.Book; 
import org.springframework.data.jpa.repository.JpaRepository; 

public interface BookDao extends JpaRepository<Book, Integer>{ 
} 

私は、ローカルのMavenでそれをパッケージ化しようとすると、私はこのエラーを取得:

Description: 

Cannot determine embedded database driver class for database type NONE 

私が展開するとき私のアプリが一般的なエラーでクラッシュする:

at=error code=H10 desc="App crashed" method=GET path="/" 
+0

okすぐに削除します –

答えて

0

問題はBook pojoの注釈@Columnの過去のコピーに原因があります。まったく同じ名前の列がありました!

関連する問題