我有一个Java/Spring应用程序在标准环境上运行.每次我将其部署到App Engine时,我正在使用的postgres数据库都会被重置.如何防止这种情况发生?

我的应用程序.属性:

spring.datasource.url=jdbc:postgresql:///***

spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.properties.hibernate.format_sql=true

spring.jpa.hibernate.ddl-auto=create

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

我的app.yaml:

runtime: java17
instance_class: F1

automatic_scaling:
  max_idle_instances: 1

推荐答案

您只需将您的ddl-auto配置更改为:

spring.jpa.hibernate.ddl-auto=validate

spring.jpa.hibernate.ddl-auto=update

These are the options f或 spring.jpa.hibernate.ddl-auto:

  • None:无数据库方案启动
  • Create:在应用程序启动时删除并创建架构.使用此选项,您的所有数据将在每次启动时丢失.
  • Create-drop: Creates schema at the startup and destroys the schema on context closure. Useful f或 unit tests.
  • Validate:仅判断架构是否与实体匹配.如果模式不匹配,则应用程序启动将失败.Makes no changes to the database.
  • Update: Updates the schema only if necessary. F或 example, If a new field was added in an entity, then it will simply alter the table f或 a new column without destroying the data

However, hibernate.ddl-auto should usually not be used in production (check this discussion).

Java相关问答推荐

在Java中将Charsequence数组更改为String数组或List String<>

在AnyLogic中增加变量计数

将成为一个比较者.比较…在现代Java中,编译器会对`CompareTo`方法进行优化吗?

在Java Stream上调用collect方法出现意外结果

通过合并Akka Streams中的多个慢源保持订购

具有多种令牌类型和段的复杂Java 17正则表达式

Hibernate EmptyInterceptor可以工作,但不能拦截器

Spring Data JPA慢慢地创建了太多非活动会话

如何对多个字段进行分组和排序?

如何在antlr4中跳过所有反斜杠-换行符而保留换行符?

如何在透视表中添加对计数列的筛选?

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

在应用程序运行时更改LookAndFeel

为什么相同的数据条码在视觉上看起来不同?

如何对存储为字符串的大数字数组进行排序?

始终使用Spring Boot连接mongodb上的测试数据库

转换为JSON字符串时,日期按天递减-Java

UuidGenerator Bean 类型不匹配?

对于 Hangman 游戏,索引 0 超出长度 0 的范围

元音变音字符:如何在 Java 中将Á<0x9c>转换为Ü?