【发布时间】:2021-07-14 07:45:12
【问题描述】:
我有一个连接到 Heroku 的 Spring Boot 应用程序,所以我想尝试将 postgreSQL 集成到我的应用程序中。
我创建了数据库和一个非常基本的表: table creation and data insertion
在我的 SpringBoot 应用程序中,我创建了一个 repository、一个entity,然后我只是想打印出名字。
这是 JPA 存储库:
@Repository
public interface MytableRepository extends JpaRepository<Mytable, Integer>{
}
这是实体:
@Entity
@Table(name = "mytable")
public class Mytable {
@Id
@Column(name="id")
private int id;
@Column(name = "firstName")
private String firstName;
@Column(name = "lastName")
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
最后这是我的控制器:
@Controller
public class TestingStationController {
@Autowired
MytableRepository MytableRepo;
@RequestMapping(value={"test", "Test", "TEST", "TestingStation", "testingstation", "Testingstation", "testingStation"})
public ModelAndView testingStationController(ModelAndView mv) {
mv.setViewName("TestingStation/TestingStation");
System.out.println(MytableRepo.findAll());
List<Mytable> mytable = MytableRepo.findAll();
for (Mytable mytable2 : mytable) {
System.out.println("name=" + mytable2.getFirstName());
}
return mv;
}
}
最后,我要做的只是从这张表中打印出一些东西。该应用程序构建得很好,我似乎没有任何连接问题。以防万一这是我的 application.properties 示例:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://ec2-54-224-120-186.compute-1.amazonaws.com:5432/ddd3l9bfia0463
spring.jpa.properties.hibernate.default_schema=public
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
出于显而易见的原因省略用户名和密码。没有控制台错误,没有崩溃,没有连接问题。当我第一次打印时只打印空括号,然后在 foreach 循环中不打印任何内容。任何帮助将不胜感激。
【问题讨论】:
-
并且该数据再次被删除。你让休眠创建将在启动时擦除它的模式。将
generate-ddl设置为false和/或ddl-auto设置为none以保留您所拥有的。 -
感谢您花时间回答 Deinum 先生。这并没有解决它,但确实带来了一个新的错误,指出
o.h.engine.jdbc.spi.SqlExceptionHelper : table "mytable" does not exist, skipping -
这意味着您正在查看与 hibernate 不同的数据库(并且显然需要为其创建表)。或者您是否真的将这些数据和更改提交到数据库?如果不是,它将不可见。
-
是的,我刚刚去重新启动我的 postgres 客户端与我的数据库的连接以确保。然后在我的桌子上快速选择,我看到了数据
-
那么唯一的结论可能是您正在查看不同的数据库。检查您自己和应用程序的连接 URL。
标签: spring postgresql spring-boot heroku heroku-postgres