{"id":887,"date":"2020-01-07T10:31:15","date_gmt":"2020-01-07T10:31:15","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=887"},"modified":"2020-01-07T12:13:17","modified_gmt":"2020-01-07T12:13:17","slug":"hibernate-5-4-configuration","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/hibernate-5-4-configuration\/","title":{"rendered":"Hibernate 5.4 Configuration"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Core Hibernate 5.4 Configuration<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\r\n  xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n  xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\r\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n  &lt;groupId&gt;HibernateProj&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;HibernateProj&lt;\/artifactId&gt;\r\n  &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\r\n  &lt;build&gt;\r\n    &lt;sourceDirectory&gt;src&lt;\/sourceDirectory&gt;\r\n    &lt;plugins&gt;\r\n      &lt;plugin&gt;\r\n        &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n        &lt;version&gt;3.8.0&lt;\/version&gt;\r\n        &lt;configuration&gt;\r\n          &lt;source&gt;1.8&lt;\/source&gt;\r\n          &lt;target&gt;1.8&lt;\/target&gt;\r\n        &lt;\/configuration&gt;\r\n      &lt;\/plugin&gt;\r\n    &lt;\/plugins&gt;\r\n  &lt;\/build&gt;\r\n  &lt;dependencies&gt;\r\n    &lt;!-- https:\/\/mvnrepository.com\/artifact\/org.hibernate\/hibernate-core --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;hibernate-core&lt;\/artifactId&gt;\r\n      &lt;version&gt;5.4.10.Final&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n\r\n    &lt;!-- https:\/\/mvnrepository.com\/artifact\/mysql\/mysql-connector-java --&gt;\r\n    &lt;dependency&gt;\r\n      &lt;groupId&gt;mysql&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt;\r\n      &lt;version&gt;8.0.15&lt;\/version&gt;\r\n    &lt;\/dependency&gt;\r\n  &lt;\/dependencies&gt;\r\n&lt;\/project&gt;<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;!DOCTYPE hibernate-configuration PUBLIC\r\n\"-\/\/Hibernate\/Hibernate Configuration DTD 3.0\/\/EN\"\r\n\"http:\/\/hibernate.sourceforge.net\/hibernate-configuration-3.0.dtd\"&gt;\r\n&lt;hibernate-configuration&gt;\r\n    &lt;session-factory&gt;\r\n    \t&lt;!-- &lt;property name=\"hibernate.current_session_context_class\"&gt;thread&lt;\/property&gt; --&gt;\r\n    \t&lt;property name=\"hibernate.current_session_context_class\"&gt;org.hibernate.context.internal.ThreadLocalSessionContext&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.bytecode.use_reflection_optimizer\"&gt;false&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.connection.driver_class\"&gt;com.mysql.jdbc.Driver&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.connection.password\"&gt;&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.connection.url\"&gt;jdbc:mysql:\/\/localhost:3306\/test&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.connection.username\"&gt;root&lt;\/property&gt;\r\n        &lt;property name=\"hibernate.dialect\"&gt;org.hibernate.dialect.MySQLDialect&lt;\/property&gt;\r\n        &lt;property name=\"show_sql\"&gt;true&lt;\/property&gt;\r\n    &lt;\/session-factory&gt;\r\n&lt;\/hibernate-configuration&gt;\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.ResultSet;\r\nimport java.sql.Statement;\r\nimport java.util.List;\r\n\r\nimport org.hibernate.Criteria;\r\nimport org.hibernate.Session;\r\nimport org.hibernate.cfg.Configuration;\r\nimport org.hibernate.query.Query;\r\n\r\npublic class MainClass {\r\n  @SuppressWarnings(\"deprecation\")\r\n  public static void main(String[] args) {\r\n\r\n    try{  \r\n      Class.forName(\"com.mysql.cj.jdbc.Driver\");  \r\n      Connection con=DriverManager.getConnection(  \r\n          \"jdbc:mysql:\/\/localhost:3306\/test\",\"root\",\"\");  \r\n      \/\/here sonoo is database name, root is username and password  \r\n      Statement stmt=con.createStatement();\r\n\/\/\t\t\tstmt.executeUpdate(\"insert into emp VALUES (null, 'shailesh', 31, 'Nagpur')\");\r\n      ResultSet rs=stmt.executeQuery(\"select * from emp\");  \r\n      while(rs.next())\r\n        System.out.println(rs.getString(1)+\"  \"+rs.getString(2)+\"  \"+rs.getString(3)+\"  \"+rs.getString(4));  \r\n      con.close();  \r\n    } catch(Exception e){ System.out.println(e);}  \r\n\r\n    System.out.println(\"Hibernate connection established successfully...\");\r\n\r\n    Session session = new Configuration().configure(\"hibernate.cfg.xml\").buildSessionFactory().openSession();\r\n    Query query = session.createNativeQuery(\"select * from emp\");\r\n    List&lt;Student[]&gt; list = query.list();\r\n    System.out.println(list);\r\n\r\n    for(Object[] obj : list) {\r\n      Student std = new Student((Integer)obj[0], (String)obj[1], (Integer)obj[2], (String)obj[3]);\r\n      System.out.println(std);\r\n    }\t\t\r\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    Query query2 = session.createNativeQuery(\"Select id, name, age, city from emp\");\r\n    query2.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);\r\n    List&lt;Student&gt; students = query2.list();\r\n    System.out.println(students);\r\n    session.close();\r\n    \/\/==============================\/\/\r\n    Session session2 = new Configuration().configure(\"database.cfg.xml\").buildSessionFactory().openSession();\r\n    List list2 = session2.createNativeQuery(\"select * from testdmarc\").list();\r\n    System.out.println(list2);\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Reference:<\/p>\n<p>https:\/\/docs.jboss.org\/hibernate\/orm\/5.4\/userguide\/html_single\/Hibernate_User_Guide.html#sql<\/p>\n<p>https:\/\/tools.jboss.org\/downloads\/jbosstools\/2019-09\/4.13.0.Final.html#update_site<\/p>\n<p>https:\/\/www.tutorialspoint.com\/hibernate\/hibernate_examples.htm<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"0muvTpmIKU\"><p><a href=\"https:\/\/www.journaldev.com\/3422\/hibernate-native-sql-query-example\">Hibernate Native SQL Query Example<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/www.journaldev.com\/3422\/hibernate-native-sql-query-example\/embed#?secret=0muvTpmIKU\" data-secret=\"0muvTpmIKU\" width=\"600\" height=\"338\" title=\"&#8220;Hibernate Native SQL Query Example&#8221; &#8212; JournalDev\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/rKr7VI7GHaY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/kIVvIJfQQAs?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/JhsOHqzq2NQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/IOhvon4o_os?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/UdSqsNc1-Fg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p><iframe loading=\"lazy\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/KO_IdJbSJkI?start=386&#038;feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Core Hibernate 5.4 Configuration &lt;project xmlns=&#8221;http:\/\/maven.apache.org\/POM\/4.0.0&#8243; xmlns:xsi=&#8221;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#8221; xsi:schemaLocation=&#8221;http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&#8221;&gt; &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt; &lt;groupId&gt;HibernateProj&lt;\/groupId&gt; &lt;artifactId&gt;HibernateProj&lt;\/artifactId&gt; &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt; &lt;build&gt; &lt;sourceDirectory&gt;src&lt;\/sourceDirectory&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt; &lt;version&gt;3.8.0&lt;\/version&gt; &lt;configuration&gt; &lt;source&gt;1.8&lt;\/source&gt; &lt;target&gt;1.8&lt;\/target&gt; &lt;\/configuration&gt; &lt;\/plugin&gt; &lt;\/plugins&gt; &lt;\/build&gt; &lt;dependencies&gt; &lt;!&#8211; https:\/\/mvnrepository.com\/artifact\/org.hibernate\/hibernate-core &#8211;&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;\/groupId&gt; &lt;artifactId&gt;hibernate-core&lt;\/artifactId&gt; &lt;version&gt;5.4.10.Final&lt;\/version&gt; &lt;\/dependency&gt; &lt;!&#8211; https:\/\/mvnrepository.com\/artifact\/mysql\/mysql-connector-java &#8211;&gt; &lt;dependency&gt; &lt;groupId&gt;mysql&lt;\/groupId&gt; &lt;artifactId&gt;mysql-connector-java&lt;\/artifactId&gt; &lt;version&gt;8.0.15&lt;\/version&gt; &lt;\/dependency&gt; &lt;\/dependencies&gt; &lt;\/project&gt; &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC &#8220;-\/\/Hibernate\/Hibernate Configuration DTD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/887"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=887"}],"version-history":[{"count":5,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"predecessor-version":[{"id":892,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/887\/revisions\/892"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}