Glasnhost
Glasnhost

Reputation: 1135

SpringBoot Hibernate Entity mapping for existing database table

I need to use SpringBoot on a old legacy db where table are already defined and populated. I don't understand if it's even possible to map an @Entity to an existing table. I tried to use

@Entity
 public class MyTable {...}

but every time I launch the SpringBoot application the table is destroyed and recreated empty... I tried to look in Hibernate Entity annotation doc but I didn't find anything useful. I've seen around some tools, but I would rather do it quickly by hand.

I don't even know if what I intend to do is meaningful, first time using Hibernate.

Upvotes: 2

Views: 8638

Answers (3)

Paul Carranza
Paul Carranza

Reputation: 19

Since it is already with data I recommend you to set spring.jpa.hibernate.ddl-auto = update so it will not delete anything while restarting the project. also you can add next properties to get more information about what it is actually executing: spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true

This configuration is added in application.properties file

here you can fin more information: how does exactly spring.jpa.hibernate.ddl-auto property works in Spring? https://www.baeldung.com/sql-logging-spring-boot

Upvotes: 0

Abdelghani Roussi
Abdelghani Roussi

Reputation: 2817

I suppose you are using springboot-data-jpa in someway, so what you should know is this :

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.

so try to disable the creation of the DDL by setting spring.jpa.hibernate.ddl-auto = none in your application.properties

Upvotes: 3

Glasnhost
Glasnhost

Reputation: 1135

oh, I had spring.jpa.hibernate.ddl-auto=create in my application.properties....

Upvotes: 0

Related Questions