<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sanjay Agarwal on Medium]]></title>
        <description><![CDATA[Stories by Sanjay Agarwal on Medium]]></description>
        <link>https://medium.com/@sanjaysanjayagarwal3?source=rss-fe38c74272b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jjyvSm8QPEMoxaafcOmGOA.jpeg</url>
            <title>Stories by Sanjay Agarwal on Medium</title>
            <link>https://medium.com/@sanjaysanjayagarwal3?source=rss-fe38c74272b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:55:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sanjaysanjayagarwal3/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Annotations Support for Primary Key Generation Alg in Hibernate Application :]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/annotations-support-for-primary-key-generation-alg-in-hibernate-application-5f2771ef46e7?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f2771ef46e7</guid>
            <category><![CDATA[key-generation]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[primary-key-sql]]></category>
            <category><![CDATA[annotations]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 06:05:41 GMT</pubDate>
            <atom:updated>2024-10-04T06:05:41.172Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Annotations Support for Primary Key Generation Alg in Hibernate Application :</strong></h3><p>Hibernate has provided annotations for almost all the primary key generation alg in the form of javax.persistence package and org.hibernate.annotation package.</p><p>To provide annotations for primary key generation alg javax.persistence package has provided the following annotations.</p><p>@GeneratedValue( — , — )</p><p>@SequenceGenerator( — )</p><p>@TableGenerator( — )</p><p>— -</p><p>— -</p><p>To provide a particular primary key generation alg in hibernate applications, we have to use “strategy” member in @GeneratedValue(-) annotation, it will take either of the following constants from “GenerationType” Enum.</p><p>1. IDENTITY</p><p>2. SEQUENCE</p><p>3. TABLE</p><p>4. AUTO</p><h3><strong>1. IDENTITY:</strong></h3><p>This value of GenerationType enum will represent IdentityGenerator or “identity” primary key generation algorithm to generate primary key value on the basis of the underlying database provided identity column.</p><h3><strong>EX: Employee.java</strong></h3><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>@GeneratedValue(strategy=GenerationType.IDENTITY)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>setXXX() and getXXX()<br>}</pre><p><strong>In MySQL:</strong></p><pre>drop table emp1;<br>create table emp1<br>(<br>eno int primary key auto_increment,<br>ename varchar(10),<br>esal float,<br>eaddr varchar(10)<br>);<br>Select * from emp1;</pre><p>In Oracle</p><pre>create table emp1<br>(<br>eno number GENERATED BY DEFAULT ON NULL AS IDENTITY,<br>ename varchar2(10),<br>esal float,<br>eaddr varchar2(10)<br>);</pre><p>Update the following line in the cfg file.</p><pre>&lt;mapping class=&quot;com.ccteam.app15.entities.Employee /&gt;</pre><h3><strong>2. SEQUENCE :</strong></h3><p>This constant from GenerationType enum is able to represent SequenceGenerator or “sequence” primary key generation algorithm inorder to generate primary key value on the basis of the sequence which we defined in database.</p><p>To configure “sequence” name we have to use “@SequenceGenerator” annotation with the following members.</p><p><strong>1. name: </strong>It will take logical name of the @SequenceGenerator.</p><p><strong>2. sequenceName: </strong>it will take “sequence” name provided by underlying database.</p><p>To apply @SequenceGenerator to @GeneratedValue annotation we have to use “generator” member in “@GeneratedValue” annotation.</p><h3><strong>EX: Employee.java</strong></h3><pre>@Entity<br>@Table(name=&quot;emp11&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>@SequenceGenerator(name=&quot;seqGen&quot;, sequenceName=&quot;my_seq&quot;)<br>@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;seqGen&quot;)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>setXXX() and getXXX()<br>}</pre><p><strong>Note:</strong></p><p>In the above example , if we remove @SequenceGenerator annotation and “generator” member in @GeneratedValue annotation then Hibernate software will search for the default sequence name like “<strong>hibernate_sequence</strong>” in the database.</p><h3><strong>3. TABLE:</strong></h3><p>This constant from GenerationType enum is able to represent <strong>HiLo</strong> Primary key generation alg inorder to generate primary key values on the basis of Global resource.</p><p>To configure Global resource table and its properties we have to use @TableGenerator() annotation with the following members.</p><p><strong>1. name: </strong>it will take logical name to the @TableGenerator</p><p><strong>2. pkColumnName: </strong>it will take primary key column name of the Global resource table.</p><p><strong>3. pkColumnValue: </strong>It will take initial value to the primary key column in global resource table.</p><p><strong>4. valueColumnName: </strong>it will take a column name which is generating our required primary key value.</p><p><strong>5. table: </strong>it will take Global resource table name.</p><p>To apply @TableGenerator annotation to @GeneratedValue annotation we have to use “generator” member in @GeneratedValue annotation.</p><p><strong>In Oracle:</strong></p><h3><strong>EX: Employee.java</strong></h3><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>@TableGenerator(name=&quot;tableGen&quot;, table=&quot;my_table&quot;, pkColumnName=&quot;id&quot;,<br>pkColumnValue=&quot;10&quot;, valueColumnName=&quot;next_hi&quot;)<br>@GeneratedValue(strategy=GenerationType.TABLE, generator=&quot;tableGen&quot;)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>setXXX() and getXXX()<br>}</pre><h3><strong>4. AUTO:</strong></h3><p>This constant from GenerationType enum is able to represent “native” primary key generation alg inorder to generate primary key value by selecting either sequence or identity or hilo primary key generation algorithms on the basis of the under lying database.</p><p><strong>Note:</strong></p><p>Depending on the database or depending on the internal primary key generation alg we have to provide input parameters by using @SequenceGenerator() or @TableGenerator() annotations.</p><h3><strong>EX: Employee.java</strong></h3><pre>1. @Entity<br>2. @Table(name=&quot;emp1&quot;)<br>3. public class Employee {<br>4. @Id<br>5. @Column(name=&quot;ENO&quot;)<br>6. @SequenceGenerator(name=&quot;seqGen&quot;, sequenceName=&quot;my_seq&quot;)<br>7. @GeneratedValue(strategy=GenerationType.AUTO, generator=&quot;seqGen&quot;)<br>8. private int eno;<br>9. @Column(name=&quot;ENAME&quot;)<br>10. private String ename;<br>11. @Column(name=&quot;ESAL&quot;)<br>12. private float esal;<br>13. @Column(name=&quot;EADDR&quot;)<br>14. private String eaddr;<br>15. setXXX() and getXXX()<br>16. }</pre><p>It will work in oracle.</p><p>But if you want to execute in mysql then you have to create one table</p><p><strong>IN MySQL database:</strong></p><pre>use college;<br>create table emp1<br>(<br>ENO int(3) primary key auto_increment,<br>ENAME char(10),<br>ESAL float,<br>EADDR char(10)<br>);</pre><p><strong>Note:</strong></p><p>For the remaining primary key generation alg , Hibernate has provided the following annotation in org.hibernate.annotation package.</p><p>@GenericGenerator(name=” — “, strategy=” — “,parameters={…})</p><p>@GeneratedValue(generator=” — “)</p><p>1. name: It will take logical name to @GenericGenerator</p><p>2. strategy: It will take short name of the primary key generation alg.</p><p>3. parameters: It will take array of @Parameter() with the following members to declare parameter values.</p><p>1. name: it will take param name.</p><p>2. value: It will take param value.</p><h3><strong>EX:Employee.java</strong></h3><pre>1. @Entity<br>2. @Table(name=&quot;emp1&quot;)<br>3. public class Employee{<br>4. @Id<br>5. @Column(name=&quot;ENO&quot;)<br>6. @GenericGenerator(name=&quot;incGen&quot; strategy=&quot;increment&quot;)<br>7. @GeneratedValue(generator=&quot;incGen&quot;)<br>8. private int eno;<br>9. - - <br>10. - - <br>11. }<br>EXAMPLE:<br>Employee.java<br>1. package com.ccteam.app08.pojo;<br>2.<br>3. import javax.persistence.Column;<br>4. import javax.persistence.Entity;<br>5. import javax.persistence.GeneratedValue;<br>6. import javax.persistence.GenerationType;<br>7. import javax.persistence.Id;<br>8. import javax.persistence.SequenceGenerator;<br>9. import javax.persistence.Table;<br>10. import javax.persistence.TableGenerator;<br>11.<br>12. import org.hibernate.annotations.Generated;<br>13. import org.hibernate.annotations.GenericGenerator;<br>14. import org.hibernate.annotations.Parameter;<br>15.<br>16. @Entity<br>17. @Table(name=&quot;emp1&quot;)<br>18. public class Employee {<br>19. @Id<br>20. @Column(name=&quot;ENO&quot;)<br>21. /*<br>22. @SequenceGenerator(name=&quot;seqGen&quot;, sequenceName=&quot;my_sequence&quot;)<br>23. @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;seqGen&quot;)<br>24. */<br>25. //@GeneratedValue(strategy=GenerationType.IDENTITY)<br>26. //@GeneratedValue(strategy=GenerationType.AUTO)<br>27. /*<br>28. @TableGenerator(name=&quot;tableGen&quot;, table=&quot;my_table&quot;, pkColumnName=&quot;id&quot;, pkColumnValue=&quot;10&quot;, valueColumnName=&quot;next_hi&quot;)<br>29. @GeneratedValue(strategy=GenerationType.TABLE, generator=&quot;tableGen&quot;)<br>30. */<br>31. @GenericGenerator(name=&quot;incrementGen&quot;, strategy=&quot;increment&quot;)<br>32. @GeneratedValue(generator=&quot;incrementGen&quot;)<br>33.<br>34. private int eno;<br>35. @Column(name=&quot;ENAME&quot;)<br>36. private String ename;<br>37. @Column(name=&quot;ESAL&quot;)<br>38. private float esal;<br>39. @Column(name=&quot;EADDR&quot;)<br>40. private String eaddr;<br>41.<br>42. public int getEno() {<br>43. return eno;<br>44. }<br>45. public void setEno(int eno) {<br>46. this.eno = eno;<br>47. }<br>48. public String getEname() {<br>49. return ename;<br>50. }<br>51. public void setEname(String ename) {<br>52. this.ename = ename;<br>53. }<br>54. public float getEsal() {<br>55. return esal;<br>56. }<br>57. public void setEsal(float esal) {<br>58. this.esal = esal;<br>59. }<br>60. public String getEaddr() {<br>61. return eaddr;<br>62. }<br>63. public void setEaddr(String eaddr) {<br>64. this.eaddr = eaddr;<br>65. }<br>66.<br>67. }</pre><p><strong>hibernate.cfg.xml</strong></p><pre>1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>2. &lt;!DOCTYPE hibernate-configuration PUBLIC<br>3. &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>4. &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>5. &lt;hibernate-configuration&gt;<br>6.<br>7. &lt;session-factory&gt;<br>8. &lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>9. &lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>10. &lt;property name=&quot;connection.username&quot;&gt;system&lt;/property&gt;<br>11. &lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>12. &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.Oracle10gDialect&lt;/property&gt;<br>13. &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>14. &lt;mapping class=&quot;com.ccteam.hbn.pojo.Employee&quot;/&gt;<br>15. &lt;/session-factory&gt;<br>16.<br>17. &lt;! - <br>18. &lt;session-factory&gt;<br>19. &lt;property name=&quot;connection.driver_Class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;<br>20. &lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/college&lt;/property&gt;<br>21. &lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt;<br>22. &lt;property name=&quot;connection.password&quot;&gt;mysql&lt;/property&gt;<br>23. &lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;<br>24. &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>25. &lt;mapping class=&quot;com.ccteam.hbn.pojo.Employee&quot;/&gt;<br>26. &lt;/session-factory&gt;<br>27. →<br>28. &lt;/hibernate-configuration&gt;</pre><h4><strong>ClientApp.java</strong></h4><pre>package com.ccteam.hbn.test;<br>import org.hibernate.Session;<br>import org.hibernate.SessionFactory;<br>import org.hibernate.Transaction;<br>import org.hibernate.boot.registry.StandardServiceRegistry;<br>import org.hibernate.boot.registry.StandardServiceRegistryBuilder;<br>import org.hibernate.cfg.Configuration;<br>import com.ccteam.pojo.Employee;<br>public class Test<br>{<br>public static void main(String[] args)throws Exception<br>{<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();<br>builder = builder.applySettings(cfg.getProperties());<br>StandardServiceRegistry registry = builder.build();<br>SessionFactory sessionFactory = cfg.buildSessionFactory(registry);<br>Session session = sessionFactory.openSession();<br>Employee emp = new Employee();<br>//emp.setEno(111);<br>emp.setEname(&quot;AAA&quot;);<br>emp.setEsal(5000);<br>emp.setEaddr(&quot;AGR&quot;);<br>Transaction tx = session.beginTransaction();<br>session.save(emp);<br>tx.commit();<br>System.out.println(&quot;Employee Inserted Successfully&quot;);<br>}<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f2771ef46e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Primary Key Generation Algorithms in Hibernate]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/primary-key-generation-algorithms-in-hibernate-05e4761e32eb?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/05e4761e32eb</guid>
            <category><![CDATA[primary-key-sql]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[assigned]]></category>
            <category><![CDATA[sequence]]></category>
            <category><![CDATA[generator]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 05:53:36 GMT</pubDate>
            <atom:updated>2024-10-04T05:53:36.144Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Primary Key Generation Algorithms in Hibernate</strong></p><p>Primary key is single column or Collection of columns in a table to recognize the records individually.</p><p>In Database applications, to perform the database operations like retrieving a record, updating a record, deleting a record, we need primary key and its value.</p><p>In Database applications, we are unable to give option to the users to enter primary key values, because, there is no guarantee for the data entered by the users whether it is unique data or not, but, in Database tables only unique values are accepted by primary key columns.</p><p>To give guarantee for uniqueness in primary key values we have to use Primary key generation algorithms.</p><p>Almost all the Persistence mechanisms like Hibernate, JPA, Open JPA, Toplink,…. are having their own implementation for primary key generation algorithms.</p><p>Hibernate has provided support for the following primary key generation algorithms in order to generate primary key values.</p><pre>1. assigned<br>2. increment<br>3. sequence<br>4. identity<br>5. hilo<br>6. native.<br>7. seq-hilo<br>8. select<br>9. UUID<br>10. GUID<br>11. foreign</pre><p>Hibernate has represented all the above primary key generation algorithms in the form of a set of predefined classes provided in <strong>“org.hiberante.id”</strong> package.</p><p>If we want to use any of the above algorithms in Hibernate applications then we have to configure that algorithms in hibernate mapping file by using the following tags.</p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot; - &quot; table=&quot; - &quot;&gt;<br>&lt;id name=&quot; - &quot; column=&quot; - &quot;&gt;<br>&lt;generator class=&quot; - &quot;&gt;<br>&lt;param name=&quot; - &quot;&gt;value&lt;/param&gt;<br> - - <br>&lt;/generator&gt;<br>&lt;/id&gt;<br> - - -<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><p>1)Where &lt;generator&gt; tag will represent a particular primary key generation algorithm.</p><p>2) Where “class” attribute in &lt;generator&gt; tag will take either short name or Fully qualified name of Generator class.</p><p>3)Where &lt;param&gt; tag will provide single input parameter to the Primary key generator inorder to generate primary key value.</p><p>4) Where “name” attribute in &lt;param&gt; tag will take parameter name , here we have to provide parameter value as body to the &lt;param&gt; tag.</p><h3><strong>1. assigned</strong></h3><p>This algorithm is default primary key generation algorithm in hibernate applications, it will not required configurations in mapping file.</p><p>This algorithm will not have its own mechanism to generate primary key value , it will request to Client Application to provide primary key value explicitly.</p><p>This algorithm is able to support for any type of primary key values like short, int, long, String,…..</p><p>This alg is supported by almost all the databases like Oracle, MySQL, DB2, ……</p><p>This alg is not required any input parameter.</p><p>This alg is represented by Hibernate in the form of a predefined class <strong>“org.hibernate.id.Assigned”</strong></p><p><strong>EX:</strong></p><pre>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;assigned&quot;/&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;</pre><h3><strong>Project Name -[app09]</strong></h3><pre>Create the Mavenized Project<br>And do some updation in pom.xml file.<br>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>Add one Hibernate core &amp; Database Dependency in pom file.<br>&lt;! -  https://mvnrepository.com/artifact/org.hibernate/hibernate-core →<br>&lt;dependency&gt;<br>&lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br>&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;<br>&lt;version&gt;4.3.11.Final&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc11 →<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;<br>&lt;artifactId&gt;ojdbc11&lt;/artifactId&gt;<br>&lt;version&gt;21.8.0.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p><strong>Create one sub package [</strong>com.ccteam.app09.pojo]</p><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.app09.pojo;<br>public class Employee {<br>private int eno;<br>private String ename;<br>private float esal;<br>private String eaddr;<br>public int getEno() {<br>return eno;<br>}<br>public void setEno(int eno) {<br>this.eno = eno;<br>}<br>public String getEname() {<br>return ename;<br>}<br>public void setEname(String ename) {<br>this.ename = ename;<br>}<br>public float getEsal() {<br>return esal;<br>}<br>public void setEsal(float esal) {<br>this.esal = esal;<br>}<br>public String getEaddr() {<br>return eaddr;<br>}<br>public void setEaddr(String eaddr) {<br>this.eaddr = eaddr;<br>}<br>}</pre><h4><strong>Create one resources source folder.</strong></h4><h3><strong>Employee.hbm.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.app09.pojo.Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;assigned&quot; /&gt;<br>&lt;/id&gt;<br>&lt;property name=&quot;ename&quot; column=&quot;ENAME&quot; /&gt;<br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot; /&gt;<br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot; /&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>hibernate.cfg.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.username&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;mapping resource=&quot;Employee.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>public class ClientApp {<br>public static void main(String[] args)throws Exception {<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();<br>builder = builder.applySettings(cfg.getProperties());<br>StandardServiceRegistry registry = builder.build();<br>SessionFactory sessionFactory = cfg.buildSessionFactory(registry);<br>Session session = sessionFactory.openSession();<br>Employee emp = new Employee();<br>emp.setEno(111);<br>emp.setEname(&quot;AAA&quot;);<br>emp.setEsal(5000);<br>emp.setEaddr(&quot;AGR&quot;);<br>Transaction transaction=session.beginTransaction();<br>session.save(employee);<br>transaction.commit();<br>System.out.println(&quot;Employee Inserted Success Fully&quot;);<br>session.close();<br>sessionFactory.close();<br>}<br>}</pre><h4><strong>And Run the Application.</strong></h4><p><strong>Note: Before running the project delete the all content from the table then run the program.</strong></p><h3><strong>2. increment</strong></h3><p>This primary key generation algorithm is able to generate primary key value by incrementing max value of the primary key column.</p><p>New_Val = max(PK_Column)+1</p><p>This alg is able to generate primary key values of the data types like short, int, long,…</p><p>This alg is not required any input parameter to generate primary key values.</p><p>This alg is supported by almost all the databases which are supporting numeric values as Primary key values.</p><p>This alg is representned by Hibernate Software in the form of a short name “increment” and in the form of a predefined class like “<strong>org.hibernate.id.IncrementGenerator</strong>”.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;org.hibernate.id.IncrementGenerator&quot;/&gt;<br>Or<br>&lt;generator class=&quot;increment&quot;/&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;<br>Please Type this in ClientApp.<br>Employee emp1 = new Employee();<br>//emp1.setEno(1);<br>emp1.setEname(&quot;AAA&quot;);<br>emp1.setEsal(8000);<br>emp1.setEaddr(&quot;AGRA&quot;);<br>Transaction tx = session.beginTransaction();<br>session.save(emp1);<br>tx.commit();<br>System.out.println(&quot;Employee Inserted Successfully&quot;);</pre><h3><strong>3. sequence:</strong></h3><p>→ This primary key generation algorithm is able to generate primary key value on the basis of the sequence provided by the underlying Database.</p><p>→ This alg is able to generate primary key values of the data types like short, int, long,…</p><p>→ This alg required “sequence” input parameter with the sequence name as value in order to generate primary key value.</p><p>→ If we have not provided any sequence name as input parameter then this alg is able to take “<strong>hibernate_sequence</strong>” as default sequence name, here developers must create “hibernate_sequence” in database explicitly.</p><p>→ This alg is supported by almost all the databases which are supporting sequences like Oracle, DB2,….</p><p>→ This alg is represented by Hibernate Software in the form of a short name like “sequence” and in the form of a predefined class like</p><p><strong>“org.hibernate.id.SequenceGenerator”</strong></p><p><strong>EX:</strong></p><p><strong>In Oracle Database:</strong></p><pre>SQL&gt;create sequence my_sequence increment by 5;<br>SQL&gt;commit;<br>In mapping File:<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;sequence&quot;&gt;<br>&lt;param name=&quot;sequence&quot;&gt;my_sequence&lt;/param&gt;<br>&lt;/generator&gt;<br>&lt;/id&gt;<br> - - -<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;<br>Note:<br>If we dont want to use explicit sequence then we must create default sequence in<br>database , that is, &quot;hibernate_sequence&quot;.<br>SQL&gt;create sequence hibernate_sequence increment by 5;</pre><h3><strong>4. identity</strong></h3><p>This alg is able to generate primary key values on the basis on the underlying database table provided identity column.</p><p><strong>Note:</strong></p><p>Identity column is a primary key Column with “auto_increment” capability.</p><p>This alg is able to provide the primary key values of the data types like short, int, long,….</p><p>This alg is not required any input parameter.</p><p>This alg is supported by almost all the databases which are supporting Identity column.</p><p><strong>EX: </strong>MySQL.</p><p>To represent this alg, Hibernate has provided “identity” as short name and “org.hibernate.id.<strong>IdentityGenerator</strong>” as predefined class.</p><p><strong>EX:</strong></p><pre>IN MySQL database:<br>use college;<br>create table emp1<br>(<br>ENO int(3) primary key auto_increment,<br>ENAME char(10),<br>ESAL float,<br>EADDR char(10)<br>);</pre><p><strong>IN Mapping File:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp10&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;identity&quot;/&gt;<br>&lt;/id&gt;<br>&lt;/class&gt;<br> - - -<br>&lt;/hibernate-mapping&gt;</pre><p><strong>[hibernate.cfg.xml]</strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/college&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;root&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;mysql&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;<br>&lt;mapping resource=&quot;Employee.hbm.xml&quot;/&gt;<br>&lt;! - &lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.username&quot;&gt;system&lt;/property&gt; &lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt; &lt;mapping resource=&quot;Employee.hbm.xml&quot;/&gt;<br> →<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>5. hilo[High-Low]</strong></h3><p>This alg is able to generate primary key value by getting high value from a particular global resource and low value from mapping file.</p><p><strong>Note:</strong></p><p>Global resource is a particular table with a column in the same database.</p><p>This alg is able to generate primary key values of the data types like short, int, long,….</p><p>To represent this alg. Hibernate software has provides a separate short in the form of “hilo” and a seperate predefined class in the form of “org.hibernate.id.TableHiLoGenerator”.</p><p>This alg is supported by almost all the databases like Oracle, MySQL,….</p><p>To generate primary key value this alg will take the following three parameters.</p><p>1. table: Global resource name , that is, a table name which provides high value.</p><p>2. column:column name existed in Global Resource to manage high value.</p><p>3. max_lo:It will provide low value .</p><p><strong>Note:</strong></p><p>If we have not provided Global resources parameters like table and column in mapping file then Hibernate Software will take high value from default global resource table “hibernate_unique_key” and column “next_hi”.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;hilo&quot;&gt;<br>&lt;param name=&quot;table&quot;&gt;my_table&lt;/param&gt;<br>&lt;param name=&quot;column&quot;&gt;my_column&lt;/param&gt;<br>&lt;param name=&quot;max_lo&quot;&gt;10&lt;/param&gt;<br>&lt;/generator&gt;<br>&lt;/id&gt;<br>- - <br>&lt;/class&gt;<br><br>&lt;/hibernate-mapping&gt;</pre><p><strong>In Oracle Database:</strong></p><pre>SQL&gt; create table my_table(my_column number primary key);<br>SQL&gt; insert into my_table values(100);<br>SQL&gt; commit;</pre><p><strong>Note:</strong></p><p>If we want to use <strong>default table name and column</strong> instead of <strong>my_table and my_column</strong> then we have to remove <strong>“table”</strong> and <strong>“column”</strong> parameters in mapping file and provide the following in Database.</p><pre>SQL&gt;create table hibernate_unique_key(next_hi number primary key);<br>SQL&gt;insert into hibernate_unique_key values(100);<br>SQL&gt;commit;</pre><h3><strong>6. native:</strong></h3><p>This alg is able to generate primary key value by selecting a particular primary key generation alg depending on the database which we used.</p><p>This alg is not having its own alg to generate primary key value. It will select “<strong>SequenceGenerator</strong>” alg if we are using Oracle database, “<strong>IdentityGenerator</strong>” alg if we are using MySQL database and “TableHiLoGenerator” if we are using some other database which is not supporting SequenceGenerator and IdentityGenerator.</p><p>This Primary key generator is able to generate primary key values of the data types like short, int, long,…</p><p>This primary key generator is supported by almost all the databases.</p><p>To represent this mechanism, Hibernate has provided a short name in the form of “<strong>native</strong>”, Hibernate has not provided any predefined class.</p><p>This alg is able to take parameters on the basis of the selected primary key generator depending on the database. If it is using “SequenceGenerator” then we must provide “sequence” parameter.</p><p>If it is using TableHiloGenerator then we have to provide “table”, “column”, “max_lo” parameters.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;native&quot;&gt;<br> - -provide parameters depending on requirement - -<br>&lt;param name=&quot;sequence&quot;&gt;mysequence&lt;/param&gt;<br>&lt;/generator&gt;<br>&lt;/id&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><p>In Oracle Database:</p><pre>SQL&gt; CREATE SEQUENCE mysequence<br>INCREMENT BY 1;</pre><p><strong>In MySQl Database:</strong></p><pre>Create Sequence Using AUTO_INCREMENT<br>The simplest way for creating a sequence is by defining the column<br>as AUTO_INCREMENT during table creation, which should be a primary<br>key column.<br>&lt;generator class=&quot;native&quot;&gt;<br>&lt;/generator&gt;</pre><p><strong>In the case of mysql no need put paramerter within generator.</strong></p><pre>create table emp11<br>(<br>ENO int(3) primary key auto_increment,<br>ENAME varchar(10),<br>ESAL float,<br>EADDR varchar(10)<br>);</pre><h3><strong>7. seq-hilo:</strong></h3><p>→This alg is able to generate primary key values on the basis of the sequence value provided by the underlying database and the mapping file provided max_lo property value.</p><p>→This alg is almost all same as HiLo alg but in “seqhilo” alg we will get sequence value inplace of the high value from Global resource.</p><p>→ This alg is able to generate primary key values of the data types like short, int and long.</p><p>→This alg is represented by Hibernate software in the form a short name like “seqhilo” and a predefined class name like “org.hibernate.id.SequenceHiLoGenerator”.</p><p>→This alg is supported by almost all the databases which are supporting sequences internally.</p><p>→This alg is required the following two input parameters.</p><p>1. <strong>sequence</strong>: It will take sequence name provided by Database.</p><p>2. <strong>max_lo</strong> : It will provide low value.</p><p><strong>Note:</strong></p><p>If we have not provided “sequence” input parameter then hibernate software will search for the default sequence name like <strong>“hibernate_sequence”.</strong></p><p><strong>Execute this code in oracle database</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;seqhilo&quot;&gt;<br>&lt;param name=&quot;sequence&quot;&gt;my_sequence&lt;/param&gt;<br>&lt;param name=&quot;max_lo&quot;&gt;1&lt;/param&gt;<br>&lt;/generator&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>8. select</strong></h3><p>→This alg is able to generate primary key values on the basis of the underlying database provided triggers.</p><p>→This alg is able to provide primary key values of the data types like short, int and long.</p><p>→This alg is represented by Hibernate software in the form of a short name like <strong>“select”</strong> and a predefined class name “<strong>org.hibernate.id.SelectGenerator</strong>”.</p><p>→This alg may take input parameters which are related to triggers.</p><p>→This alg is supported by almost all the databases which are supporting triggers.</p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;select&quot;&gt;<br> - - parameters related to triggers - - <br>&lt;/generator&gt;<br>&lt;/id&gt;<br> - - -<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>9. UUID[Universal Unique Identity]</strong></h3><p>→This alg is able to generate primary key value on the basis of System IP-Address , JVM Startup time, Current System time,…. factors.</p><p>→This alg is able to generate primary key value in the form of 32 bit hexa decimal value.</p><p>→This alg is able to generate primary key value of the data type like String.</p><p>→This alg is represented by Hibernate software in the form of a short name “uuid” and a predefined class name “org.hibernate.id.UUIDHexGenerator”.</p><p>→This alg is supported by almost all the databases which are supporting String type primary key values.</p><p>→This alg is not required any input parameters, but, it must required “varchar” type primary key column with 32 column size minimum in database table.</p><p><strong>Note:</strong></p><p><strong>In Oracle Database</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot; type=&quot;string&quot;&gt;<br>&lt;generator class=&quot;org.hibernate.id.UUIDHexGenerator&quot;/&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><p><strong>Do some data type changes in pojo class.</strong></p><h3><strong>10. GUID:</strong></h3><p>→This alg is able to generate primary key values on the basis of the underlying database provided identity column <strong>with String type</strong>.</p><p>→This alg is represented in the form of a predefined class org.hibernate.id.GUIDGenerator and in the form of a short name like “<strong>guid</strong>”.</p><p>→This alg is not requires any input parameters to generate primary values.</p><p>→This alg is supported by the databases which are supporting string type id columns.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;org.hibernate.id.GUIDGenerator&quot;/&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>11. foreign :</strong></h3><p>→This primary key generation alg is able to generate primary key values on the basis of the foreign key of the present table.</p><p>→This alg is able to generate primary key values of the data types like short, int and long.</p><p>→This alg is not required any input parameters to generate primary key values.</p><p>→This alg is represented by Hibernate software in the form of a short name “foreign” and in the form of a predefined class like “org.hibernate.id.<strong>ForeignGenerator</strong>”.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;&gt;<br>&lt;generator class=&quot;org.hibernate.id.ForeignGenerator&quot;/&gt;<br>&lt;/id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=05e4761e32eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Program_Hibernate4.x version:-]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/program-hibernate4-x-version-fa7be03fb9b1?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa7be03fb9b1</guid>
            <category><![CDATA[hibernate-tutorial]]></category>
            <category><![CDATA[database-development]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[hibernate-jpa]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 05:23:13 GMT</pubDate>
            <atom:updated>2024-10-04T05:23:13.750Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Creating SessionFactory object in Hibernate4.x version:</strong></p><p>To improve Performance of the Hibernate booting Hibernate 4.x version has provided a separate approach to prepare SessionFactory.</p><p>In Hibernate4.x version, cfg.buildSessionFactory() method was deprecated, because, to load hibernate software and to setup Hibernate environment it may take lot of time. To simplify Hibernate Software loading and to set up Hibernate Environment in simplified manner, Hibernate4.x version has included some booting features.</p><p>In Hibernate4.x version, to create SessionFactory object we have to use the following steps.</p><pre>a) Create Configuration Object with all configuration details:<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>b) Get all configuration details into Properties object:<br>public Properties getProperties()<br>EX:<br>Properties p = cfg.getProperties();<br>c) Creating StandardServiceRegistryBuilder:<br>StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();<br>d) Get All configuration details from Properties object into<br>StandardServiceRegistryBuilder object:<br>public StandardServiceRegistryBuilder applySettings(Map m)<br>EX:<br>builder = builder.applySettings(p);<br>e) Create StandardServiceRegistry object:<br>public StandardServiceRegistry build()<br>EX:<br>StandardServiceRegistry registry = builder.build();<br>f) Create SessionFactory object:<br>public SessionFactory buildSessionFactory(StandardServiceRegistry registry)<br>Ex:<br>Sessionfactory sf = cfg.buildSessionfactory(registry);</pre><h3><strong>Project Name :app08</strong></h3><pre>Create the Mavenized Project<br>And do some updation in pom.xml file.<br>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>Add one Hibernate core &amp; Database Dependency in pom file.<br>&lt;! -  https://mvnrepository.com/artifact/org.hibernate/hibernate-core →<br>&lt;dependency&gt;<br>&lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br>&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;<br>&lt;version&gt;4.3.11.Final&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc11 →<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;<br>&lt;artifactId&gt;ojdbc11&lt;/artifactId&gt;<br>&lt;version&gt;21.8.0.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.app08.pojo;<br>public class Employee {<br>private int eno;<br>private String ename;<br>private float esal;<br>private String eaddr;<br>getXXX()<br>setXXX()<br>}</pre><p><strong>After that create one source folder [resources] and in that create mapping and configuration file.</strong></p><h3><strong>Employee.hbm.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.app08.pojo.Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot;/&gt;<br>&lt;property name=&quot;ename&quot;/&gt;<br>&lt;property name=&quot;esal&quot;/&gt;<br>&lt;property name=&quot;eaddr&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>hibernate.cfg.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.username&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;mapping resource=&quot;Employee.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>public class ClientApp {<br>public static void main(String[] args)throws Exception {<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();<br>builder = builder.applySettings(cfg.getProperties());<br>StandardServiceRegistry registry = builder.build();<br>SessionFactory sessionFactory = cfg.buildSessionFactory(registry);<br>Session session = sessionFactory.openSession();<br>Employee emp = (Employee)session.get(&quot;com.ccteam.app08.pojo.Employee&quot;, 111);<br>if(emp == null) {<br>System.out.println(&quot;Emnployee Not Existed&quot;);<br>}<br>else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot; - - - - - - - - - - -&quot;);<br>System.out.println(&quot;Employee Number :&quot;+emp.getEno());<br>System.out.println(&quot;Employee Name :&quot;+emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot;+emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot;+emp.getEaddr());<br>}<br>session.close();<br>sessionFactory.close();<br>}<br>}</pre><h4><strong>And Run the Application.</strong></h4><h3>To create a table in Database:</h3><pre>Create Database College;<br>To Activate the Database <br>  =&gt; Use College;<br>Then Create the table by using the following command.<br>Ex:<br>create table Emp1<br>(<br>ENO int primary key,<br>EName varchar(10),<br>ESal float,<br>EAddr varchar(10)<br>);<br><br>insert into emp1 <br>values(101,&#39;DD Singh&#39;,5000,&#39;Agra&#39;);<br><br>Select * from emp1;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa7be03fb9b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hibernate Persistence Object Lifecycle:]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/hibernate-persistence-object-lifecycle-ca06de8b34d1?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca06de8b34d1</guid>
            <category><![CDATA[schema-update]]></category>
            <category><![CDATA[code-generation]]></category>
            <category><![CDATA[schema-export]]></category>
            <category><![CDATA[hibernate-persistence]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 05:10:25 GMT</pubDate>
            <atom:updated>2024-10-04T05:10:25.948Z</atom:updated>
            <content:encoded><![CDATA[<p>The collective information of any object right from starting point to end point is called as Object Lifecycle.</p><p>In Hibernate Applications, Persistence object is having the following lifecycle states.</p><p><strong>1. Transient State</strong></p><p><strong>2. Persistence State</strong></p><p><strong>3. Detached State</strong></p><p><strong>4. Removed State</strong></p><h3><strong>1. Transient State</strong></h3><p>In Hibernate Applications, when we create POJO object then automatically POJO object will come to Transient State.</p><p>If POJO object is available in Transient State then Hibernate Software is not aware about POJO object.</p><p>If POJO object is available in Transient State then POJO object is representing any record in Database table.</p><p>In this state, Hibernate Software is unable to provide synchronization between POJO object and record in Database.</p><p>In this state, if we perform modifications in POJO object then that modifications are not reflected to any record in Database.</p><p>If we keep POJO object in Transient State for long time then POJO object is eligible for Garbage Collection then Garbage Collector will destroy POJO object.</p><h3><strong>2. Persistence State:</strong></h3><p>If we perform the Operations like save() , update(), saveOrUpdate(),…. over the POJO object which is available in Transient State then POJO object will come to Persistence State.</p><p>In Hibernate Applications, if we perform the objects like get() , load(), find(),… automatically Hibernate Software will create POJO object in Persistence State directly without Transient State.</p><p>In Persistence State, Hibernate Software is aware about POJO Object and database table record and its synchronization.</p><p>In Persistence State, if we perform modifications on POJO object then that modifications are reflected to Database table record.</p><h3><strong>3. Detached State:</strong></h3><p>If we perform the operations like close(), clear(), evict(),…. over the Session object then POJO object will come from Persistence State to Detached State.</p><p>In Detached State, Hibernate Software is not aware about POJO object and it will not provide synchronization between POJO object and database table record.</p><p>In Detached State, POJO object is representing valid record in database table, but, modifications on POJO object are not reflected to that record.</p><p>In Detached State, if we perform operations like save() , update(), saveOrUpdate(),… over the persistence object after getting Session back then POJO object will transfer from Detached State to Persistence State again.</p><p>If we keep POJO object in Detached State for long time then POJO object is eligible for Garbage Collection, where Garbage Collector will destroy that object.</p><h3><strong>4. Removed State:</strong></h3><p>If we perform the operations like remove() or delete() over the POJO object in Persistence state then POJO object will come from Persistence State to Removed State.</p><p>In Removed State, Hibernate Software is not aware about POJO object.</p><p>In Removed State, POJO object is not representing any record in Database table.</p><p>In Hibernate Applications, Once if POJO object is in Removed State then it is not possible to get back into Persistence State, directly, it must go for Garbage Collection only.</p><p><strong>Ex:</strong></p><pre>Employee emp = new Employee(); // Transient State<br> - - <br>session.save(emp);// Persistence State<br> - - <br> - - <br>session.delete(emp); //Removed State<br><br>session.close(); // Detached State</pre><h3><strong>Hibernate Schema Generation Tools:</strong></h3><p>In Hibernate applications, to perform DML operations like <strong>insert, update, delete</strong> ,… we will use the methods lile <strong>save(), update(), delete()</strong>,… from org.hibernate.Session interface provided by Hibernate Software.</p><p>In Hibernate applications, to perform DDL operations like create, alter, drop,… Hibernate has provided the following implicit tools.</p><p>1. SchemaExport</p><p>2. SchemaUpdate</p><p>3. CodeGeneration</p><p><strong>Note:</strong></p><p>The above Schema generation tools are useful to create tables when we want to work with unknown databases.</p><h3><strong>1. SchemaExport:</strong></h3><p>This tool is provided by Hibernate software in the form of</p><p>org.hibernate.tool.hbm2ddl.<strong>SchemaExport </strong>predefined class in <strong>hibernate3.jar file</strong>. If we activate this tool then it will perform the following actions.</p><p>a) First SchemaExport tool will take name and location of hibernate configuration file which we have provided as input parameter.</p><p>b) SchemaExport tool will load and parse hibernate configuration file.</p><p>c) SchemaExport tool will recognize mapping file configuration in hibernate configuration file then it will load and parse mapping file.</p><p>d) As per mapping file details, SchemaExport tool will check whether any table is existed or not in Database.</p><p>e) If no table is existed then SchemaExport tool will create new table as per mapping file details.</p><p>f) If any table is existed with the name then SchemaExport tool will drop that existed table and creates new table as per mapping file configuration details.</p><h4>Conclusion:</h4><p>SchemaExport tool will create a new table irrespective of existing the table or not.</p><p>To activate the Schema Export tool we have to use the following command on command prompt.</p><p>To activate SchemaExport tool manually through command prompt then we have to use the following command on command prompt.</p><pre>F:\apps&gt;java org.hibernate.tool.hbm2ddl.SchemaExport - config=hibernate.cfg.xml<br>Note:<br>To run the above command we must copy all hibernate jars to &quot;C:\Java\jdk1.7.0\jre\lib\ext&quot; folder and we must provide POJO class, mapping file and configuration file at the same location from where we are activating SchemaExport tool.</pre><h3><strong>Example: App08 — Customer.java</strong></h3><pre>public class Customer {<br>//instance variable<br>private String cid;<br>private String cname;<br>private String caddr;<br>private String cemail;<br>public void setCid(String cid)<br>{<br>this.cid = cid;<br>}<br>public void setCname(String cname)<br>{<br>this.cname = cname;<br>}<br>public void setCaddr(String caddr)<br>{<br>this.caddr = caddr;<br>}<br>public void setCemail(String cemail)<br>{<br>this.cemail = cemail;<br>}<br>public String getCid()<br>{<br>return cid;<br>}<br>public String getCname()<br>{<br>return cname;<br>}<br>public String getCaddr()<br>{<br>return caddr;<br>}<br>public String getCemail()<br>{<br>return cemail;<br>}<br>}</pre><h3><strong>Customer.hbm.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;Customer&quot; table=&quot;customer&quot;&gt;<br>&lt;id name=&quot;cid&quot; column=&quot;CID&quot; length=&quot;5&quot; type=&quot;string&quot;/&gt;<br>&lt;property name=&quot;cname&quot; column=&quot;CNAME&quot; length=&quot;10&quot; type=&quot;string&quot;/&gt;<br>&lt;property name=&quot;caddr&quot; column=&quot;CADDR&quot; length=&quot;10&quot; type=&quot;string&quot;/&gt;<br>&lt;property name=&quot;cemail&quot; column=&quot;CEMAIL&quot; length=&quot;20&quot; type=&quot;string&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>hibernate.cfg.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;mapping resource=&quot;Customer.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>Command on Command prompt:</strong></h3><pre>F:\apps&gt;javac Customer.java<br>F:\apps&gt;java org.hibernate.tool.hbm2ddl.SchemaExport - config=hibernate.cfg.xml<br>Above command would generate error because classpath is not set.<br>set classpath=%classpath%;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\hibernate3.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\antlr-2.7.6.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\commons-collections-3.1.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\dom4j-1.6.1.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\javassist-3.12.0.GA.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\jta-1.1.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\slf4j-api-1.6.1.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar;f:\mysql-connector-java-8.0.29.jar;e:\ojdbc11.jar;.;</pre><h3><strong>2. SchemaUpdate:</strong></h3><p>This tool is provided by Hibernate software in the form of org.hibernate.tool.hbm2ddl.SchemaUpdate predefined class in hibernate3.jar file.</p><p>If we activate this tool then it will perform the following actions.</p><p>a) First SchemaUpdate tool will take name and location of hibernate</p><p>configuration file which we have provided as input parameter.</p><p>b) SchemaUpdate tool will load and parse hibernate configuration file.</p><p>c) SchemaUpdate tool will recognize mapping file configuration in</p><p>hibernate configuration file then it will load and parse mapping file.</p><p>d) As per mapping file details, SchemaUpdate tool will check whether any</p><p>table is existed or not in Database with the same name.</p><p>e) If no table is existed then SchemaUpdate tool will create new table as</p><p>per mapping file details.</p><p>f) If any table is existed with the name then SchemaUpdate tool will alter that existed table as per mapping file configuration details.</p><p>Note:</p><p>When mapping contains new columns details when compared with database table then only SchemaUpdate tool will perform alter operation.</p><p>If Database table contains more columns when compared with mapping file configuration details then SchemaUpdate tool will not perform alter operation.</p><p><strong>To activate SchemaUpdate tool manually through command prompt then we have to use the following command on command prompt.</strong></p><pre>F:\apps&gt;java org.hibernate.tool.hbm2ddl.SchemaUpdate - config =hibernate.cfg.xml<br>This(SchemaUpdate) is for alteration for that add one property [cphone] in pojo file and hbm file.<br>And create the setter and getter also.<br><br>private String cphone;<br>public void setCphone(String cphone) {<br>this.cphone = cphone;<br>}<br>public String getCphone() {<br>return cphone;<br>}<br>Then recompile the pojo file<br>F:\App08&gt;javac Customer.java<br>Then do some updation in the hbm file.<br>F:\app08&gt;java org.hibernate.tool.hbm2ddl.SchemaUpdate - config=hibernate.cfg.xml<br>Above command would generate error because classpath is not set.<br>set classpath=%classpath%;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\hibernate3.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\antlr-2.7.6.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\commons-collections-3.1.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\dom4j-1.6.1.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final \lib\required\javassist-3.12.0.GA.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\jta-1.1.jar; D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\required\slf4j-api-1.6.1.jar;D:\4-Spring-Framework\3-Hibernate\Software\hibernate-distribution-3.6.10.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar;f:\mysql-connector-java-8.0.29.jar;e:\ojdbc11.jar;.;<br>Then execute the following command but make it a point classpath must be set.<br>F:\apps&gt;java org.hibernate.tool.hbm2ddl.SchemaUpdate - config=hibernate.cfg.xml</pre><h3><strong>3. CodeGeneration:</strong></h3><h4>The main intention of CodeGeneration tool is to create POJO class as per database table configurations which we provided in mapping file.</h4><p><strong>Note:</strong></p><p>This is available upto Hibernate2.x version, but, it is deprecated in Hibernate3.x version.</p><p>In Hibernate applications, we are able to activate SchemaExport and SchemaUpdate tools in declarative manner also , for this, we have to use the following property in hibernate configuration file.</p><p>We can activate SchemaExport and SchemaUpdate through Configuration file also by providing a property.</p><p>&lt;property name=”hibernate.hbm2ddl.auto”&gt;value&lt;/property&gt;</p><p>Where value may be</p><p>1. Create</p><p>2. Update</p><p>3. Create-Drop</p><p><strong>1. create:</strong></p><p>This option for “hibernate.hbm2ddl.auto” property will activate SchemaExport tool, it will create a table irrespective of the table is existed or not.</p><p>EX: [hibernate.cfg.xml]</p><pre>&lt;hibernate-configuration&gt;<br> - - <br>&lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;create&lt;/property&gt;<br> - - <br>&lt;/hibernate-configuration&gt;</pre><p><strong>2. Update:</strong></p><p>This option for “hibernate.hbm2ddl.auto” property will activate <strong>SchemaUpdate</strong> tool,</p><p>it will create table if table is not existed, it will alter the table if table is existed as per the mapping file.</p><p><strong>EX:</strong></p><pre>&lt;hibernate-configuration&gt;<br> - - <br>&lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/property&gt;<br> - - <br>&lt;/hibernate-configuration&gt;</pre><p><strong>3. Create-Drop:</strong></p><p>This option for “hibernate.hbm2ddl.auto” will create table when SessionFactory object is created and it will drop table when <strong>SessionFactory object is closed.</strong></p><p><strong>EX:</strong></p><pre>&lt;hibernate-configuration&gt;<br> - - <br>&lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;create-drop&lt;/property&gt;<br> - - <br>&lt;/hibernate-configuration&gt;</pre><h3>Create one <strong>Maven </strong>Project <strong>[app13]</strong></h3><pre>Update the pom.xml file<br>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>And Update the Maven Project.<br>After that add the dependencies.</pre><p>After that create one subpackage <strong>[com.ccteam.app13.entities]</strong></p><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;,length=5)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;,length=10)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;,length=5)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;,length=10)<br>private String eaddr;<br>public int getEno() {<br>return eno;<br>}<br>public void setEno(int eno) {<br>this.eno = eno;<br>}<br>public String getEname() {<br>return ename;<br>}<br>public void setEname(String ename) {<br>this.ename = ename;<br>}<br>public float getEsal() {<br>return esal;<br>}<br>public void setEsal(float esal) {<br>this.esal = esal;<br>}<br>public String getEaddr() {<br>return eaddr;<br>}<br>public void setEaddr(String eaddr) {<br>this.eaddr = eaddr;<br>}<br>}</pre><p><strong>Now Create one Source Folder [resources]</strong></p><h4><strong>hibernate.cfg.xml</strong></h4><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/college&lt;/property&gt;<br>&lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;mysql&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;property name=&quot;hibernate.hbm2ddl.auto&quot;&gt;create&lt;/property&gt;<br>&lt;mapping class=&quot;com.ccteam.app13.entities.Employee&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h4><strong>ClientApp.java</strong></h4><pre>public class ClientApp {<br>public static void main(String[] args) {<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp1 = new Employee();<br>emp1.setEno(111);<br>emp1.setEname(&quot;AAA&quot;);<br>emp1.setEsal(8000);<br>emp1.setEaddr(&quot;AGRA&quot;);<br>Transaction tx = session.beginTransaction();<br>session.save(emp1);<br>tx.commit();<br>System.out.println(&quot;Employee Inserted Successfully&quot;);<br>}<br>catch (Exception e) {<br>e.printStackTrace();<br>} finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}</pre><h4>And Run the Application.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca06de8b34d1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Composite Keys in Hibernate Application :]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/composite-keys-in-hibernate-application-5e3cb6dccd2f?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e3cb6dccd2f</guid>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[composite]]></category>
            <category><![CDATA[mapping]]></category>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[composite-key]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 06:09:17 GMT</pubDate>
            <atom:updated>2024-10-03T06:09:17.897Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Composite Keys in Hibernate Application :</strong></h3><p><strong>In Database applications, if one column is not sufficient to manage uniqueness as primary key then it is possible to declare more than one column combination as primary key, such type of column combination as primary key is called as “Composite Key”.</strong></p><h4>Execute the below command in Oracle.</h4><pre>sql&gt;create table emp10<br>(<br>ENO number,<br>ENAME varchar2(10),<br>ESAL float,<br>EADDR varchar2(10),<br>primary key(ENO,ENAME)<br>);<br>sql&gt;commit;</pre><p>To represent Composite keys in Hibernate applications we have to use the following xml tags in mapping file.</p><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot; - &quot; table=&quot; - &quot;&gt;<br>&lt;composite-id&gt;<br>&lt;key-property name=&quot; - &quot; column=&quot; - &quot;/&gt;<br> - - -<br>&lt;/composite-id&gt;<br> - - <br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><p>1)Where &lt;composite-id&gt; tag is representing composite key configuration in mapping file.</p><p>2)Where &lt;key-property&gt; tag is able to represent single ID or column configuration as part of composite key.</p><p>3)Where “name” attribute will take id property name and column attribute will take “database table column name”.</p><h4><strong>Employee.hbm.xml</strong></h4><pre>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.app07.pojo.Employee&quot; table=&quot;emp10&quot;&gt;<br>&lt;composite-id&gt;<br>&lt;key-property name=&quot;eno&quot; column=&quot;ENO&quot;/&gt;<br>&lt;key-property name=&quot;ename&quot; column=&quot;ENAME&quot;/&gt;<br>&lt;/composite-id&gt;<br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot;/&gt;<br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><p><strong>Note: </strong>To declare composite keys in annotated classes, no need to use any separate Annotation, simply we have to declare @Id annotation at both the columns.</p><p>To prepare composite keys we have to use the following queries at database SQL.</p><pre>create table emp10(ENO number(5), ename varchar2(10), esal float(5), eaddr varchar2(10), primary key(ENO, ENAME));</pre><h3><strong>Project Name [app07] Mavenized Projects:</strong></h3><h4><strong>Do some Updation in pom.xml file.</strong></h4><p>Below thing is for version.</p><pre>&lt;properties&gt;<br>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>&lt;/properties&gt;<br>And after that add the dependencies.<br>&lt;! - <br>https://mvnrepository.com/artifact/org.hibernate/hibernate-core<br> →<br>&lt;dependency&gt;<br>&lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br>&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;<br>&lt;version&gt;3.6.10.Final&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/com.mysql/mysql-connector-j →<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.mysql&lt;/groupId&gt;<br>&lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;<br>&lt;version&gt;8.0.31&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/org.javassist/javassist →<br>&lt;dependency&gt;<br>&lt;groupId&gt;org.javassist&lt;/groupId&gt;<br>&lt;artifactId&gt;javassist&lt;/artifactId&gt;<br>&lt;version&gt;3.29.2-GA&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc11 →<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;<br>&lt;artifactId&gt;ojdbc11&lt;/artifactId&gt;<br>&lt;version&gt;21.8.0.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h4><strong>Note: To use Composite keys feature in Hibernate applications then it is mandatory to implement java.io.Serializable marker interface to POJO class.</strong></h4><h3><strong>Employee.java</strong></h3><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee implements Serializable {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>private int eno;<br>@Id<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>setXXX() and getXXX()<br></pre><h3><strong>Employee.hbm.xml</strong></h3><p><strong>For these type of the file we have to create one source folder. [resources]</strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.app07.pojo.Employee&quot; table=&quot;emp10&quot;&gt;<br>&lt;composite-id&gt;<br>&lt;key-property name=&quot;eno&quot; column=&quot;ENO&quot;/&gt;<br>&lt;key-property name=&quot;ename&quot; column=&quot;ENAME&quot;/&gt;<br>&lt;/composite-id&gt;<br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot;/&gt;<br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>hibernate.cfg.xml inside the resources folder :</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;! - &lt;mapping resource=&quot;Employee.hbm.xml&quot;/&gt; →<br>&lt;mapping class=&quot;com.ccteam.app07.pojo.Employee&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>package com.ccteam.app07.test;<br>import org.hibernate.Session;<br>import org.hibernate.SessionFactory;<br>import org.hibernate.Transaction;<br>import org.hibernate.cfg.Configuration;<br>import com.ccteam.app07.pojo.Employee;<br>public class ClientApp<br>{<br>public static void main(String[] args)<br>{<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp1 = new Employee();<br>emp1.setEno(111);<br>emp1.setEname(&quot;AAA&quot;);<br>emp1.setEsal(8000);<br>emp1.setEaddr(&quot;AGRA&quot;);<br>Transaction tx = session.beginTransaction();<br>session.save(emp1);<br>tx.commit();<br>System.out.println(&quot;Employee Inserted Successfully&quot;);<br>/*<br>Employee emp_Key = new Employee();<br>emp_Key.setEno(111);<br>emp_Key.setEname(&quot;AAA&quot;);<br>Employee emp = (Employee) session.get(Employee.class, emp_Key);<br>if (emp == null) {<br>System.out.println(&quot;Employee Not Existed&quot;);<br>}<br>else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot;====================&quot;);<br>System.out.println(&quot;Employee Number :&quot; + emp.getEno());<br>System.out.println(&quot;Employee Name :&quot; + emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot; + emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot; + emp.getEaddr());<br>}<br>}<br>catch (Exception e) {<br>e.printStackTrace();<br>} finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}</pre><h3>And Run the Application.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e3cb6dccd2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is it possible to prepare hibernate application without configuration and without mapping file ?]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/is-it-possible-to-prepare-hibernate-application-without-configuration-and-without-mapping-file-0c5d215868f3?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c5d215868f3</guid>
            <category><![CDATA[mapping-solution]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[declarative-approach]]></category>
            <category><![CDATA[declarative-programming]]></category>
            <category><![CDATA[configuration-file]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 05:58:37 GMT</pubDate>
            <atom:updated>2024-10-03T05:58:37.658Z</atom:updated>
            <content:encoded><![CDATA[<h3>Is it possible to prepare hibernate application without configuration and without mapping file ?</h3><p>YES</p><p>In Declarative approach, we will provide all configuration details in either properties file or in XML file then we will send configuration details to Hibernate application.</p><h4>There are two ways to provide configuration details to Hibernate Applications in Declarative Approach.</h4><pre>1) By using properties file.<br>2) By Using XML file.</pre><h4><strong>1) Using properties file in Declarative Approach:</strong></h4><p>In This approach, to provide all hibernate configuration details, we have to declare a properties file with the default name “<strong>hibernate.properties</strong>” under “src” folder.</p><p>In this context, when we create Configuration class object , Hibernate Software will search for the properties file with the name “<strong>hibernate.properties</strong>” and get all the configuration details into Configuration class object.</p><p>In this approach we must add mapping file explicitly to Configuration File by using the following method.</p><pre>public void addResource(String mapping_File-Name)</pre><h4><strong>hibernate.properties:</strong></h4><h4><strong>For Oracle:</strong></h4><pre>hibernate.connection.driver_Class = oracle.jdbc.OracleDriver<br>hibernate.connection.url = jdbc:oracle:thin:@localhost:1521:xe<br>hibernate.connection.username = system<br>hibernate.connection.password = oracle<br>hibernate.dialect = org.hibernate.dialect.OracleDialect</pre><h4><strong>For MySQL:</strong></h4><pre>hibernate.connection.driver_Class = com.mysql.cj.jdbc.Driver<br>hibernate.connection.url = jdbc:mysql://localhost:3306/college<br>hibernate.connection.username = root<br>hibernate.connection.password = mysql<br>hibernate.dialect = org.hibernate.dialect.MYSQLDialect</pre><h4><strong>ClientApp.java</strong></h4><pre>class ClientApp{<br>public static void main(String[] args)<br>{<br>Configuration cfg = new Configuration();<br>cfg.addResource(&quot;Employee.hbm.xml&quot;);<br> - - <br>}<br>}</pre><p>If we change name and location of properties file from hibernate.properties to some other abc.properties then we have to give that intimation to Hibernate software, for this, we have to create FileInputStream and Properties class object then we have to set Properties object to Configuration class object by using the following method.</p><pre>public void setProperties(Properties p)</pre><h4><strong>Example to provide configuration details to Hibernate by using properties file:</strong></h4><h4><strong>Create the Maven Project :[app10-declarative-approach]</strong></h4><p>First of all to change the jdk version do some modification in pom.xml file. And then</p><p>Right Click on the Project — → Maven -à Update Project.</p><p>After that add some dependencies.</p><p>Maven Dependencies:</p><pre>&lt;dependency&gt;<br>&lt;groupId&gt;org.hibernate&lt;/groupId&gt;<br>&lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;<br>&lt;version&gt;3.6.10.Final&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.mysql&lt;/groupId&gt;<br>&lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;<br>&lt;version&gt;8.0.31&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>&lt;groupId&gt;com.oracle.database.jdbc&lt;/groupId&gt;<br>&lt;artifactId&gt;ojdbc11&lt;/artifactId&gt;<br>&lt;version&gt;21.8.0.0&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;! -  https://mvnrepository.com/artifact/org.javassist/javassist →<br>&lt;dependency&gt;<br>&lt;groupId&gt;org.javassist&lt;/groupId&gt;<br>&lt;artifactId&gt;javassist&lt;/artifactId&gt;<br>&lt;version&gt;3.29.2-GA&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.pojo;<br>public class Employee {<br>//instance variable<br>private int eno;<br>private String ename;<br>private float esal;<br>private String eaddr;<br>public int getEno()<br>{<br>return eno;<br>}<br>public void setEno(int eno)<br>{<br>this.eno = eno;<br>}<br>public String getEname() {<br>return ename;<br>}<br>public void setEname(String ename) {<br>this.ename = ename;<br>}<br>public float getEsal() {<br>return esal;<br>}<br>public void setEsal(float esal) {<br>this.esal = esal;<br>}<br>public String getEaddr() {<br>return eaddr;<br>}<br>public void setEaddr(String eaddr) {<br>this.eaddr = eaddr;<br>}<br>}</pre><h4><strong>Now create one resources folder for that right click on the project and select source folder.</strong></h4><h3><strong>[hibernate.properties/abc.properties]</strong></h3><pre>hibernate.connection.driver_Class = oracle.jdbc.OracleDriver<br>hibernate.connection.url = jdbc:oracle:thin:@localhost:1521:xe<br>hibernate.connection.username = system<br>hibernate.connection.password = oracle<br>hibernate.dialect = org.hibernate.dialect.OracleDialect<br>If it is mysql then<br>hibernate.connection.driver_Class = com.mysql.cj.jdbc.Driver<br>hibernate.connection.url = jdbc:mysql://localhost:3306/college<br>hibernate.connection.username = root<br>hibernate.connection.password = mysql<br>hibernate.dialect = org.hibernate.dialect.MYSQLDialectxml</pre><h3><strong>Employee.hbm.xml</strong></h3><pre>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.pojo.Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;/&gt;<br>&lt;property name=&quot;ename&quot; column=&quot;ENAME&quot;/&gt;<br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot;/&gt;<br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>public class ClientApp {<br>public static void main(String[] args) {<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>// Configuration cfg = new AnnotationConfiguration();<br>Configuration cfg = new Configuration();<br>//FileInputStream fis = new FileInputStream(&quot;F:\\HibernateDemos\\abc.properties&quot;);<br>//Properties p = new Properties();<br>// p.load(fis);<br>// cfg.setProperties(p);<br>cfg.addResource(&quot;Employee.hbm.xml&quot;);<br>//cfg.addAnnotatedClass(com.ccteam.hbn.pojo.Employee.class);<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp = (Employee) session.get(Employee.class, 111);<br>if (emp == null) {<br>System.out.println(&quot;Employee Not Existed&quot;);<br>}<br>else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot;====================&quot;);<br>System.out.println(&quot;Employee Number :&quot; + emp.getEno());<br>System.out.println(&quot;Employee Name :&quot; + emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot; + emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot; + emp.getEaddr());<br>}<br>} catch (Exception e) {<br>e.printStackTrace();<br>} finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}</pre><h4><strong>And Finally Run the Application.</strong></h4><h4>Remember : To create table in Database like Mysql and Oracle</h4><pre>create table Emp1<br>(<br>ENO int primary key,<br>EName varchar(10),<br>ESal float,<br>EAddr varchar(10)<br>);<br><br>insert into emp1 <br>values(101,&#39;DD Singh&#39;,5000,&#39;Agra&#39;);<br><br>Select * from emp1;</pre><p>In hibernate applications, if we use “properties” file then we are able to provide only hibernate connection properties, dialect propeties,… , but, we are unable to provide mapping properties,… through properties file, to provide mapping properties we have to use java methods like addResource( — ) or addAnnotatedClass( — ) methods from Configuration class.</p><p>In Hibernate applications, if we want to provide all configuration details in declarative manner then we have to use XML file approach.</p><p>If we want to use xml file to provide all configuration details then we have to use configure() method to get all configuration details from xml file. Here configure() method will search for xml file with the default name hibernate.cfg.xml under src folder inorder to get configuration details. If we change the default name and location of hibernate configuration file then we have to pass that name and location to configure( — ) method as parameter.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c5d215868f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is it possible to run Hibernate Applications without using configuration file?]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/is-it-possible-to-run-hibernate-applications-without-using-configuration-file-b658b9529941?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/b658b9529941</guid>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[programatic-approach]]></category>
            <category><![CDATA[hibernate-application]]></category>
            <category><![CDATA[without-mapping]]></category>
            <category><![CDATA[without-configuration]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 05:45:03 GMT</pubDate>
            <atom:updated>2024-10-03T05:45:03.344Z</atom:updated>
            <content:encoded><![CDATA[<p>Yes</p><h4>Yes, it is possible to run Hibernate applications without using configuration file.</h4><p>In Hibernate Applications, we are able to provide configuration details in the following two approaches.</p><p><strong>1) Programmatic Approach</strong></p><p><strong>2) Declarative Approach</strong></p><p>a) By using properties file</p><p>b) By Using XML file</p><h3><strong>1) Programmatic Approach:</strong></h3><p>In Programmatic approach, to provide configuration details, first we have to create Configuration class object then we have to set all hibernate properties to Configuration class object explicitly by using the following method.</p><p>Syntax:</p><pre>public void setProperty(String prop_Name, String prop_Val)</pre><p>To add mapping file name and location to Configuration file we have to use the following method.</p><pre>public void addResource(String mapping_File_Name)</pre><p>If we are using annotations in place of mapping file then we have to use the following method to add annotated class.</p><pre>public void addAnnotatedClass(Class class)</pre><p><strong>Example on without configuration File and with mapping file in Programmatic Approach:</strong></p><h3>Project :</h3><h4><strong>Create one maven project [app09-without-configuration]</strong></h4><h4><strong>Update the POM.xml file</strong></h4><pre>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>Then Update the Project.<br>Right Click on the Project ==&gt; Maven ==&gt; Update the Project.</pre><p><strong>Now add some dependencies.</strong></p><pre>è Hibernate Core Maven Dependency<br>è MYSQL Connector Maven Dependency<br>è Javassist</pre><h4><strong>Now create one package :com.ccteam.pojo</strong></h4><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.pojo;<br>public class Employee {<br>private int eno;<br>private String ename;<br>private float esal;<br>private String eaddr;<br>getXXX();<br>setXXX();<br>}</pre><h4><strong>Now create one source folder [resources] For that right click on the project.</strong></h4><h3><strong>Employee.hbm.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.pojo.Employee&quot; table=&quot;emp1&quot;&gt;<br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;/&gt;<br>&lt;property name=&quot;ename&quot; column=&quot;ENAME&quot;/&gt;<br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot;/&gt;<br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;</pre><h3><strong>ClientApp.java :</strong></h3><pre>package com.ccteam.test;<br>import org.hibernate.Session;<br>import org.hibernate.SessionFactory;<br>import org.hibernate.cfg.AnnotationConfiguration;<br>import org.hibernate.cfg.Configuration;<br>import com.ccteam.hbn.pojo.Employee;<br>public class ClientApp {<br>public static void main(String[] args) {<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>// Configuration cfg = new AnnotationConfiguration();<br>Configuration cfg = new Configuration();<br>/*<br>cfg.setProperty(&quot;hibernate.connection.driver_Class&quot;,&quot;oracle.jdbc.OracleDriver&quot;);<br>cfg.setProperty(&quot;hibernate.connection.url&quot;,&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;);<br>cfg.setProperty(&quot;hibernate.connection.username&quot;, &quot;system&quot;);<br>cfg.setProperty(&quot;hibernate.connection.password&quot;, &quot;oracle&quot;);<br>cfg.setProperty(&quot;hibernate.dialect&quot;, &quot;org.hibernate.dialect.OracleDialect&quot;);<br>cfg.addResource(&quot;Employee.hbm.xml&quot;);<br>*/<br>cfg.setProperty(&quot;hibernate.connection.driver_Class&quot;,&quot;com.mysql.cj.jdbc.Driver&quot;);<br>cfg.setProperty(&quot;hibernate.connection.url&quot;,&quot;jdbc:mysql://localhost:3306/college&quot;);<br>cfg.setProperty(&quot;hibernate.connection.username&quot;, &quot;root&quot;);<br>cfg.setProperty(&quot;hibernate.connection.password&quot;, &quot;mysql&quot;);<br>cfg.setProperty(&quot;hibernate.dialect&quot;, &quot;org.hibernate.dialect.MySQLDialect&quot;);<br>cfg.addResource(&quot;Employee.hbm.xml&quot;);<br>//cfg.addAnnotatedClass(com.ccteam.pojo.Employee.class);<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp = (Employee) session.get(Employee.class, 222);<br>if (emp == null) {<br>System.out.println(&quot;Employee Not Existed&quot;);<br>} else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot;====================&quot;);<br>System.out.println(&quot;Employee Number :&quot; + emp.getEno());<br>System.out.println(&quot;Employee Name :&quot; + emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot; + emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot; + emp.getEaddr());<br>}<br>} catch (Exception e) {<br>e.printStackTrace();<br>} finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}<br></pre><h3>And Finally Run the Project :</h3><h4>Remember : To create table in Mysql Database :</h4><pre>create table Emp1<br>(<br>ENO int primary key,<br>EName varchar(10),<br>ESal float,<br>EAddr varchar(10)<br>);<br><br>insert into emp1 <br>values(101,&#39;DD Singh&#39;,5000,&#39;Agra&#39;);<br><br>Select * from emp1;</pre><h3><strong>Example on without configuration file and without mapping file in Programmatic approach:</strong></h3><h4>For this we need to do some modification in the Employee class.</h4><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.pojo;<br>import javax.persistence.Column;<br>import javax.persistence.Entity;<br>import javax.persistence.Id;<br>import javax.persistence.Table;<br>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>public int getEno() {<br>return eno;<br>}<br>public void setEno(int eno) {<br>this.eno = eno;<br>}<br>public String getEname() {<br>return ename;<br>}<br>public void setEname(String ename) {<br>this.ename = ename;<br>}<br>public float getEsal() {<br>return esal;<br>}<br>public void setEsal(float esal) {<br>this.esal = esal;<br>}<br>public String getEaddr() {<br>return eaddr;<br>}<br>public void setEaddr(String eaddr) {<br>this.eaddr = eaddr;<br>}<br>}</pre><h3><strong>ClientApp.java</strong></h3><pre>public class ClientApp {<br>public static void main(String[] args) {<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>// Configuration cfg = new AnnotationConfiguration();<br>Configuration cfg = new Configuration();<br>cfg.setProperty(&quot;hibernate.connection.driver_Class&quot;,&quot;oracle.jdbc.OracleDriver&quot;);<br>cfg.setProperty(&quot;hibernate.connection.url&quot;,&quot;jdbc:oracle:thin:@localhost:1521:xe&quot;);<br>cfg.setProperty(&quot;hibernate.connection.username&quot;, &quot;system&quot;);<br>cfg.setProperty(&quot;hibernate.connection.password&quot;, &quot;oracle&quot;);<br>cfg.setProperty(&quot;hibernate.dialect&quot;, &quot;org.hibernate.dialect.OracleDialect&quot;);<br>//cfg.addResource(&quot;Employee.hbm.xml&quot;);<br>cfg.addAnnotatedClass(com.ccteam.pojo.Employee.class);<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp = (Employee) session.get(Employee.class, 222);<br>if (emp == null) {<br>System.out.println(&quot;Employee Not Existed&quot;);<br>} else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot;====================&quot;);<br>System.out.println(&quot;Employee Number :&quot; + emp.getEno());<br>System.out.println(&quot;Employee Name :&quot; + emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot; + emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot; + emp.getEaddr());<br>}<br>} catch (Exception e) {<br>e.printStackTrace();<br>} finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}</pre><h3>And Run the Application.</h3><p><strong>In Programmatic approach, </strong>if we want to change configuration details like connection properties or database properties,…. then we have to perform modifications in JAVA code, if we perform modifications in JAVA code then we must recompile the java application , it is not suggestible in enterprise applications. To overcome the problem we have to use Declarative approach.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b658b9529941" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is it possible to run hibernate applications without using mapping file?]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/is-it-possible-to-run-hibernate-applications-without-using-mapping-file-6b9d65f82a8a?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b9d65f82a8a</guid>
            <category><![CDATA[configuration]]></category>
            <category><![CDATA[hibernate-tutorial]]></category>
            <category><![CDATA[configuration-management]]></category>
            <category><![CDATA[client-management]]></category>
            <category><![CDATA[hibernate]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 05:30:59 GMT</pubDate>
            <atom:updated>2024-10-03T05:30:59.307Z</atom:updated>
            <content:encoded><![CDATA[<h4><strong>ANS: </strong>Yes,</h4><h4>It is possible to run hibernate applications without using mapping file, but, we have to use Annotations.</h4><p>To provide mapping details in Hibernate applications we have to use the following annotations provided by JPA in the form of “<strong>javax.persistence</strong>” package.</p><h4><strong>1. @Entity:</strong></h4><p>It is a class level annotation, it can be used to declare a class as an Entity class.</p><h4><strong>2. @Table(name=” — “)</strong></h4><p>This annotation is a class level annotation, it can be used to configure table name for the entity class. Where “name” member is able to provide the respective table name.</p><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>class Employee{<br>}</pre><h4><strong>3. @Id :</strong></h4><p>This annotation is Field/Method[getXXX()] level annotation, it can be used to declare a property as ID property.</p><h4><strong>4. @Column(name=” — “)</strong></h4><p>This annotation is Field/Method[getXXX()] level annotation, it can be used to provide a database table column name in order to provide mapping .</p><p>In Hibernate Applications, to use annotations we have to use the following steps.</p><p>1) Remove mapping file and provide annotations in POJO class.</p><p>2) In Configuration File , provide &lt;mapping&gt; tag with “class” attribute in place of “resource” attribute.“class” attribute will take fully qualified name of the respective POJO class.</p><p>3) In Client Application, Create either AnnotationConfiguration object or Configuration object to process Annotations.</p><p><strong>Note: </strong>AnnotationConfiguration is deprecated class, so it is better to use Configuration class object to process annotations.</p><h3><strong>Employee.java</strong></h3><pre>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee{<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>setXXX() and getXXX()<br>}</pre><h3><strong>hibernate.cfg.xml</strong></h3><pre>&lt;hibernate-configuration&gt;<br> - - - <br> - - - <br>&lt;mapping class=&quot;com.ccteam.pojo.Employee&quot;/&gt;<br>&lt;/hibernate-Configuration&gt;</pre><h3><strong>ClientApp.java:</strong></h3><pre>To prepare Client Architecture .</pre><h4><strong>Project Name : EMPLOYEE MANAGEMNET SYSTEM</strong></h4><h4><strong>Create one maven project : app08-annotation</strong></h4><h4><strong>Update the POM.xml file</strong></h4><pre>&lt;maven.compiler.source&gt;1.17&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;1.17&lt;/maven.compiler.target&gt;<br>Then Update the Project.<br>Right Click on the Project ==&gt; Maven ==&gt; Update the Project.</pre><h4><strong>Now add some dependencies :</strong></h4><pre> Hibernate Core Maven Dependency<br> MYSQL Connector Maven Dependency<br> Javassist</pre><h4><strong>Now create one package : com.ccteam.pojo</strong></h4><h3><strong>Employee.java :</strong></h3><pre>package com.ccteam.pojo;<br>import javax.persistence.Column;<br>import javax.persistence.Entity;<br>import javax.persistence.Id;<br>import javax.persistence.Table;<br>@Entity<br>@Table(name=&quot;emp1&quot;)<br>public class Employee {<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>private int eno;<br>@Column(name=&quot;ENAME&quot;)<br>private String ename;<br>@Column(name=&quot;ESAL&quot;)<br>private float esal;<br>@Column(name=&quot;EADDR&quot;)<br>private String eaddr;<br>/*<br>@Id<br>@Column(name=&quot;ENO&quot;)<br>*/<br>public int getEno()<br>{<br>return eno;<br>}<br>public void setEno(int eno)<br>{<br>this.eno = eno;<br>}<br>// @Column(name=&quot;ENAME&quot;)<br>public String getEname()<br>{<br>return ename;<br>}<br>public void setEname(String ename)<br>{<br>this.ename = ename;<br>}<br>//@Column(name=&quot;ESAL&quot;)<br>public float getEsal() {<br>return esal;<br>}<br>public void setEsal(float esal) {<br>this.esal = esal;<br>}<br>//@Column(name=&quot;EADDR&quot;)<br>public String getEaddr() {<br>return eaddr;<br>}<br>public void setEaddr(String eaddr) {<br>this.eaddr = eaddr;<br>}<br>}</pre><h4>Now create one source folder [resources] For that right click on the project.</h4><h3><strong>hibernate.cfg.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/college&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;root&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;mysql&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;<br>&lt;mapping class=&quot;com.ccteam.pojo.Employee&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>package com.ccteam.test;<br>import org.hibernate.Session;<br>import org.hibernate.SessionFactory;<br>import org.hibernate.cfg.AnnotationConfiguration;<br>import org.hibernate.cfg.Configuration;<br>import com.durgasoft.hbn.pojo.Employee;<br>public class ClientApp {<br>public static void main(String[] args) {<br>SessionFactory sessionFactory = null;<br>Session session = null;<br>try {<br>//Configuration cfg = new AnnotationConfiguration();<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>sessionFactory = cfg.buildSessionFactory();<br>session = sessionFactory.openSession();<br>Employee emp = (Employee)session.get(Employee.class, 111);<br>if(emp == null) {<br>System.out.println(&quot;Employee Not Existed&quot;);<br>}else {<br>System.out.println(&quot;Employee Details&quot;);<br>System.out.println(&quot;====================&quot;);<br>System.out.println(&quot;Employee Number :&quot;+emp.getEno());<br>System.out.println(&quot;Employee Name :&quot;+emp.getEname());<br>System.out.println(&quot;Employee Salary :&quot;+emp.getEsal());<br>System.out.println(&quot;Employee Address :&quot;+emp.getEaddr());<br>}<br>} catch (Exception e) {<br>e.printStackTrace();<br>}finally {<br>session.close();<br>sessionFactory.close();<br>}<br>}<br>}</pre><h3>Finally Run the Application.</h3><h4>Remember to Create table in Mysql :</h4><pre>create table Emp1<br>(<br>ENO int primary key,<br>EName varchar(10),<br>ESal float,<br>EAddr varchar(10)<br>);<br><br>insert into emp1 <br>values(101,&#39;DD Singh&#39;,5000,&#39;Agra&#39;);<br><br>Select * from emp1;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b9d65f82a8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to prepare JSP_Hibernate_Application:]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/how-to-prepare-jsp-hibernate-application-afbb7608b0d0?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/afbb7608b0d0</guid>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[jsp]]></category>
            <category><![CDATA[application-architecture]]></category>
            <category><![CDATA[hibernate-jpa]]></category>
            <category><![CDATA[application]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 05:16:53 GMT</pubDate>
            <atom:updated>2024-10-03T05:16:53.984Z</atom:updated>
            <content:encoded><![CDATA[<h4>There are two types of Frameworks.</h4><ol><li>Web Frameworks</li></ol><p>2. Application Frameworks</p><h4><strong>What is the difference between web frameworks and Application Frameworks?</strong></h4><p><strong>Ans: Web Frameworks</strong> will provide very good environment to prepare and execute web applications only.</p><p>EX: Struts, JSF.</p><p><strong>Application Frameworks</strong> will provide very good environment to prepare and execute any type of JAVA/J2EE Applications including Standalone Applications, Web applications, Distributed Applications,</p><p>EX: Spring</p><p>Struts is MVC based web framework provided by Apache Software Foundations.</p><h4><strong>Que)Is it possible to interact with more than one Database from a single Hibernate Application?</strong></h4><h4>Ans) Yes, in single hibernate application we are able to interact with more than one Database, but, we must use the following conventions.</h4><p>1) We must provide separate configuration file for each and every Database.</p><p>2) Use either single mapping file if we have same table name and same columns in both the databases tables and use different mapping file for each and every database if we have difference in table names and column names.</p><p>3) In Client application.</p><p>a) Prepare Seperate Configuration object for each and every DB.</p><p>b) Prepare seperate SessionFactory object for each and every DB.</p><p>c) Prepare seperate Session object for each and every DB.</p><p>d) Prepare Seperate Transaction object for each and every DB as per the requirement.</p><p>e) Perform persistance operations on DB respective Session objects.</p><h3><strong>Project Name [MultiDBApp] :</strong></h3><h3><strong>Employee.java</strong></h3><pre>package com.ccteam.entities; <br>public class Employee { <br>private int eno; <br>private String ename; <br>private float esal; <br>private String eaddr; <br><br>public int getEno() <br>{ <br>return eno; <br>} <br>public void setEno(int eno) <br>{ <br>this.eno = eno; <br>} <br>public String getEname() <br>{ <br>return ename; <br>} <br>public void setEname(String ename) <br>{ <br>this.ename = ename; <br>} <br>public float getEsal() <br>{ <br>return esal; <br>} <br>public void setEsal(float esal) <br>{ <br>this.esal = esal; <br>} <br>}</pre><h4><strong>Now Create one source folder name is resources :</strong></h4><h3><strong>Employee.hbm.xml :</strong></h3><pre>&lt;!DOCTYPE hibernate-mapping PUBLIC <br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot; <br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt; <br>&lt;hibernate-mapping&gt; <br>&lt;class name=&quot;com.ccteam.pojo.Employee&quot; table=&quot;emp1&quot;&gt; <br>&lt;id name=&quot;eno&quot; column=&quot;ENO&quot;/&gt; <br>&lt;property name=&quot;ename&quot; column=&quot;ENAME&quot;/&gt; <br>&lt;property name=&quot;esal&quot; column=&quot;ESAL&quot;/&gt; <br>&lt;property name=&quot;eaddr&quot; column=&quot;EADDR&quot;/&gt; <br>&lt;/class&gt; <br>&lt;/hibernate-mapping&gt; </pre><h3><strong>Inside the resources folder hibernate_oracle.cfg.xml :</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;mapping resource=&quot;com/ccteam/mappings/Employee.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>Inside the resources folder hibernate_mysql.cfg.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;connection.driver_Class&quot;&gt;com.mysql.cj.jdbc.Driver&lt;/property&gt;<br>&lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/durgadb&lt;/property&gt;<br>&lt;property name=&quot;connection.user&quot;&gt;root&lt;/property&gt;<br>&lt;property name=&quot;connection.password&quot;&gt;mysql&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;mapping resource=&quot;com/ccteam/mappings/Employee.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>ClientApp.java</strong></h3><pre>public class ClientApp<br>{<br>public static void main(String[] args)<br>{<br>SessionFactory oracle_SessionFactory = null;<br>SessionFactory mysql_SessionFactory = null;<br>Session oracle_Session = null;<br>Session mysql_Session = null;<br>try<br>{<br>Configuration oracle_Cfg = new Configuration();<br>Configuration mysql_Cfg = new Configuration();<br>oracle_Cfg.configure(&quot;hibernate_oracle.cfg.xml&quot;);<br>mysql_Cfg.configure(&quot;hibernate_mysql.cfg.xml&quot;);<br>oracle_SessionFactory = oracle_Cfg.buildSessionFactory();<br>mysql_SessionFactory = mysql_Cfg.buildSessionFactory();<br>oracle_Session = oracle_SessionFactory.openSession();<br>mysql_Session = mysql_SessionFactory.openSession();<br>Employee emp = (Employee) oracle_Session.get(Employee.class, 111);<br>System.out.println(&quot;Employee retrived from Oracle DB&quot;);<br>Transaction mysql_Tx = mysql_Session.beginTransaction();<br>mysql_Session.save(emp);<br>mysql_Tx.commit();<br>System.out.println(&quot;Employee Inserted in MySQL DB&quot;);<br>} catch (Exception e)<br>{<br>e.printStackTrace();<br>}<br>finally<br>{<br>oracle_Session.close();<br>mysql_Session.close();<br>oracle_SessionFactory.close();<br>mysql_SessionFactory.close();<br>}<br>}<br>}</pre><h3><strong>MySql Table :</strong></h3><pre>create table Emp1<br>(<br>ENO int(10) primary key,<br>EName varchar(10),<br>ESal float,<br>EAddr varchar(10)<br>);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afbb7608b0d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to prepare Servlet_Hibernate_Application:]]></title>
            <link>https://medium.com/@sanjaysanjayagarwal3/how-to-prepare-servlet-hibernate-application-e3055e7452fa?source=rss-fe38c74272b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e3055e7452fa</guid>
            <category><![CDATA[servlet-lifecycle]]></category>
            <category><![CDATA[servlet-container]]></category>
            <category><![CDATA[hibernate]]></category>
            <category><![CDATA[servlet]]></category>
            <category><![CDATA[hibernate-tutorial]]></category>
            <dc:creator><![CDATA[Sanjay Agarwal]]></dc:creator>
            <pubDate>Thu, 03 Oct 2024 05:03:26 GMT</pubDate>
            <atom:updated>2024-10-03T05:03:26.851Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*R2psLXaSepIzLSYso4PjEA.png" /><figcaption>FlowChart of Servlet HIbernate Application :</figcaption></figure><h4>First of all create one Dynamic Web Project</h4><h4>And add the required jar files.</h4><h4><strong>Create some package :</strong></h4><pre>com.ccteam.pojo<br>com.ccteam.util<br>com.ccteam.service<br>com.ccteam.servlets</pre><h3><strong>User.java</strong></h3><pre>package com.ccteam.pojo;<br>public class User {<br>//instance variable<br>private String uname;<br>private String upwd;<br>public String getUname() {<br>return uname;<br>}<br>public void setUname(String uname) {<br>this.uname = uname;<br>}<br>public String getUpwd() {<br>return upwd;<br>}<br>public void setUpwd(String upwd) {<br>this.upwd = upwd;<br>}<br>}</pre><h3><strong>Loginform.html</strong></h3><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859–1&quot;&gt;<br>&lt;title&gt;Login Screen&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;h2&gt;Welcome to My Coachin&lt;/h2&gt;<br>&lt;h3&gt;User Login Form&lt;/h3&gt;<br>&lt;form method = &quot;POST&quot; action=&quot;./login&quot;&gt;<br>&lt;table&gt;<br>&lt;tr&gt;<br>&lt;td&gt;User Name&lt;/td&gt;<br>&lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;uname&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br>&lt;td&gt;Password&lt;/td&gt;<br>&lt;td&gt;&lt;input type=&quot;password&quot; name=&quot;upwd&quot;/&gt;&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;tr&gt;<br>&lt;td&gt;<br>&lt;input type=&quot;submit&quot; value=&quot;Login&quot;/&gt;<br>&lt;/td&gt;<br>&lt;/tr&gt;<br>&lt;/table&gt;<br>&lt;/form&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><h3><strong>Success.html</strong></h3><pre>&lt;html&gt;<br>&lt;head&gt;<br>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859–1&quot;&gt;<br>&lt;title&gt;Success Page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;h2&gt;Oracle Software Solutions&lt;/h2&gt;<br>&lt;h3&gt;User Login Status Page&lt;/h3&gt;<br>&lt;font color=&quot;red&quot; size=&quot;6&quot;&gt;<br>&lt;b&gt; User Login Success &lt;/b&gt;<br>&lt;/font&gt;<br>&lt;h3&gt;<br>&lt;a href=&quot;./loginform.html&quot;&gt;|User Login Form|&lt;/a&gt;<br>&lt;/h3&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>[failure.html]<br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859–1&quot;&gt;<br>&lt;title&gt;Failure Page&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;h2&gt;Oracle Software Solutions&lt;/h2&gt;<br>&lt;h3&gt;User Login Status Page&lt;/h3&gt;<br>&lt;font color=&quot;red&quot; size=&quot;6&quot;&gt;<br>&lt;b&gt;<br>User Login Failure<br>&lt;/b&gt;<br>&lt;/font&gt;<br>&lt;h3&gt;<br>&lt;a href=&quot;./loginform.html&quot;&gt;|User Login Form|&lt;/a&gt;<br>&lt;/h3&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><h4><strong>Now Create one source folder [name is resources : ]</strong></h4><h4><strong>Inside the resources folder create a xml file</strong></h4><h3><strong>User.hbm.xml</strong></h3><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-mapping PUBLIC<br>&quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;<br>&lt;hibernate-mapping&gt;<br>&lt;class name=&quot;com.ccteam.pojo.User&quot; table=&quot;Reg_Users&quot;&gt;<br>&lt;id name=&quot;uname&quot; column=&quot;UNAME&quot;/&gt;<br>&lt;property name=&quot;upwd&quot; column=&quot;UPWD&quot;/&gt;<br>&lt;/class&gt;<br>&lt;/hibernate-mapping&gt;<br>[hibernate.cfg.xml]<br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE hibernate-configuration PUBLIC<br>&quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;<br>&quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;<br>&lt;hibernate-configuration&gt;<br>&lt;session-factory&gt;<br>&lt;property name=&quot;hibernate.connection.driver_Class&quot;&gt;oracle.jdbc.OracleDriver&lt;/property&gt;<br>&lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;<br>&lt;property name=&quot;hibernate.connection.username&quot;&gt;system&lt;/property&gt;<br>&lt;property name=&quot;hibernate.connection.password&quot;&gt;oracle&lt;/property&gt;<br>&lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;<br>&lt;property name=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt;<br>&lt;mapping resource=&quot;User.hbm.xml&quot;/&gt;<br>&lt;/session-factory&gt;<br>&lt;/hibernate-configuration&gt;</pre><h3><strong>HibernateUtil.java</strong></h3><pre>package com.ccteam.util;<br>import org.hibernate.SessionFactory;<br>import org.hibernate.cfg.Configuration;<br>public class HibernateUtil{<br>private static SessionFactory sessionFactory;<br>static<br>{<br>try<br>{<br>Configuration cfg = new Configuration();<br>cfg.configure();<br>sessionFactory = cfg.buildSessionFactory();<br>System.out.println(sessionFactory);<br>}<br>catch (Exception e)<br>{<br>e.printStackTrace();<br>}<br>}<br>public static SessionFactory getSessionFactory()<br>{<br>return sessionFactory;<br>}<br>}</pre><h3><strong>UserService.java</strong></h3><pre>package com.ccteam.service;<br>public class UserService {<br>String status = &quot;&quot;;<br>public String checkLogin(String uname, String upwd)<br>{<br>try {<br>SessionFactory sessionFactory = HibernateUtil.getSessionFactory();<br>Session session = sessionFactory.openSession();<br>System.out.println(session);<br>User user = (User)session.get(com.ccteam.pojo.User.class, uname);<br>if(user == null)<br>{<br>status = &quot;failure&quot;;<br>}<br>else<br>{<br>if(user.getUpwd().equals(upwd))<br>{<br>status = &quot;success&quot;;<br>}<br>else<br>{<br>status = &quot;failure&quot;;<br>}<br>}<br>} catch (Exception e) {<br>e.printStackTrace();<br>}<br>return status;<br>}<br>}</pre><h3><strong>LoginServlet.java</strong></h3><pre>package com.ccteam.servlets;<br>@WebServlet(&quot;/login&quot;)<br>public class LoginServlet extends HttpServlet{<br>public void init() {<br>HibernateUtil.getSessionFactory();<br>System.out.println(&quot;init()&quot;);<br>}<br>private static final long serialVersionUID = 1L;<br>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {<br>String uname = request.getParameter(&quot;uname&quot;);<br>String upwd = request.getParameter(&quot;upwd&quot;);<br>UserService user_Service = new UserService();<br>String status = user_Service.checkLogin(uname, upwd);<br>RequestDispatcher requestDispatcher = null;<br>if(status.equals(&quot;success&quot;)) {<br>requestDispatcher = request.getRequestDispatcher(&quot;/success.html&quot;);<br>requestDispatcher.forward(request, response);<br>}else {<br>requestDispatcher = request.getRequestDispatcher(&quot;/failure.html&quot;);<br>requestDispatcher.forward(request, response);<br>}<br>}<br>}</pre><p><strong>But Before Execution U need to create one table in oracle database.</strong></p><pre>Create table Reg_Users<br>(<br>Uname varchar2(10) primary key,<br>Upwd varchar2(10)<br>);</pre><h4>Now Finally Run the Project .</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e3055e7452fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>