{"id":3313,"date":"2019-04-08T13:14:46","date_gmt":"2019-04-08T13:14:46","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3313"},"modified":"2019-04-08T13:32:15","modified_gmt":"2019-04-08T13:32:15","slug":"how-hibernate-works","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-hibernate-works\/","title":{"rendered":"How Hibernate Works"},"content":{"rendered":"<p>The heart of any Hibernate application is in its configuration. There are two pieces of configuration required in any Hibernate application: one creates the database connections, and the other creates the object-to-table mapping<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2019\/04\/hibernate_high_level.jpg\" alt=\"null\" height=\"364\" width=\"460\"\/><\/p>\n<p>To create a connection to the database, Hibernate must know the details of our database, tables, classes, and other mechanics. This information is ideally provided as an XML file (usually named hibernate.cfg.xml) or as a simple text file with name\/value pairs (usually named hibernate.properties).<br \/>\nIn XML style. We name this file hibernate.cfg.xml so the framework can load this file automatically.<\/p>\n<p><strong>hibernate.cfg.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;hibernate-configuration&gt;  \r\n    &lt;session-factory&gt;\r\n        &lt;property name=&quot;connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;\/property&gt;\r\n        &lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql:\/\/localhost:3306\/hibernate&lt;\/property&gt;\r\n        &lt;property name=&quot;connection.username&quot;&gt;root&lt;\/property&gt;\r\n        &lt;property name=&quot;connection.password&quot;&gt;pass&lt;\/property&gt;\r\n         \r\n        &lt;!-- JDBC connection pool (use the built-in) --&gt;\r\n        &lt;property name=&quot;connection.pool_size&quot;&gt;1&lt;\/property&gt;\r\n \r\n        &lt;!-- SQL dialect --&gt;\r\n        &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;\/property&gt;\r\n \r\n        &lt;!-- Disable the second-level cache  --&gt;\r\n        &lt;property name=&quot;cache.provider_class&quot;&gt;org.hibernate.cache.internal.NoCacheProvider&lt;\/property&gt;\r\n \r\n        &lt;!-- Echo all executed SQL to stdout --&gt;\r\n        &lt;property name=&quot;show_sql&quot;&gt;true&lt;\/property&gt;\r\n \r\n        &lt;!-- Drop and re-create the database schema on startup --&gt;\r\n        &lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;\/property&gt;\r\n         \r\n        &lt;!-- Name of the Annotated Entity class --&gt;\r\n        &lt;mapping class=&quot;com.mugil.dto.UserDetails&quot;\/&gt;\r\n    &lt;\/session-factory&gt;\r\n&lt;\/hibernate-configuration&gt;\r\n\r\n<\/pre>\n<p><strong>hibernate.properties<\/strong><\/p>\n<pre>\r\nhibernate.connection.driver_class = com.mysql.jdbc.Driver\r\nhibernate.connection.url = jdbc:mysql:\/\/localhost:3307\/JH\r\nhibernate.dialect = org.hibernate.dialect.MySQL5Dialect\r\n<\/pre>\n<p>We must let Hibernate know our mapping definition files by including an element mapping property in the previous config file, as shown here:<br \/>\n<strong>hibernate.cfg.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;hibernate-configuration&gt;\r\n &lt;session-factory&gt;\r\n ...\r\n &lt;mapping resource=&quot;table1.hbm.xml&quot; \/&gt;\r\n &lt;mapping resource=&quot;table2.hbm.xml&quot; \/&gt;\r\n &lt;mapping resource=&quot;table3.hbm.xml&quot; \/&gt;\r\n &lt;\/session-factory&gt;\r\n&lt;\/hibernate-configuration&gt;\r\n<\/pre>\n<p>Once we have the connection configuration ready, the next step is to prepare the table1.hbm.xml file consisting of object-table mapping definitions<br \/>\n<strong>XML Mapping<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;hibernate-mapping&gt;\r\n &lt;class name=&quot;com.java.latte.table1&quot; table=&quot;TABLE1&quot;&gt;\r\n &lt;id name=&quot;id&quot; column=&quot;ID&quot;&gt;\r\n &lt;generator class=&quot;native&quot;\/&gt;\r\n &lt;\/id&gt;\r\n &lt;property name=&quot;title&quot; column=&quot;TITLE&quot;\/&gt;\r\n &lt;property name=&quot;director&quot; column=&quot;DIRECTOR&quot;\/&gt;\r\n &lt;property name=&quot;synopsis&quot; column=&quot;SYNOPSIS&quot;\/&gt;\r\n &lt;\/class&gt;\r\n&lt;\/hibernate-mapping&gt;\r\n<\/pre>\n<ol>\n<li>The Hibernate framework reads the hibernate.cfg.xml file to create a SessionFactory, which is thread-safe global factory class for creating Sessions. We should ideally create a single SessionFactory and share it across the application.SessionFactory is defined for one, and only one, database.<\/li>\n<li>SessionFactory is to create Session objects.It is the Session\u2019s job to take care of all database operations such as saving, loading, and retrieving records from relevant tables.<em>Session objects are not thread-safe <\/em>and therefore should not be shared across different classes.\n<\/li>\n<li> The Session wraps the underlying JDBC connection or J2EE data source, and it serves as a first-level cache for persistent objects bound to it.\n<\/li>\n<li>Hibernate specifies how each object state is retrieved and stored in the database via an XML configuration file. Hibernate mappings are loaded at startup and are cached in the SessionFactory. Each mapping specifies a variety of parameters related to the persistence lifecycle of instances of the mapped class <\/li>\n<li><\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2019\/04\/hibernate_architecture.jpg\" alt=\"null\" height=\"428\" width=\"460\"\/><\/p>\n<p>More on hibernate Object States <a href=\"http:\/\/codethataint.com\/blog\/hibernate-object-states\/\">here<\/a><br \/>\nMore on hibernate Object types <a href=\"codethataint.com\/blog\/difference-between-createquery-vs-createsqlquery-vs-createcriteria\/\">here<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The heart of any Hibernate application is in its configuration. There are two pieces of configuration required in any Hibernate application: one creates the database connections, and the other creates the object-to-table mapping To create a connection to the database, Hibernate must know the details of our database, tables, classes, and other mechanics. This information&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-hibernate-works\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[],"class_list":["post-3313","post","type-post","status-publish","format-standard","hentry","category-concepts-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3313"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3313\/revisions"}],"predecessor-version":[{"id":3321,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3313\/revisions\/3321"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}