{"id":203,"date":"2007-09-06T00:00:00","date_gmt":"2007-09-06T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=203"},"modified":"2007-09-06T00:00:00","modified_gmt":"2007-09-06T00:00:00","slug":"Writing Hibernate Configuration Files","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=203","title":{"rendered":"Writing Hibernate Configuration Files"},"content":{"rendered":"<p><P>In the previous section we completed the database setup and created required table and populated with the data. In this section we will write required hibernate configuration files.<\/P><br \/>\n<P><B>For this tutorial we need following Hibernate configuration files:<\/B><\/P><br \/>\n<P><B>Hibernate Configuration File<\/B><\/P><br \/>\n<P>Hibernate configuration file (hibernate.cfg.xml) is used to provide the information which is necessary for making database connections. The mapping details for mapping the domain objects to the database tables are also a part of Hibernate configuration file.&nbsp;<\/P><br \/>\n<P>Here is the code of our Hibernate Configuration File:<\/P><br \/>\n<DIV align=left><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"75%\" bgColor=#ffff99 border=1><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\">&lt;?xml version=&#8217;1.0&#8242; encoding=&#8217;utf-8&#8242;?&gt;<BR>&lt;!DOCTYPE hibernate-configuration PUBLIC<BR>&#8220;-\/\/Hibernate\/Hibernate Configuration DTD\/\/EN&#8221;<BR>&#8220;http:\/\/hibernate.sourceforge.net\/hibernate-configuration-3.0.dtd&#8221;&gt;<BR><BR>&lt;hibernate-configuration&gt;<BR>&lt;session-factory&gt;<BR>&lt;property name=&#8221;hibernate.connection.driver_class&#8221;&gt;com.mysql.jdbc.Driver&lt;\/property&gt;<BR>&lt;property name=&#8221;hibernate.connection.url&#8221;&gt;jdbc:mysql:\/\/localhost\/struts-hibernate&lt;\/property&gt;<BR>&lt;property name=&#8221;hibernate.connection.username&#8221;&gt;root&lt;\/property&gt;<BR>&lt;property name=&#8221;hibernate.connection.password&#8221;&gt;&lt;\/property&gt;<BR>&lt;property name=&#8221;hibernate.connection.pool_size&#8221;&gt;10&lt;\/property&gt;<BR>&lt;property name=&#8221;show_sql&#8221;&gt;true&lt;\/property&gt;<BR>&lt;property name=&#8221;dialect&#8221;&gt;org.hibernate.dialect.MySQLDialect&lt;\/property&gt;<BR>&lt;property name=&#8221;hibernate.hbm2ddl.auto&#8221;&gt;update&lt;\/property&gt;<BR>&lt;!&#8211; Mapping files &#8211;&gt;<BR><B>&lt;mapping resource=&#8221;\/roseindia\/net\/dao\/hibernate\/Tutorial.hbm.xml&#8221;\/&gt;<\/B><BR>&lt;\/session-factory&gt;<BR>&lt;\/hibernate-configuration&gt;&nbsp;<BR><br \/>\n<P>&nbsp;<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P>Place hibernate.cfg.xml file in the source directory e.g.&nbsp;&#8220;<B>C:\\Struts-Hibernate-Integration\\code\\src\\java&#8221;<\/B><\/P><br \/>\n<P>The &lt;mapping resource=&#8221;&gt; tag is used to specify the mapping file:<\/P><br \/>\n<P><B>&lt;mapping resource=&#8221;\/roseindia\/net\/dao\/hibernate\/Tutorial.hbm.xml&#8221;\/&gt;<\/B><BR><\/P><br \/>\n<P><B>Code of Tutorial.hbm.xml:<\/B><\/P><br \/>\n<DIV align=left><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"74%\" bgColor=#ffff99 border=1><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\">&nbsp;&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt;<BR>&lt;!DOCTYPE hibernate-mapping PUBLIC &#8220;-\/\/Hibernate\/Hibernate Mapping DTD\/\/EN&#8221;<BR>&#8220;http:\/\/hibernate.sourceforge.net\/hibernate-mapping-3.0.dtd&#8221;&gt;<BR><BR>&lt;hibernate-mapping auto-import=&#8221;true&#8221; default-lazy=&#8221;false&#8221;&gt;<BR><BR>&lt;class&nbsp;<BR>name=&#8221;roseindia.net.dao.hibernate.Tutorial&#8221;&nbsp;<BR>table=&#8221;tutorials&#8221;<BR>&gt;<BR><BR>&lt;id<BR>name=&#8221;id&#8221;<BR>type=&#8221;java.lang.Integer&#8221;<BR>column=&#8221;id&#8221;<BR>&gt;<BR>&lt;generator class=&#8221;increment&#8221; \/&gt;<BR>&lt;\/id&gt;<BR><BR>&lt;property<BR>name=&#8221;shortdesc&#8221;<BR>type=&#8221;java.lang.String&#8221;<BR>column=&#8221;shortdesc&#8221;<BR>not-null=&#8221;true&#8221;<BR>length=&#8221;50&#8243;<BR>\/&gt;<BR>&lt;property<BR>name=&#8221;longdesc&#8221;<BR>type=&#8221;java.lang.String&#8221;<BR>column=&#8221;longdesc&#8221;<BR>not-null=&#8221;true&#8221;<BR>length=&#8221;250&#8243;<BR>\/&gt;<BR>&lt;property<BR>name=&#8221;pageurl&#8221;<BR>type=&#8221;java.lang.String&#8221;<BR>column=&#8221;pageurl&#8221;<BR>not-null=&#8221;true&#8221;<BR>length=&#8221;100&#8243;<BR>\/&gt;<BR><BR><BR>&lt;\/class&gt;<BR>&lt;\/hibernate-mapping&gt;<\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P>Place <B>Tutorial.hbm.xml<\/B> file in the source directory e.g.&nbsp;&#8220;<B>C:\\Struts-Hibernate-Integration\\code\\src\\java\\roseindia\\net\\dao\\hibernate\\<\/B><B>&#8221;&nbsp;<\/B><\/P><br \/>\n<P><B>POJO Object<\/B><BR>Here is the code of Java Bean object (Tutorial.java) used to store and retrieve the data from database.<\/P><br \/>\n<DIV class=java align=left><br \/>\n<TABLE cellSpacing=0 cellPadding=3 bgColor=#ffff99 border=1><br \/>\n<TBODY><br \/>\n<TR><!-- start source code --><br \/>\n<TD vAlign=top noWrap align=left><CODE><FONT color=#7f0055><B>package&nbsp;<\/B><\/FONT><FONT color=#000000>roseindia.net.dao.hibernate;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.io.Serializable;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>public&nbsp;class&nbsp;<\/B><\/FONT><FONT color=#000000>Tutorial&nbsp;<\/FONT><FONT color=#7f0055><B>implements&nbsp;<\/B><\/FONT><FONT color=#000000>Serializable&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;identifier&nbsp;field&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>Integer&nbsp;id;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;persistent&nbsp;field&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;shortdesc;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;persistent&nbsp;field&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;longdesc;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;persistent&nbsp;field&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;pageurl;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;full&nbsp;constructor&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>Tutorial<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>Integer&nbsp;id,&nbsp;String&nbsp;shortdesc,&nbsp;String&nbsp;longdesc,&nbsp;String&nbsp;pageurl<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.id&nbsp;=&nbsp;id;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.shortdesc&nbsp;=&nbsp;shortdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.longdesc&nbsp;=&nbsp;longdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.pageurl&nbsp;=&nbsp;pageurl;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**&nbsp;default&nbsp;constructor&nbsp;*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>Tutorial<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>Integer&nbsp;getId<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>return&nbsp;this<\/B><\/FONT><FONT color=#000000>.id;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>setId<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>Integer&nbsp;id<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.id&nbsp;=&nbsp;id;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;getShortdesc<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>return&nbsp;this<\/B><\/FONT><FONT color=#000000>.shortdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>setShortdesc<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String&nbsp;shortdesc<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.shortdesc&nbsp;=&nbsp;shortdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;getLongdesc<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>return&nbsp;this<\/B><\/FONT><FONT color=#000000>.longdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>setLongdesc<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String&nbsp;longdesc<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.longdesc&nbsp;=&nbsp;longdesc;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;getPageurl<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>return&nbsp;this<\/B><\/FONT><FONT color=#000000>.pageurl;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>setPageurl<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String&nbsp;pageurl<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>this<\/B><\/FONT><FONT color=#000000>.pageurl&nbsp;=&nbsp;pageurl;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#000000>}<\/FONT><\/CODE> <\/TD><!-- end source code --><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P>In this section we have created all the Hibernate related stuffs.<\/P><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous section we completed the database setup and created required table and populated with the data. In this section we will write required hibernate configuration files. For this tutorial we need following Hibernate configuration files: Hibernate Configuration File Hibernate configuration file (hibernate.cfg.xml) is used to provide the information which is necessary for making &hellip; <a href=\"https:\/\/www.strongd.net\/?p=203\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Writing Hibernate Configuration Files<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}