我正在学习如何在我的heroku免费帐户上部署一个spring boot应用程序。一切顺利,直到我想使用真正的Postgres数据库提供作为Heroku的可能附加组件之一。我添加了我的附加组件,并且我能够获得连接到DB的所有设置,但是当我创建JpaRepositories并将Pojos映射到db时,它无法创建数据库。
我的Heroku仪表板为我提供了访问数据库的所有凭据,我在Spring启动应用程序中使用它们,即使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将使用环境变量覆盖数据源数据。
这是我的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>
这是我的控制器:
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();
}
}
这些是JpaRepositories。
author DAO.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>{
}
book DAO.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
当我在Heroku上部署时,我的应用程序因一般错误而崩溃:
at=error code=H10 desc="App crashed" method=GET path="/"
问题是由于Book pojo中注释@Column的复制过去。有完全相同名称的列!