{"id":558,"date":"2004-01-21T15:15:32","date_gmt":"2004-01-21T19:15:32","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=558"},"modified":"2004-01-21T15:15:32","modified_gmt":"2004-01-21T19:15:32","slug":"hibernate-code-generation","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/01\/21\/hibernate-code-generation\/","title":{"rendered":"Hibernate Code Generation"},"content":{"rendered":"<p>Justin left a <a href=\"http:\/\/cephas.net\/blog\/2004\/01\/19\/ant_tomcat_manager_on_virtual_hosts.html#comments\">comment<\/a> about various tools for generating Java code from XML.  Last week while I was reading about Hibernate I was interested to read that the same files you use to persist POJO&#8217;s to a database using Hibernate can be used to create the Java objects as well. It&#8217;s reasonably well <a href=\"http:\/\/www.hibernate.org\/hib_docs\/reference\/html\/toolsetguide.html#toolsetguide-s2\">documented on the hibernate site<\/a>, but doesn&#8217;t include all the classes  you need on the classpath, so here&#8217;s an example from my system:<br \/>\n<code><br \/>\njava -cp C:\\hibernate\\hibernate-2.1\\hibernate2.jar;<br \/>\nC:\\hibernate\\hibernate-extensions-2.1\\tools\\hibernate-tools.jar;<br \/>\nC:\\hibernate\\hibernate-2.1\\lib\\commons-collections.jar;<br \/>\nC:\\hibernate\\hibernate-2.1\\lib\\commons-logging.jar;<br \/>\nC:\\hibernate\\hibernate-extensions-2.1\\tools\\lib\\jdom.jar net.sf.hibernate.tool.hbm2java.CodeGenerator Resource.hbm.xml<br \/>\n<\/code><br \/>\nwhere Resource.hbm.xml looks likes this:<br \/>\n<code><br \/>\n&lt;?xml version=\"1.0\"?&gt;<br \/>\n&lt;!DOCTYPE hibernate-mapping<br \/>\n&nbsp;PUBLIC \"-\/\/Hibernate\/Hibernate Mapping DTD\/\/EN\"<br \/>\n&nbsp;\"http:\/\/hibernate.sourceforge.net\/hibernate-mapping-2.0.dtd\"&gt;<br \/>\n&lt;hibernate-mapping package=\"com.mycompany\" auto-import=\"false\"&gt;<br \/>\n&nbsp;&lt;class name=\"Resource\" table=\"resource\"&gt;<br \/>\n&nbsp;&nbsp;&lt;id name=\"id\" type=\"int\" unsaved-value=\"0\" &gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;column name=\"id\" sql-type=\"int\" not-null=\"true\"\/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;generator class=\"identity\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/id&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"label\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"filename\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"teaser\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"type\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"url\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"datetimecreated\" type=\"date\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"datetimemodified\" type=\"date\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"createdby\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"modifiedby\" type=\"string\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"active\" type=\"integer\" \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;property name=\"archive\" type=\"integer\" \/&gt;<br \/>\n&nbsp;&lt;\/class&gt;<br \/>\n&lt;\/hibernate-mapping&gt;<br \/>\n<\/code><br \/>\nThis will automatically create a JavaBean, with full and empty constructors, getters and setters for all the fields, a toString() method, an equals() method, and a hashCode() method.  Additionally, you can use an external configuration file to add an interface that the bean implements, change the rendering mechanism, the package, etc.<\/p>\n<p>Cooler still, you can go backwards and create Hibernate mapping files directly from your existing compiled classes using the <code>net.sf.hibernate.tool.class2hbm.MapGenerator<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Justin left a comment about various tools for generating Java code from XML. Last week while I was reading about Hibernate I was interested to read that the same files you use to persist POJO&#8217;s to a database using Hibernate can be used to create the Java objects as well. It&#8217;s reasonably well documented on &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/01\/21\/hibernate-code-generation\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Hibernate Code Generation<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/558"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=558"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/558\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=558"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=558"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=558"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}