Hibernate Does Not Change Automatically in Database

Asked

Viewed 432 times

0

I’d like Hibernate to not make any changes directly to the database, which means I’d like him to follow what’s in the database. Example : If in my entity I have the following situation

@ManyToOne
@JoinColumn(name="locacao_codcliente", foreignKey=@ForeignKey(name = "LO_CL_FK"))
private Cliente cliente;

@ManyToOne
@JoinColumn(name="locacao_codvendedor", foreignKey=@ForeignKey(name = "LO_VE_FK"))
private Vendedor vendedor;

Note that I would have two fields that are foreign key that will receive the names locao_codvendedor and locaca_codcliente.

I wish that if the DBA had not created these fields there in the database it would not create automatically. I’m starting now and prefer to follow the pattern that the fields are already created in the database.

The creation of my database is as below :

spring.datasource.url = jdbc:mysql://localhost:3306/sitiusdb?useTimezone=true&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = update

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

1 answer

0


Your ddl-auto is configured for update, that is, Hibernate will compare what is in your bank with the templates and generate the changes cfme the templates. To disable this you have to change the update value to None. Full file below.

spring.datasource.url = jdbc:mysql://localhost:3306/sitiusdb?useTimezone=true&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = root

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = none

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

The other possible values are:

  • validate: validate schema but make no changes to the database.
  • update: update the schema.
  • create: create schema, delete from previous data.
  • create-drop: drop the schema when Sessionfactory is closed, i.e.
  • 1

    It really worked. It doesn’t even create the table. I find it more comfortable to work like this than to let Hibernate dominate the database. I think it’s too risky and I’ve done some things wrong once.

  • I also prefer not to let Hibernate mess with the structure of the bank, but not to be managing the bank in hand use the liquibase, when you have a look time is an excellent tool to manage changes in the bank.

  • Good morning, create another question with this question, it is easier to help you and more organized

Browser other questions tagged

You are not signed in. Login or sign up in order to post.