Discovered new Hibernate feature - hbm2ddl
While working on project:Teamdocs, I discovered a new Hibernate feature–”hbm2ddl”:http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html#configuration-optional. This is a flag that you can put in your hibernate.cfg.xml file. It gets run when Hibernate is first configured. What is does is to, depending on how you set it, either create or update your database schema depending on how you have it set.
For me, the biggest benefit so far is that is allows for very easy testing of mapping files. Currently, I am trying to convert the database persistence layer that I wrote in JDBC over to Hibernate. Using hbm2ddl allows me to create a single JUnit test that gets a SessionFactory. This automatically runs through the mapping files and generates the resulting SQL statements to create the tables.
This solves a huge problem that I was facing: how to create all the tables in Derby. Because it is an embedded database, it works a little differently from a regular database server. I have yet to find a way to start it up and then import an sql file in order to create all the tables (although I am sure that there is a way of doing this). Using hbm2ddl, I don’t have to worry about it because Hibernate will take care of it for me.
For people who want to use Derby for testing, this is also an excellent way of ensuring you start every set of tests with a clean database.
You´re one of my favorite people now. Great Blog! I’m sure glad I found your information when I was looking for sql programming thank you for the knowledge! If you are interested, please go see my sql programming related site. It isn’t anything special but you may still find something of interest.