I use Hibernate in Spring Boot project, and use Redis/Redisson as second level cache. When entity change in code, then restart application, table column will be changed by “ddl-auto: update”, but cache in Redis will not update, this lead to excepiton.
You will have to clear the cache with a custom mechanism. Think about it, it would be awful if Hibernate always cleared the cache on startup, but how can Hibernate know if it should do that? Don’t tell me it should be based on hbm2ddl because you should not use hbm2ddl in production. Always use a tool like Liquibase or Flyway to manage your schema changes.
Is there some way to catch schema changes , such as listener ?
I don’t need always cleared the cache on startup, just need cleared the cache on startup when schema changes .
Emm, I realy use hbm2ddl in production.
Using hbm2ddl in production may cause data loss. You should really switch to Liquibase or Flyway. If you rename a column/table in your model, Hibernate will drop the old column/table and create the new one. This means data might be lost. With a schema migration tool, you can make sure such changes are handled properly. Also, hbm2ddl is slow on startup because it always needs to discover the whole schema.
Is there some way to catch schema changes , such as listener ?
Just don’t. You could misuse schema validation to detect such a scenario in custom code, but really, just stop using hbm2ddl for production migration.