2

I'm using Hibernate (JPA2) hibernate.hbm2ddl.auto=update for test and hibernate.hbm2ddl.auto=validate for production.

What I want to do is, extending the generated schema with an additional table (that is not mapped to an entity) so that this table is generated for the tests and verify for production.

Is this possible, and how?

Ralph
  • 118,862
  • 56
  • 287
  • 383

2 Answers2

3

Yes, it's possible using "auxiliary database objects". I wrote a blog post on the topic because the documentation wasn't the greatest.

Edit: One other undocumented feature of Hibernate that I didn't mention in that blog: if you include a file named "import.sql" in the root of your classpath when you run a Hibernate schema export, it will also execute the statements in that file.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • +1 That sounds good: but how to do this in a JPA2 environment (I only have the persistence.xml and the orm.xml) – Ralph Jul 22 '11 at 07:35
  • @Ralph: I'm afraid I can't answer that. I've never had a reason to use JPA. I added a note to my answer about import.sql that might help you out. – Ryan Stewart Jul 23 '11 at 00:27
  • I accept the answer beause it will work for pure hibernate, even if i did not get it running for jpa – Ralph Jul 30 '11 at 14:15
0

Write an SQL script to create the table. When you release up the environmental chain, run the SQL first to create the table in Prod. Then validate will be fine.

atrain
  • 9,139
  • 1
  • 36
  • 40
  • That is how I do it in the moment, but this did not answer the Problem: The question was: how to validate and autoupdate. -- If I understand your answer right, then the validation will not be enhanced, so the validation will not fail if the table is missing. As well it does not enhance the auto update process. – Ralph Jul 22 '11 at 07:19
  • So you want to `update` in Test, and then `validate` in Prod. If the table is missing, yes, `validate` will throw errors and your app won't start up correctly. Is that what you want? – atrain Jul 22 '11 at 13:33
  • yes - this is the default behavior of update and validate - and I want to extend it to the table for an not jpa managed object. – Ralph Jul 22 '11 at 13:52