{"id":2347,"date":"2011-08-08T13:03:43","date_gmt":"2011-08-08T07:33:43","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2347"},"modified":"2011-08-08T13:03:43","modified_gmt":"2011-08-08T07:33:43","slug":"step-by-step-hibernate-tutorial-using-eclipse-wtp","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/step-by-step-hibernate-tutorial-using-eclipse-wtp\/","title":{"rendered":"Step By Step Hibernate Tutorial Using eclipse WTP"},"content":{"rendered":"<p>Hibernate is the ORM tool widely used in java community to persist the java object using Object Relational Mapping (ORM) concept. ORM reduces number of lines to interact with database with optimized query language which is <strong>Hibernate Query language (HQL)<\/strong>.<\/p>\n<p>In this example, we will create a simple login application using hibernate tool of eclipse.\u00a0We will use <strong>eclipse WTP<\/strong> (Web Tools Platform), to install &#8220;Hibernate Tools&#8221;\u009d. Follow below steps :<\/p>\n<p>In Eclipse IDE, menu bar, select <strong>&#8220;Help&#8221;\u009d &gt;&gt; &#8220;Install New Software &#8230;&#8221;\u009d<\/strong> put the Eclipse update site URL <em>&#8220;http:\/\/download.jboss.org\/jbosstools\/updates\/stable\/helios&#8221;<\/em><\/p>\n<figure id=\"attachment_2348\" aria-describedby=\"caption-attachment-2348\" style=\"width: 437px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Install-New-Software-Hibernate.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2348 \" title=\"Eclipse Install New Software - Hibernate\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Install-New-Software-Hibernate.jpg?resize=437%2C186&#038;ssl=1\" alt=\"Eclipse Install New Software - Hibernate\" width=\"437\" height=\"186\" \/><\/a><figcaption id=\"caption-attachment-2348\" class=\"wp-caption-text\">Eclipse Install New Software - Hibernate<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p>Select the tool and click on &#8220;Next&#8221;\u009d. Do not select all the tools; it will install all the unnecessary tools. We just need hibernate tools.<\/p>\n<p>After installation, restart the eclipse.<\/p>\n<p>If you don&#8217;t have the internet connection and want the offline method to add hibernate tools in eclipse. To install the Hibernate Tools, extract the <code>HibernateTools-3.X.zip<\/code> file and move all the files inside the features folder into the features folder of the eclipse installation directory and move all the files inside the plugins folder into the plugins folder of the ecilpse installation directory.<\/p>\n<p>After restart, Go to <strong>Window | Open Perspective | Other<\/strong>, the following dialog box appears, select Hibernate and click the Ok button.<\/p>\n<figure id=\"attachment_2349\" aria-describedby=\"caption-attachment-2349\" style=\"width: 347px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Hibernate-Perspective.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2349\" title=\"Eclipse Hibernate Perspective\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Eclipse-Hibernate-Perspective.jpg?resize=347%2C422&#038;ssl=1\" alt=\"Eclipse Hibernate Perspective\" width=\"347\" height=\"422\" \/><\/a><figcaption id=\"caption-attachment-2349\" class=\"wp-caption-text\">Eclipse Hibernate Perspective<\/figcaption><\/figure>\n<p>Now let&#8217;s see how to define the object\/relational mapping using the XML document. This document has <strong>.hbm.xml<\/strong> extension. We will now create the mapping between table and object of entity &#8220;user&#8221; which hold the data from database. So create the package <code>\"in.shivasoft.pojo\"\u009d<\/code> in src folder.<br \/>\nRight click on project folder and select &#8220;<span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">Hibernate XML Mapping file (hbm.xml)<\/span><span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">&#8220;\u009d.<\/span><\/p>\n<figure id=\"attachment_2351\" aria-describedby=\"caption-attachment-2351\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-XML-Mapping-file-Menu-in-Hibernate-Tools-of-Eclipse.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2351 \" title=\"Hibernate XML Mapping file Menu in Hibernate Tools of Eclipse\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-XML-Mapping-file-Menu-in-Hibernate-Tools-of-Eclipse.png?resize=436%2C106&#038;ssl=1\" alt=\"Hibernate XML Mapping file Menu in Hibernate Tools of Eclipse\" width=\"436\" height=\"106\" \/><\/a><figcaption id=\"caption-attachment-2351\" class=\"wp-caption-text\">Hibernate XML Mapping file Menu in Hibernate Tools of Eclipse<\/figcaption><\/figure>\n<p>Then one popup will appear, click on &#8220;Next&#8221;\u009d.<\/p>\n<figure id=\"attachment_2352\" aria-describedby=\"caption-attachment-2352\" style=\"width: 421px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Create-Hibernate-XML-Mapping-files.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2352 \" title=\"Create Hibernate XML Mapping file(s)\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Create-Hibernate-XML-Mapping-files.png?resize=421%2C453&#038;ssl=1\" alt=\"Create Hibernate XML Mapping file(s)\" width=\"421\" height=\"453\" \/><\/a><figcaption id=\"caption-attachment-2352\" class=\"wp-caption-text\">Create Hibernate XML Mapping file(s)<\/figcaption><\/figure>\n<p>Select the pojo folder and give name &#8220;Users.hbm.xml&#8221;\u009d and click on finish.<\/p>\n<figure id=\"attachment_2353\" aria-describedby=\"caption-attachment-2353\" style=\"width: 423px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/New-Hibernate-XML-Mapping-files-hbm.xml_.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2353 \" title=\"New Hibernate XML Mapping files (hbm.xml)\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/New-Hibernate-XML-Mapping-files-hbm.xml_.png?resize=423%2C450&#038;ssl=1\" alt=\"New Hibernate XML Mapping files (hbm.xml)\" width=\"423\" height=\"450\" \/><\/a><figcaption id=\"caption-attachment-2353\" class=\"wp-caption-text\">New Hibernate XML Mapping files (hbm.xml)<\/figcaption><\/figure>\n<p>Now, either you can configure the settings from the UI like below snap:<\/p>\n<figure id=\"attachment_2355\" aria-describedby=\"caption-attachment-2355\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-3.0-XML-Editor.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2355 \" title=\"Hibernate 3.0 XML Editor\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-3.0-XML-Editor.png?resize=436%2C174&#038;ssl=1\" alt=\"Hibernate 3.0 XML Editor\" width=\"436\" height=\"174\" \/><\/a><figcaption id=\"caption-attachment-2355\" class=\"wp-caption-text\">Hibernate 3.0 XML Editor<\/figcaption><\/figure>\n<p>Or, write below code :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE hibernate-mapping PUBLIC &quot;-\/\/Hibernate\/Hibernate Mapping DTD 3.0\/\/EN&quot; &quot;http:\/\/hibernate.sourceforge.net\/hibernate-mapping-3.0.dtd&quot;&gt;\n&lt;hibernate-mapping&gt;\n &lt;class entity-name=&quot;Users&quot; name=&quot;in.shivasoft.pojo.Users&quot; table=&quot;Users&quot;&gt;\n  &lt;meta attribute=&quot;description&quot;&gt;This class is used to save the info about users&lt;\/meta&gt;\n  &lt;id column=&quot;UserId&quot; name=&quot;UserId&quot; type=&quot;long&quot;\/&gt;\n  &lt;property column=&quot;FName&quot; name=&quot;FName&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;LName&quot;  name=&quot;LName&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;UserTypeId&quot; name=&quot;UserTypeId&quot; type=&quot;long&quot;\/&gt;\n  &lt;property column=&quot;UserName&quot; name=&quot;UserName&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;Email&quot; name=&quot;Email&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;Pwd&quot; name=&quot;Pwd&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;Note&quot; name=&quot;Note&quot; type=&quot;string&quot;\/&gt;\n  &lt;property column=&quot;IsActive&quot; name=&quot;IsActive&quot; type=&quot;boolean&quot;\/&gt;\n &lt;\/class&gt;\n&lt;\/hibernate-mapping&gt;\n<\/pre>\n<p>Now right click and select &#8220;hibernate configuration file&#8221;. One window will open, there select the &#8220;src&#8221;\u009d folder and click on next.<\/p>\n<figure id=\"attachment_2357\" aria-describedby=\"caption-attachment-2357\" style=\"width: 435px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Configuration-File-cfg.xml_.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2357  \" title=\"Hibernate Configuration File (cfg.xml)\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Configuration-File-cfg.xml_.png?resize=435%2C382&#038;ssl=1\" alt=\"Hibernate Configuration File (cfg.xml)\" width=\"435\" height=\"382\" \/><\/a><figcaption id=\"caption-attachment-2357\" class=\"wp-caption-text\">Hibernate Configuration File (cfg.xml)<\/figcaption><\/figure>\n<p>Enter detail like below screen and click on finish button.<\/p>\n<figure id=\"attachment_2359\" aria-describedby=\"caption-attachment-2359\" style=\"width: 437px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Configuration-File-cfg.xml-Wizard.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2359 \" title=\"Hibernate Configuration File (cfg.xml) Wizard\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Configuration-File-cfg.xml-Wizard.png?resize=437%2C382&#038;ssl=1\" alt=\"Hibernate Configuration File (cfg.xml) Wizard\" width=\"437\" height=\"382\" \/><\/a><figcaption id=\"caption-attachment-2359\" class=\"wp-caption-text\">Hibernate Configuration File (cfg.xml) Wizard<\/figcaption><\/figure>\n<p>After this add the Users.hbm.xml file to the newly generated xml configuration.<\/p>\n<pre class=\"brush: xml; highlight: [12]; title: ; notranslate\" title=\"\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;!DOCTYPE hibernate-configuration PUBLIC\n\t\t&quot;-\/\/Hibernate\/Hibernate Configuration DTD 3.0\/\/EN&quot;\n\t\t&quot;http:\/\/hibernate.sourceforge.net\/hibernate-configuration-3.0.dtd&quot;&gt;\n&lt;hibernate-configuration&gt;\n    &lt;session-factory&gt;\n        &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt; org.gjt.mm.mysql.Driver &lt;\/property&gt;\n        &lt;property name=&quot;hibernate.connection.password&quot;&gt; root &lt;\/property&gt;\n        &lt;property name=&quot;hibernate.connection.url&quot;&gt; jdbc:mysql:\/\/localhost\/test &lt;\/property&gt;\n        &lt;property name=&quot;hibernate.connection.username&quot;&gt; root &lt;\/property&gt;\n        &lt;property name=&quot;hibernate.dialect&quot;&gt; org.hibernate.dialect.MySQL5InnoDBDialect &lt;\/property&gt;\n        &lt;mapping resource=&quot;in\/shivasoft\/pojo\/Users.hbm.xml&quot;\/&gt;\n    &lt;\/session-factory&gt;\n&lt;\/hibernate-configuration&gt;\n<\/pre>\n<p>After the entire configuration, in the toolbar you can select &#8220;hibernate code generation tool&#8221;.<\/p>\n<figure id=\"attachment_2361\" aria-describedby=\"caption-attachment-2361\" style=\"width: 325px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Configurations.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2361\" title=\"Hibernate Code Generation Configurations\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Configurations.png?resize=325%2C132&#038;ssl=1\" alt=\"Hibernate Code Generation Configurations\" width=\"325\" height=\"132\" \/><\/a><figcaption id=\"caption-attachment-2361\" class=\"wp-caption-text\">Hibernate Code Generation Configurations<\/figcaption><\/figure>\n<figure id=\"attachment_2362\" aria-describedby=\"caption-attachment-2362\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2362 \" title=\"Hibernate Code Generation Wizard in Eclipse\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse.png?resize=436%2C216&#038;ssl=1\" alt=\"Hibernate Code Generation Wizard in Eclipse\" width=\"436\" height=\"216\" \/><\/a><figcaption id=\"caption-attachment-2362\" class=\"wp-caption-text\">Hibernate Code Generation Wizard in Eclipse<\/figcaption><\/figure>\n<p>Now select the Exporters tab and select &#8220;Use Java 5 Syntax&#8221;\u009d.<\/p>\n<figure id=\"attachment_2363\" aria-describedby=\"caption-attachment-2363\" style=\"width: 418px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse-Exporters-Tab.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2363 \" title=\"Hibernate Code Generation Wizard in Eclipse-Exporters Tab\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse-Exporters-Tab.png?resize=418%2C269&#038;ssl=1\" alt=\"Hibernate Code Generation Wizard in Eclipse-Exporters Tab\" width=\"418\" height=\"269\" \/><\/a><figcaption id=\"caption-attachment-2363\" class=\"wp-caption-text\">Hibernate Code Generation Wizard in Eclipse-Exporters Tab<\/figcaption><\/figure>\n<p>In refresh tab, select &#8220;<span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">The Project containing the selected resource<\/span>&#8220;\u009d.<\/p>\n<figure id=\"attachment_2364\" aria-describedby=\"caption-attachment-2364\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse-Refresh-Tab.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2364 \" title=\"Hibernate Code Generation Wizard in Eclipse- Refresh Tab\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Hibernate-Code-Generation-Wizard-in-Eclipse-Refresh-Tab.png?resize=436%2C239&#038;ssl=1\" alt=\"Hibernate Code Generation Wizard in Eclipse- Refresh Tab\" width=\"436\" height=\"239\" \/><\/a><figcaption id=\"caption-attachment-2364\" class=\"wp-caption-text\">Hibernate Code Generation Wizard in Eclipse- Refresh Tab<\/figcaption><\/figure>\n<p>Now click the run button to finish java code generation. Following code will be generated:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage in.shivasoft.pojo;\n\n\/\/ Generated Aug 1, 2011 7:02:59 PM by Hibernate Tools 3.4.0.CR1\n\n\/**\n * Users generated by hbm2java\n *\/\npublic class Users implements java.io.Serializable {\n\n\tprivate long UserId;\n\tprivate String FName;\n\tprivate String LName;\n\tprivate long UserTypeId;\n\tprivate String UserName;\n\tprivate String Email;\n\tprivate String Pwd;\n\tprivate String Note;\n\tprivate boolean IsActive;\n\n\tpublic Users() {\n\t}\n\n\tpublic Users(long UserId) {\n\t\tthis.UserId = UserId;\n\t}\n\n\tpublic Users(long UserId, String FName, String LName, long UserTypeId,\n\t\t\tString UserName, String Email, String Pwd, String Note,\n\t\t\tboolean IsActive) {\n\t\tthis.UserId = UserId;\n\t\tthis.FName = FName;\n\t\tthis.LName = LName;\n\t\tthis.UserTypeId = UserTypeId;\n\t\tthis.UserName = UserName;\n\t\tthis.Email = Email;\n\t\tthis.Pwd = Pwd;\n\t\tthis.Note = Note;\n\t\tthis.IsActive = IsActive;\n\t}\n\n\tpublic long getUserId() {\n\t\treturn this.UserId;\n\t}\n\n\tpublic void setUserId(long UserId) {\n\t\tthis.UserId = UserId;\n\t}\n\n\tpublic String getFName() {\n\t\treturn this.FName;\n\t}\n\n\tpublic void setFName(String FName) {\n\t\tthis.FName = FName;\n\t}\n\n\tpublic String getLName() {\n\t\treturn this.LName;\n\t}\n\n\tpublic void setLName(String LName) {\n\t\tthis.LName = LName;\n\t}\n\n\tpublic long getUserTypeId() {\n\t\treturn this.UserTypeId;\n\t}\n\n\tpublic void setUserTypeId(long UserTypeId) {\n\t\tthis.UserTypeId = UserTypeId;\n\t}\n\n\tpublic String getUserName() {\n\t\treturn this.UserName;\n\t}\n\n\tpublic void setUserName(String UserName) {\n\t\tthis.UserName = UserName;\n\t}\n\n\tpublic String getEmail() {\n\t\treturn this.Email;\n\t}\n\n\tpublic void setEmail(String Email) {\n\t\tthis.Email = Email;\n\t}\n\n\tpublic String getPwd() {\n\t\treturn this.Pwd;\n\t}\n\n\tpublic void setPwd(String Pwd) {\n\t\tthis.Pwd = Pwd;\n\t}\n\n\tpublic String getNote() {\n\t\treturn this.Note;\n\t}\n\n\tpublic void setNote(String Note) {\n\t\tthis.Note = Note;\n\t}\n\n\tpublic boolean isIsActive() {\n\t\treturn this.IsActive;\n\t}\n\n\tpublic void setIsActive(boolean IsActive) {\n\t\tthis.IsActive = IsActive;\n\t}\n\n}\n<\/pre>\n<p>Now create the<\/p>\n<pre>HibernateUtil<\/pre>\n<p>class. The HibernateUtil class helps in creating the\u00a0<span class=\"Apple-style-span\" style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;\">SessionFactory <\/span>from the Hibernate configuration file. The SessionFactory is threadsafe, so it is not necessary to obtain one for each thread. Here the static singleton pattern is used to instantiate the SessionFactory. The implementation of the HibernateUtil class is shown below.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage in.shivasoft.util;\n\nimport org.hibernate.SessionFactory;\n\nimport org.hibernate.cfg.Configuration;\n\npublic class HibernateUtil {\n\n\tprivate static final SessionFactory sessionFactory;\n\n\tstatic {\n\n\t\ttry {\n\n\t\t\tsessionFactory = new Configuration().configure()\n\t\t\t.buildSessionFactory();\n\n\t\t} catch (Throwable ex) {\n\n\t\t\tSystem.err.println(&quot;Initial SessionFactory creation failed.&quot; + ex);\n\n\t\t\tthrow new ExceptionInInitializerError(ex);\n\n\t\t}\n\n\t}\n\n\tpublic static SessionFactory getSessionFactory() {\n\n\t\treturn sessionFactory;\n\n\t}\n}\n<\/pre>\n<p>Now, create the test application having main method which will demonstrate that how data is saved:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage in.shivasoft.test;\n\nimport in.shivasoft.pojo.Users;\nimport in.shivasoft.util.HibernateUtil;\n\nimport java.util.List;\n\nimport org.hibernate.Session;\nimport org.hibernate.Transaction;\n\npublic class TestMain {\n\n\t\/**\n\t * @param args\n\t *\/\n\tpublic static void main(String&#x5B;] args) {\n\t\tTestMain obj = new TestMain();\n\t\t\/\/obj.saveRecord();\n\t\tobj.updateUser(12);\n\t\tobj.deleteUser(13);\n\t\tobj.getList();\n\t}\n\n\tpublic void saveRecord()\n\t{\n\t\tUsers u = new Users(0, &quot;Jitendra&quot;, &quot;Zaa&quot;, 1, &quot;jitendra.zaa&quot;, &quot;jitendra.zaa@JitendraZaa.com&quot;, &quot;test&quot;, &quot;this is note&quot;, true);\n\t\tSession session = HibernateUtil.getSessionFactory().openSession();\n\t\tTransaction transaction = null;\n\t\ttry\n\t\t{\n\t\t\ttransaction = session.beginTransaction();\n\t\t\tsession.save(u);\n\t\t\ttransaction.commit();\n\t\t\tSystem.out.println(&quot;Data Saved&quot;);\n\t\t}catch(Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}finally{session.close();}\n\n\t}\n\tpublic void deleteUser(long UserId)\n\t{\n\t\tSession session = HibernateUtil.getSessionFactory().openSession();\n\t\tTransaction transaction = null;\n\t\ttry\n\t\t{\n\t\t\ttransaction = session.beginTransaction();\n\t\t\tUsers u = (Users)session.get(Users.class,UserId);\n\t\t\tsession.delete(u);\n\t\t\ttransaction.commit();\n\t\t\tSystem.out.println(&quot;Data Deleted&quot;);\n\t\t}\n\t\tcatch(Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}\n\t\tfinally{\n\t\t\tsession.close();\n\t\t}\n\t}\n\tpublic void updateUser(long UserId)\n\t{\n\t\tSession session = HibernateUtil.getSessionFactory().openSession();\n\t\tTransaction transaction = null;\n\t\ttry\n\t\t{\n\t\t\ttransaction = session.beginTransaction();\n\t\t\tUsers u = (Users)session.get(Users.class,UserId);\n\t\t\tu.setFName(&quot;ShivaSoft&quot;);\n\t\t\ttransaction.commit();\n\t\t\tSystem.out.println(&quot;Data Updated&quot;);\n\t\t}\n\t\tcatch(Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}\n\t\tfinally{\n\t\t\tsession.close();\n\t\t}\n\t}\n\n\tpublic void getList()\n\t{\n\t\tSession session = HibernateUtil.getSessionFactory().openSession();\n\t\tTransaction transaction = null;\n\t\ttry\n\t\t{\n\t\t\ttransaction = session.beginTransaction();\n\t\t\tList&lt;Users&gt; uList = session.createQuery(&quot;from Users&quot;).list();\n\t\t\tfor(Users u : uList)\n\t\t\t{\n\t\t\t\tSystem.out.println(&quot;First Name - &quot;+u.getFName());\n\t\t\t}\n\t\t}\n\t\tcatch(Exception e)\n\t\t{\n\t\t\te.printStackTrace();\n\t\t}\n\t\tfinally{\n\t\t\tsession.close();\n\t\t}\n\t}\n}\n<\/pre>\n<p>Needed jar files :<\/p>\n<blockquote><p>antlr-2.7.6.jar<br \/>\ncommons-collections-3.1.jar<br \/>\ndom4j-1.6.1.jar<br \/>\nhibernate-core.3.3.1.GA.jar<br \/>\njavassist-3.9.0.GA.jar<br \/>\njta-1.1.jar<br \/>\nmysql-connector-java-5.1.1.17-bin.jar<br \/>\nslf4j-api-1.6.1.jar<br \/>\nslf4j-api-1.6.1-tests.jar<br \/>\nslf4j-slf4j-simple-2.0.jar<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Step By Step Hibernate (ORM Tool) Tutorial Using eclipse WTP<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[25,28],"tags":[86,329],"class_list":["post-2347","post","type-post","status-publish","format-standard","hentry","category-hibernate","category-myqsl","tag-eclipse","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3460,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/introduction-hibernate\/","url_meta":{"origin":2347,"position":0},"title":"Introduction to Hibernate","author":"Jitendra","date":"October 1, 2013","format":false,"excerpt":"Post by - Subhash Bhajankar What is the problem that hibernate is trying to solve? \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Let's take an example of simple class that you have in your application. Referring Image 1, we have Employee object in our java class which has six fields. In our running\u2026","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Necessity of Hibernate ","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/problem.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/problem.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/10\/problem.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1543,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/hibernate\/cause-and-solution-of-org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-session-error\/","url_meta":{"origin":2347,"position":1},"title":"Cause and solution of &#8220;org.hibernate.LazyInitializationException: could not initialize proxy &#8211; no Session&#8221; error","author":"Jitendra","date":"February 22, 2011","format":false,"excerpt":"Cause and solution of \"org.hibernate.LazyInitializationException: could not initialize proxy - no Session\" error","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1643,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jsp\/servlet-hibernate-jquery-and-ajax-based-google-like-chat\/","url_meta":{"origin":2347,"position":2},"title":"Servlet, Hibernate, jQuery and Ajax based google like chat","author":"Jitendra","date":"March 14, 2011","format":false,"excerpt":"Servlet, Hibernate, jQuery and Ajax based google like chat with source code","rel":"","context":"In &quot;Hibernate&quot;","block_context":{"text":"Hibernate","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/hibernate\/"},"img":{"alt_text":"Servlet, Hibernate, jQuery and Ajax based google like chat","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Servlet-Hibernate-jQuery-and-Ajax-based-google-like-chat.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3231,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/creating-first-application-in-heroku-using-eclipse\/","url_meta":{"origin":2347,"position":3},"title":"Creating First Application in Heroku using Eclipse","author":"Jitendra","date":"March 3, 2013","format":false,"excerpt":"Hello Readers, in this article we will see step by step procedure to deploy your first (Hello World) program in Heroku. There are two ways, first going through series of Commands of \"Heroku\" and \"GIT\" and other simple and smart way is to use Eclipse Capability with Heroku. To make\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Setting up Credentials for Heroku in Eclipse","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/03\/Setting-up-Credentials-for-Heroku-in-Eclipse.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/03\/Setting-up-Credentials-for-Heroku-in-Eclipse.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/03\/Setting-up-Credentials-for-Heroku-in-Eclipse.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1192,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-custom-object-1-of-n\/","url_meta":{"origin":2347,"position":4},"title":"Step by Step Salesforce Tutorial \u2013 Creating custom object \u2013 1 of 6","author":"Jitendra","date":"October 6, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial - Creating custom object - 1 of n tutorial series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create Custom object Eclipse Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Create-Custom-object-Eclipse-Salesforce-300x290.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1505,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/locate-source-file-on-local-disc-in-eclipse-external-tool\/","url_meta":{"origin":2347,"position":5},"title":"Locate source file on Local Disc in Eclipse &#8211; External tool","author":"Jitendra","date":"February 13, 2011","format":false,"excerpt":"Locate source file on Local Disc in Eclipse - External tool","rel":"","context":"In &quot;Tech Tips&quot;","block_context":{"text":"Tech Tips","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/tips\/"},"img":{"alt_text":"External Tools Configurations Eclipse","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/External-Tools-Configurations-Eclipse.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2347"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2347\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}