hibernate - SpringBoot Hibernate 现有数据库表的实体映射

标签 hibernate spring-boot

我需要在旧的遗留数据库上使用 SpringBoot,其中已经定义并填充了表。 我不明白是否可以将 @Entity 映射到现有表。 我尝试使用

@Entity
 public class MyTable {...}

但是每次我启动 SpringBoot 应用程序时,表都会被销毁并重新创建为空...... 我尝试查看 Hibernate Entity 注释文档,但没有找到任何有用的东西。我见过一些工具,但我宁愿手动快速完成。

我什至不知道我打算做的事情是否有意义,第一次使用Hibernate。

最佳答案

我想您正在以某种方式使用 springboot-data-jpa,所以您应该知道的是:

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.

因此,请尝试通过在 application.properties 中设置 spring.jpa.hibernate.ddl-auto = none 来禁用 DDL 的创建

关于hibernate - SpringBoot Hibernate 现有数据库表的实体映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55284548/

相关文章:

mysql - 从 postman 发帖时,数据以匿名用户身份在数据库中发布

security - Spring Boot 始终使用最新的 tomcat 版本

mysql - Spring Hibernate事务隔离不能产生不可重复读

java - hibernate session 使用特定的jdbc连接?

spring-boot - 在构建时从application.properties设置值

当 https 端口被 http 请求命中时,Spring boot 禁用响应

java - Spring Boot 安全性在登录失败后显示 Http-Basic-Auth 弹出窗口

postgresql - JPA/hibernate/PostgresDB 和 Vaadin 8 : SQL select query to get data from inherited tables and show in Vaadin grid

java - hibernate "repeated column in mapping for entity"错误

java - @OneToMany 关系 hibernate