2

The hibernate orm options, create, create-drop, update and validate.

Is there any one that says, create once, but don't drop or update?

An option that says, just create if don't exist, don't update, don't drop?

mjs
  • 21,431
  • 31
  • 118
  • 200

3 Answers3

1

The create-once as you are seeking doesn't exist as a hibernate flag. For your scenario it would be better to manage the DDL of your DB with an external tool and not use the hibernate.hbm2ddl.auto. Pesonally, I can recommend http://www.liquibase.org/, its advertised as a source control for the DB and it really supports development well, when working against different branches with a fast changing model.

Liquibase is centered around the changeLog.xml file, that store all the changes applied to the DB. So when you make a change to your model, you generate a diff resulting in xml entries for the changeLog.xml. The underlying DB keeps track of the applied changes, so you can, re-create, apply update, generate migrate SQL, empty DB or leave as is, you're in control.

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • I just want to get up and running and just want to create the damn database without fiddling, starting with create to then manually having to shut it off afterwards. In https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch03.html section hibernate.hbm2ddl.auto create is mentioned as an option. – mjs Dec 17 '14 at 17:39
  • but than just use "create", and switch to "none", or leave empty whey you have your DB. – Master Slave Dec 17 '14 at 17:41
  • Yeah, that's exactly what I don't want to have to do. I don't want to start up the application, shut it down, go into some random file in production, change it and then start it again. If I need to drop it later on, I have to a) drop the database manually, then repeat this f***g step, twice! – mjs Dec 17 '14 at 18:02
1

Here is an existing discussion that summarizes the options of hibernate.hbm2ddl.auto. hbm2ddl discussion

It is common for shops that use Hibernate to manage the physical database layer separately, using SQL DDL scripts run directly by DBAs for example, rather than allowing Hibernate tools to modify the database schema. If you still wish to have Hibernate run these for you, the hibernate.hbm2ddl.import_files option can be used to tweak your DDL operations manually by providing custom script files.

In your case if you wish to let your ORM dictate the phsyical model as opposed to the reverse, you could just leave your hbm2ddl.auto as 'update'. The other option would be to programmatically generate your script files referenced in hibernate.hbm2ddl.import_files using entity mapping or meta-data(annotations) as the guiding input.

Community
  • 1
  • 1
itguysouth
  • 36
  • 1
  • I don't want to update it. I just want it to be conveniently created but not dropped or altered. It's so strange that no one though of this as a possible usecase. I will look into the import_files option someday, but right now I just want to send the war to the server and hit run, shut down, run, shutdown run, as many times as I want, without altering tables, dropping or whatever. – mjs Dec 17 '14 at 18:05
  • I see. just leave it as none or validate then, and make changes manually yourself if there are model changes. if there are no changes, leaving as none will greatly reduce your server start time. – itguysouth Dec 17 '14 at 18:13
  • i wanted it to just create the dabatase. I will to have to programatically check for this somehome now. – mjs Dec 17 '14 at 18:15
0

I had to write my own solution for this. This is not possible using hibernate or jpa.

mjs
  • 21,431
  • 31
  • 118
  • 200