Question

The last few days I've rolled up my sleeves and dug into Hibernate for the first time. I was very surprised to learn that Hibernate's default behavior is to actually drive the DDL of the database itself:

<property name="hbm2ddl.auto">create</property>
    or
<property name="hbm2ddl.auto">update</property>

This is opposite of what I'm used to, where someone (usually a DBA) creates the database structure: the schemas, the table, the key constraints, the indexes, triggers, etc; and then I (the developer) code my app to abide those constraints.

This raises a few similarly-related questions:

  1. How are indexes created/maintained in conjunction with a Hibernate-based app? Pick your favorite relational DB - MySQL, Postgres, Oracle, anything. Do you specify indexes through Hibernate (and if so, how), or do you have to specify them in the DB (and if so, how do you get Hibernate to honor such indexes and not overwrite them)?
  2. Same question as #1 above, but with multi-column keys instead of indexes.
  3. How do you specify column order in Hibernate? Is it just based on the order of the Java fields inside the entity? What about columns that Hibernate adds (such as when doing joins or implementing inheritance strategies)?
  4. If I manuall install a trigger on a table that Hibernate created, how do I prevent Hibernate from overwriting/deleting it?
  5. How do I specify what DB/schema a Hibernate table gets created in?

Thanks in advance!

Était-ce utile?

La solution

  1. You can use @Index annotation on your entity field
  2. Please see this question / answer: How to define index by several columns in hibernate entity?
  3. Yes it's just based on the order of Java fields in the entity
  4. You can set hbm2ddl.auto to "validate" to make it just validate your schema, without making any updates.
  5. You can use @Table(name = "..") annotation to specify custom name for your entity/table
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top