java - spring-data-jpa ddl-auto 权限

标签 java spring spring-boot spring-data-jpa

我的项目使用 spring-data-jpa。

公司规定测试环境和生成环境数据库账号只有读写权限。

我发现当ddl-auto设置为none时,数据库结构也会改变,因为没有权限导致服务启动失败。如何设置ddl-auto属性让程序既不改变数据库也不做检查?

spring-data-jpa document:https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-database-initialization

10.2. Initialize a Database Using Hibernate You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, and create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded. It defaults to create-drop if no schema manager has been detected or none in all other cases. An embedded database is detected by looking at the Connection type. hsqldb, h2, and derby are embedded, and others are not. Be careful when switching from in-memory to a ‘real’ database that you do not make assumptions about the existence of the tables and data in the new platform. You either have to set ddl-auto explicitly or use one of the other mechanisms to initialize the database.

最佳答案

spring.jpa.hibernate.ddl-auto=none

标准 Hibernate 属性值为:create、update、create-drop、validate 和 none:

创建 – Hibernate 首先删除现有表,然后创建新表。

更新 – 基于映射(注释或 XML)创建的对象模型与现有架构进行比较,然后 Hibernate 根据差异更新架构。它永远不会删除现有的表或列,即使应用程序不再需要它们。

create-drop – 与create类似,但Hibernate会在所有操作完成后删除数据库。通常用于单元测试。

validate – Hibernate 仅验证表和列是否存在,否则抛出异常。

none – 该值有效地关闭 DDL 生成

关于java - spring-data-jpa ddl-auto 权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58622076/

相关文章:

java - Spring项目中获取配置的更好实践

java - 当我尝试发布到我的 spring api 时 403 被禁止?

java - 如何在 Java 中使用 HSL 色彩空间?

java - View 分页器的 fragment 需要从主要 Activity 中进行编程

java - Apache Spark : akka version error by build jar with all dependencies

java - cURL命令上传文件不上传文件

java - 未达到 Spring MVC 中的异常处理

java - 在 Kotlin 中为接口(interface)编写默认方法时 IClass$DefaultImpls 的 NoClassDefFoundError

spring - 如何使用 JDBC 在 Spring 中创建动态连接(数据源)

java - 带有应用程序/八位字节流的spring boot multipartFile抛出异常