java - 在 Heroku 上设置 PostgreSQL 数据库(需要 entityManagerFactory)
问题描述
我想在 Spring Boot 中实现的 Heroku 上创建一个 Web 应用程序。最后,我设法部署了一个带有“Whitelabel error page 404”的普通项目,确认了部署的正确性。
我跟着(几乎相同):https ://devcenter-assets0.herokucdn.com/articles/spring-boot-memcache
不幸的是,我在 Spring Data JPA 中只遇到了一个(我希望是)问题(在添加了一项服务(例如 Spring Security 和 Lombok 正常工作)之后),这使我无法构建应用程序和使用 PostgreSQL:
Description:
Parameter 0 of constructor in io.github.plkpiotr.fifabackend.controller.TaskController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
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>io.github.plkpiotr</groupId>
<artifactId>fifa-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fifa-backend</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</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>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用程序属性:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
任务.java:
package io.github.plkpiotr.fifabackend.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Task {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
public Task() {}
public Task(String name) {
this.name = name;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return String.format("Task[id=%d, name='%s']", this.id, this.name);
}
}
TaskRepository.java:
package io.github.plkpiotr.fifabackend.repository;
import io.github.plkpiotr.fifabackend.model.Task;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TaskRepository extends CrudRepository<Task, Long> {}
任务控制器.java:
package io.github.plkpiotr.fifabackend.controller;
import javax.validation.Valid;
import java.lang.Iterable;
import io.github.plkpiotr.fifabackend.model.Task;
import io.github.plkpiotr.fifabackend.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/")
public class TaskController {
@Autowired
private TaskRepository taskRepo;
@Autowired
public TaskController(TaskRepository repo) {
this.taskRepo = repo;
}
@RequestMapping(method = RequestMethod.GET)
public String showAllTasks(ModelMap model) {
Iterable<Task> tasks = this.taskRepo.findAll();
model.addAttribute("tasks", tasks);
model.addAttribute("newTask", new Task());
return "task";
}
@RequestMapping(method = RequestMethod.POST)
public String newTask(ModelMap model,
@ModelAttribute("newTask") @Valid Task task,
BindingResult result) {
if (!result.hasErrors()) {
this.taskRepo.save(task);
}
return showAllTasks(model);
}
@RequestMapping(method = RequestMethod.DELETE)
public String deleteTask(ModelMap model, @RequestParam("taskId") Long id) {
this.taskRepo.deleteById(id);
return showAllTasks(model);
}
}
我试过了:
1)。添加差异化依赖项(h2,hibernate-core,...)
2)。删除 /.m2/ 文件夹并重新下载依赖项
3)。不要在其他尝试失败后哭泣……
如果您对克隆 repo感兴趣: https ://github.com/plkpiotr/fifa-backend
除此之外,如果我在 TaskController.java 中删除,我会得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
如何解决问题?
解决方案
@EntityScan
,@EnableJpaRepositories
并且@ComponentScan
不支持通配符语法。例如,您当前使用:
@EntityScan("io.github.plkpiotr.*")`
但它应该只是基本包名称
@EntityScan("io.github.plkpiotr")
推荐阅读
- mysql - 如何在不同的 WHERE 条件下加入?
- laravel - Laravel 表单请求验证存储和更新使用相同的验证
- node.js - 使用文件夹导入模块并将数据传递给nodejs中的模块
- c - 索引怎么可能是字符串(i[s])?
- amazon-s3 - Oozie 在 EMR 上将操作数据路径从 S3 更改为 hdfs
- axios - Nuxt:在 .ENV 中设置 Axios BaseURL 不起作用?
- python - 预算午餐和晚餐的 Web Scraping Tabelog
- python - 映射 Django url 和视图
- artificial-intelligence - PDDL AI 规划中的一个错误
- entity-framework-core - 包含在实体框架中的问题