1

I have the hibernate.hbm2ddl.auto=create-drop setting but I also create a view based on one of the tables in SQL after hibernate generates the database using hibernate.import_files=db/create.sql.

The next time it runs hibernate cannot drop the existing table because the view depends on it.

How do I make hibernate drop table cascade so that the view gets dropped as well?

Defining the entity like this doesn't seem to have an effect

@Entity
@OnDelete(action = OnDeleteAction.CASCADE)
@Table(name = "MY_TABLE")
opticyclic
  • 7,412
  • 12
  • 81
  • 155

1 Answers1

1

I had exactly the same issue - a view definition (with corresponding JPA mapping) that blocked other table to be dropped.

The solution that works for me was to use javax.persistence.* properties that gives you more control over create/drop scripts. So instead of using plain hibernate.hbm2ddl.auto I've used javax.persistence.schema-generation.database.action. That way I was able to first drop the view and then referenced table.

By using <property name="javax.persistence.schema-generation.create-source" value="script-then-metadata"/> you can combine user-defined script with schema auto-creation.

You can read more about these properties here: https://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm