{"id":39,"date":"2011-04-02T01:09:35","date_gmt":"2011-04-02T08:09:35","guid":{"rendered":"http:\/\/brie.com\/brian\/blog\/?p=39"},"modified":"2011-04-02T01:12:04","modified_gmt":"2011-04-02T08:12:04","slug":"generating-domain-classes-from-hbm-files-in-hibernate","status":"publish","type":"post","link":"https:\/\/brie.com\/brian\/blog\/?p=39","title":{"rendered":"Generating domain classes from hbm files in Hibernate"},"content":{"rendered":"<p>Hibernate offers the capability to generate both the database schema and the java class files for the domain model when you specify the model using the .hbm.xml files.\u00a0 You can accomplish this using the hibernate3-maven-plugin. I was not able to find too much clear documentation on how to accomplish this, but I was able to hack together a configuration that works. Once again, you start with Maven installed and make sure that you have the <em>.m2\/<a href=\"http:\/\/www.brie.com\/brian\/hibernate\/start\/settings.xml\">settings.xml<\/a> <\/em>file for Maven pointing to the JBoss nexus repository. See my <a title=\"Hibernate Tutorial refined\" href=\"http:\/\/brie.com\/brian\/blog\/?p=27\">hibernate tutorial refined<\/a> for instructions on doing this. Assuming you have Maven installed and the settings for the JBOSS tutorial installed, these are the steps. The lines that you type in commands, don&#8217;t type the &#8216;$&#8217;. First, download the sample code using the following command:<\/p>\n<p><code>$ wget http:\/\/brie.com\/brian\/hibernate\/hibcodegen.tgz<\/code><\/p>\n<p>Unpack it using tar. This works on GNU\/Linux.<\/p>\n<p><code>$ tar zxvf hibcodegen.tgz<\/code><\/p>\n<p><em>cd <\/em>into the directory where you unpacked the code.<\/p>\n<p><code>$ cd hibcodegen<\/code><\/p>\n<p>Do an <em>ls<\/em> of the directory and you should see the pom.xml file. Start two more terminals having the same current working directory as the pom.xml file. In window 1, start the database server, window 2, the database manager, and 3, we will use to complete the rest of the commands. The following is the command to start the database, HSQLDB (all on one line):<\/p>\n<p><code><br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.Server\" -Dexec.args=\"-database.0 file:target\/data\/tutorial -dbname.0  meetings\"<\/code><\/p>\n<p>Start the dababase manager with the following command (again all on one line!):<\/p>\n<p><code><br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.util.DatabaseManagerSwing\" -Dexec.args=\"-url jdbc:hsqldb:hsql:\/\/localhost\/meetings -driver org.hsqldb.jdbcDriver<\/code><\/p>\n<p>You database manager should show the database without any tables. Press <em>Ctrl-R <\/em>to verify this on the database manager. Now, comes time for the auto code and schema generation. This is true magic!<\/p>\n<p>This will generate the Meeting.java class in the <em>src\/main\/java\/com\/brie\/domain <\/em>directory:<\/p>\n<p><code>$ mvn hibernate3:hbm2java<\/code><\/p>\n<p>Examine the pom.xml file for the plugins area. The hbm2java takes the <em>Meeting.hbm.xml<\/em> and automatically generates the Java class file with getters, setters, etc. Of course, if you modify the Meeting.java class file, and run the command again, it will clobber the file with the newly generated one, but aside from those deficiencies (there are ways around this), this very slick.<\/p>\n<p>To add to the &#8220;coolness&#8221; factor, the following command generates the shema:<br \/>\n<code>$ mvn hibernate3:hbm2ddl<\/code><\/p>\n<p>Go to the database browser, type <em>Ctrl-R<\/em>, and the new Meeting table will show.<\/p>\n<p>Run a sample program that creates a new object and save it.<br \/>\n<code>$ mvn exec:java -Dexec.mainClass=\"com.brie.NativeApiIllustration\"<\/code><\/p>\n<p>Use the database manager to view the object just created.<\/p>\n<p><code>select * from meeting<\/code><\/p>\n<p>Using the .hbm.xml file to generate the java classes and the schema makes for some interesting tool ideas. I hope to envision the day when one can use Violet to generate a class diagram and it generates the hbml.xml files. I hope this provides a concrete example for using hbm2java and hbm2ddl. I am sure there could be improvements to my approach, so I will be looking to find them in the future.<\/p>\n<p>brian<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hibernate offers the capability to generate both the database schema and the java class files for the domain model when you specify the model using the .hbm.xml files.\u00a0 You can accomplish this using the hibernate3-maven-plugin. I was not able to &hellip; <a href=\"https:\/\/brie.com\/brian\/blog\/?p=39\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":2,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":41,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/41"}],"wp:attachment":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}