{"id":7953,"date":"2021-01-20T17:48:50","date_gmt":"2021-01-20T12:18:50","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=7953"},"modified":"2022-07-19T16:42:39","modified_gmt":"2022-07-19T11:12:39","slug":"what-are-the-hibernate-configuration-files","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/what-are-the-hibernate-configuration-files\/","title":{"rendered":"What are the Hibernate Configuration Files?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is HBM File?<\/strong><\/h2>\n\n\n\n<p>The full name of HBM&nbsp;is Hibernate Mapping.&nbsp;It is an XML&nbsp;<strong>file<\/strong>&nbsp;in which we define the mapping between POJO class to the database table and POJO class variables to table columns.<strong> <\/strong>The resource file&nbsp;<strong>hibernate.cfg.xml<\/strong>, which supports to represent the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/hibernate-configuration\/\" class=\"rank-math-link\">Hibernate configuration<\/a> information.<strong> <\/strong>The connection.driver_class, connection.URL, connection.username, and connection.password property element that characterizes the JDBC connection information.&nbsp;The connection.pool_size is used to configure Hibernate&#8217;s built-in connection pool how many connections to the pool.<strong> <\/strong>The Hibernate XML mapping file which includes the mapping correlation between the Java class and the database table.&nbsp;It is mostly named &#8220;xx.hbm.xml&#8221; and represents in the Hibernate configuration file &#8220;hibernate.cfg.xml.\u201d<\/p>\n\n\n\n<p>For example, the mapping file (hbm.xml) is mention in the &#8220;mapping&#8221; tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;hibernate-configuration&gt;\n&lt;session-factory&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.bytecode.use_reflection_optimizer\"&gt;false&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.connection.driver_class\"&gt;com.mysql.jdbc.Driver&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.connection.password\"&gt;password&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.connection.url\"&gt;jdbc:mysql:\/\/localhost:3306\/demo&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.connection.username\"&gt;root&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"hibernate.dialect\"&gt;org.hibernate.dialect.MySQLDialect&lt;\/property&gt;\n&nbsp;&nbsp;&lt;property name=\"show_sql\"&gt;true&lt;\/property&gt;\n&nbsp;&nbsp;&lt;mapping resource=\"com\/demo\/common\/HiberDemo.hbm.xml\"&gt;&lt;\/mapping&gt;\n&lt;\/session-factory&gt;\n&lt;\/hibernate-configuration&gt;\n<\/pre>\n\n\n\n<p>Add Hibernates mapping file (hbm.xml) programmatically:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Hibernate provides a method for the developer to add mapping files technically.<\/li><li>Let&#8217;s modify the default Hibernate<a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-session-management\/\" class=\"rank-math-link\"> SessionFactory class <\/a>by passing your \u201c<strong>hbm.xml<\/strong>\u201d file path as an argument into the addResource() method:<\/li><\/ul>\n\n\n\n<p>SessionFactory sessionFactory = new Configuration().addResource(&#8220;com\/demo\/commonStock.hbm.xml&#8221;).buildSessionFactor();<\/p>\n\n\n\n<p>HibernateUtil.java<\/p>\n\n\n\n<p>Example of HibernateUtil.java, load the Hibernate XML mapping file demo.hbm.xml\u201d programmatically:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import org.hibernate.SessionFactory;\nimport org.hibernate.cfg.Configuration;\n&nbsp;\npublic class HibernateUtil {\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static final SessionFactory sessionFactory = buildSessionFactory();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private static SessionFactory buildSessionFactory() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SessionFactory sessionFactory = new Configuration()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.configure(\"\/com\/demo\/persistence\/hibernate.cfg.xml\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addResource(\"com\/demo\/common\/Stock.hbm.xml\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.buildSessionFactory();\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sessionFactory;\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (Throwable e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(\"Initial SessionFactory creation failed.\" + e);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new ExceptionInInitializerError(e);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n<\/pre>\n\n\n\n<p>There are two property elements that declare the remaining two properties of the Event class: i.e., date and title.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Hibernate | Configuring Hibernate application | Java Interview Question | Core Java By H2K infosys\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/Xgqy3v8uu8s?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>The date property mapping includes the column attribute, but the title does not has. When there is an absence of a column attribute, Hibernate uses the property name as the column name. This is appropriate for the title, but since the date is a reserved keyword in most of the databases.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public static SessionFactory getSessionFactory() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sessionFactory;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public static void shutdown() {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Close caches and connection pools\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSessionFactory().close();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;\n}\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>There is the two way of mapping in hibernate &#8211; The first one which is by using the hibernate annotation and the second one which is by using the hbm.xml.<\/li><li>When we use hbm.xml, to just modify the default hibernate SessionFactory class in hibernate-cfg.xml bypassing your &#8220;hbm.xml&#8221; file path as an argument to resource method.<\/li><\/ul>\n\n\n\n<p><strong>Example:&nbsp;<\/strong>Here, we are taking an example of the employee table.<\/p>\n\n\n\n<p>Employee.hbm.xml file is your hbm.xml file in which table column and their types are mapped.<\/p>\n\n\n\n<p>Employee.hbm.xml<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;hibernate-mapping&gt;\n&lt;class name=\"net.roseindia.table.Employee\" table=\"employee\"&gt;\n&lt;id name=\"empId\" type=\"int\" column=\"emp_id\"&gt;\n&lt;generator class=\"native\" \/&gt;\n&lt;\/id&gt;\n&lt;property name=\"EmpName\" type=\"string\" column=\"emp_name\" \/&gt;\n&lt;property name=\"salary\" type=\"int\" column=\"emp_salary\" \/&gt;\n&lt;property name=\"designation\" type=\"string\" column=\"designation\" \/&gt;\n&lt;property name=\"address\" type=\"string\" column=\"address\" \/&gt;\n&lt;\/class&gt;\n&lt;\/hibernate-mapping&gt;\n<\/pre>\n\n\n\n<p>hibernate-cfg.xml is the configuration file of hibernate where you map hbm.xml<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;hibernate-configuration&gt;\n&lt;session-factory&gt;\n&lt;!-- Database connection settings --&gt;\n&lt;property name=\"connection.driver_class\"&gt;com.mysql.jdbc.Driver&lt;\/property&gt;\n&lt;property name=\"connection.url\"&gt;jdbc:mysql:\/\/localhost:3306\/hibernate&lt;\/property&gt;\n&lt;property name=\"connection.username\"&gt;root&lt;\/property&gt;\n&lt;property name=\"connection.password\"&gt;root&lt;\/property&gt;\n&lt;!-- JDBC connection pool (use the built-in) --&gt;\n&lt;property name=\"connection.pool_size\"&gt;1&lt;\/property&gt;\n&lt;!-- SQL dialect --&gt;\n&lt;property name=\"dialect\"&gt;org.hibernate.dialect.MySQLDialect&lt;\/property&gt;\n&lt;!-- Enable Hibernate's automatic session context management --&gt;\n&lt;property name=\"current_sesion_context_class\"&gt;thread&lt;\/property&gt;\n&lt;!-- Disable the second-level cache --&gt;\n&lt;property name=\"cache.provider_class\"&gt;org.hibernate.cache.NoCacheProvider&lt;\/property&gt;\n&lt;!-- Echo all executed SQL to stdout --&gt;\n&lt;property name=\"show_sql\"&gt;true&lt;\/property&gt;\n\n&lt;property name=\"hbm2ddl.auto\"&gt;none&lt;\/property&gt;\n&lt;mapping resource=\"Employee.hbm.xml\"\/&gt;\n&lt;\/session-factory&gt;\n&nbsp;\n&lt;\/hibernate-configuration&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is HBM File? The full name of HBM&nbsp;is Hibernate Mapping.&nbsp;It is an XML&nbsp;file&nbsp;in which we define the mapping between POJO class to the database table and POJO class variables to table columns. The resource file&nbsp;hibernate.cfg.xml, which supports to represent the Hibernate configuration information. The connection.driver_class, connection.URL, connection.username, and connection.password property element that characterizes the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7979,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-7953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=7953"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7979"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=7953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=7953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=7953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}