1

I have a Spring Boot application accessing a database having schemata A and B, employing JPA and Hibernate, all entities but one are controlled by me (I can modify them and update their data).

Sometimes, when I restart the application, I want Hibernate to drop/create or update tables to reflect the latest modifications from the entities annotated @Entity in schema 1.

I configured the application to update the database schema and seed it with data.

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.sql.init.mode=always
spring.sql.init.data-locations=classpath:/sample.sql
spring.jpa.defer-datasource-initialization=true

All the entities controlled and seeded by me are hosted in the first schema; the entity I can not modify and seed is hosted in schema 2 (It is managed by another application).

Employing the idead borrowed from here, I was able to protect the entities from the second schema from modifications. Still, when data is loaded, the content of the table is deleted. I underline there are no statements about clearing the content in my seeding script.

How can I prevent the update of the data of the entities in the second schema?

1 Answer 1

1

Hibernate wipes schema B because you let it manage that entity.

If Hibernate sees an @Entity, it assumes "cool, I own this table" and applies ddl-auto=create everywhere...

To fix it, either make the schema-B entity read-only so Hibernate can’t DDL it:

@Entity
@Immutable
@Subselect("select * from schema_b.shared_table")

or put schema B in a second persistence unit with ddl-auto=none, wich is cleaner !

That’s it. Hibernate stops touching schema B because it no longer thinks it’s allowed to...

Sign up to request clarification or add additional context in comments.

1 Comment

I just implemented the first strategy you suggested and found that Hibernate is no longer touching my data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.