{"id":503,"date":"2014-08-06T14:34:55","date_gmt":"2014-08-06T14:34:55","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=503"},"modified":"2016-06-13T11:27:28","modified_gmt":"2016-06-13T11:27:28","slug":"hibernate-creating-table","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/hibernate-creating-table\/","title":{"rendered":"Hibernate creating table from bean"},"content":{"rendered":"<p>Simple Hibernate Table Creation from Scratch<\/p>\n<figure id=\"attachment_506\" class=\"wp-caption thumbnail alignnone\" style=\"width: 444px;\">\n\t\t\t\t<a href=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2014\/08\/1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2014\/08\/1.png\" alt=\"Hibernate Required Jar Files List\" width=\"444\" height=\"467\" class=\"size-full wp-image-506\" \/><\/a>\n\t\t\t\t<figcaption class=\"wp-caption-text\">Hibernate Required Jar Files List<\/figcaption>\n\t\t\t<\/figure>\n<p>Step1: Create a Bean for which Table should be created in Database<br \/>\nStep2: Create a Class which uses the bean. <\/p>\n<p><strong>Step 1<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.dto;\r\n\r\nimport javax.persistence.Entity;\r\nimport javax.persistence.Id;\r\n\r\n@Entity \r\npublic class UserDetails {\r\n\t@Id\t\r\n\tprivate int userId;\r\n\tprivate String userName;\r\n\t\r\n\tpublic int getUserId() {\r\n\t\treturn userId;\r\n\t}\r\n\t\r\n\tpublic void setUserId(int userId) {\r\n\t\tthis.userId = userId;\r\n\t}\r\n\t\r\n\tpublic String getUserName() {\r\n\t\treturn userName;\r\n\t}\r\n\t\r\n\tpublic void setUserName(String userName) {\r\n\t\tthis.userName = userName;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Step 2<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.access;\r\n\r\nimport org.hibernate.Session;\r\nimport org.hibernate.SessionFactory;\r\nimport org.hibernate.boot.registry.StandardServiceRegistryBuilder;\r\nimport org.hibernate.cfg.Configuration;\r\nimport org.hibernate.service.ServiceRegistry;\r\n\r\nimport com.mugil.dto.UserDetails;\r\n\r\npublic class CreateUser {\r\n\tprivate static ServiceRegistry serviceRegistry;\r\n\tprivate static SessionFactory sessionFactory;\r\n\t\r\n\tpublic static void main(String&#x5B;] args) {\r\n\t\tUserDetails objUserDetail =  new UserDetails();\r\n\t\tobjUserDetail.setUserId(101);\r\n\t\tobjUserDetail.setUserName(&quot;Mugil&quot;);\r\n\t\t\r\n\t\tSessionFactory sessionFact = createSessionFactory();\r\n\t\tSession session = sessionFact.openSession();\r\n\t\tsession.beginTransaction();\r\n\t\tsession.save(objUserDetail);\r\n\t\tsession.getTransaction().commit();\t\r\n\t}\r\n\t\r\n\t\r\n\tpublic static SessionFactory createSessionFactory() {\r\n\t    Configuration configuration = new Configuration();\r\n\t    configuration.configure();\r\n\t    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(\r\n\t            configuration.getProperties()).build();\r\n\t    sessionFactory = configuration.buildSessionFactory(serviceRegistry);\r\n\t    return sessionFactory;\r\n\t}\r\n}\r\n<\/pre>\n<p>In Some cases the hibernate.cfg.xml might become unrecognized.In such case the code should be changed to force the config to be picked from the file location.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static SessionFactory createSessionFactory() \r\n{\r\n  Configuration configuration = new Configuration().configure();\t  \r\n  configuration.configure(&quot;hibernate.cfg.xml&quot;);\t  \r\n  configuration.addAnnotatedClass(com.mugil.tutor.UserDetails.class);\r\n\t  \r\n  serviceRegistry = new   StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();\r\n  sessionFactory = configuration.buildSessionFactory(serviceRegistry);\r\n  return sessionFactory;\r\n}\r\n<\/pre>\n<p>Hibernate uses SessionFactory pattern internally as below<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nSessionFactory sessionFact = createSessionFactory();\r\nSession session = sessionFact.openSession();\r\nsession.beginTransaction();\r\nsession.save(objUserDetail);\r\nsession.getTransaction().commit();\t\r\n<\/pre>\n<p>1.Create Object for SessionFactory<br \/>\n2.Open Session to begin Transaction<br \/>\n3.Begin Transaction using beginTransaction() Method<br \/>\n4.Save the Object by Passing Object of the bean<br \/>\n5.Complete the Transaction using commit<\/p>\n<p><strong>Annotations<\/strong><br \/>\n@Entity &#8211; Means entity as a whole>table would be created by the Name of the Entity<br \/>\n@Id &#8211; Tells the Primary Key<\/p>\n<p><strong>Having a Different table name from Class Name<\/strong><br \/>\n<strong>Annotations<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity(name=&quot;User_Details&quot;)\r\npublic class Users \r\n{\r\n .\r\n .\r\n}\r\n<\/pre>\n<p><strong>Table with User_Details would be created instead of  Users<\/strong><\/p>\n<p><strong>Having a Different Column name from Object Name<\/strong><br \/>\n<strong>Annotations<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity(name=&quot;User_Details&quot;)\r\npublic class Users \r\n{\r\n @Id\r\n @Column(name=&quot;USER_ID&quot;)\r\n private String UserId;\r\n .\r\n .\r\n}\r\n<\/pre>\n<p><strong>Columns with User_Id would be created instead of  UserId<\/strong><\/p>\n<p><strong>Appending String to Getters<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic void setName(String name) \r\n{\r\n  Name = name + &quot; Append Test &quot;;\r\n}\r\n<\/pre>\n<p><strong>Appending String to Getters<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity\r\n@Table (name=&quot;User_Details&quot;)\r\npublic class Users \r\n{\r\n\r\n}\r\n<\/pre>\n<p>The Entity Name Still remains the same but the table Name is different.<\/p>\n<p><strong>@Basic Annotation &#8211; Tells Hibernate to persist which it does by default<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Users \r\n{\r\n @Basic\r\n private String UserName;\r\n . \r\n .\r\n}\r\n<\/pre>\n<p><em>@Basic has 2 Parameters &#8211; Fetch, optional<\/em>. The only time you use @Basic is while applying the above options. <\/p>\n<p><strong>@Transient Annotation &#8211; Tells Hibernate to not store data in database<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Users \r\n{\r\n @Transient\r\n private String UserName;\r\n . \r\n .\r\n}\r\n<\/pre>\n<p><strong>@Temporal Annotation &#8211; Tells Hibernate to specify Date or Time<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Users \r\n{\r\n @Temporal (TemporalType.Date)\r\n private String joinedDate;\r\n . \r\n .\r\n}\r\n<\/pre>\n<p>Without  @Temporal the joinedDate is store along with TimeStamp in DB. Now using TemporalType(which is ENUM) you can select the type of data which can be stored in  Database.<\/p>\n<p><strong>@Lob &#8211; Tells Hibernate to specify Date or Time<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Users \r\n{\r\n @Lob\r\n private String Address;\r\n . \r\n .\r\n}\r\n<\/pre>\n<p>Tells the database field should be created as CLOB instead of VARCHAR(255).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/06\/Hibernate4.png\" alt=\"\" width=\"1198\" height=\"628\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simple Hibernate Table Creation from Scratch Step1: Create a Bean for which Table should be created in Database Step2: Create a Class which uses the bean. Step 1 package com.mugil.dto; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class UserDetails { @Id private int userId; private String userName; public int getUserId() { return userId; } public void&hellip; <a href=\"https:\/\/codethataint.com\/blog\/hibernate-creating-table\/\">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":[74],"tags":[],"class_list":["post-503","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/503","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=503"}],"version-history":[{"count":19,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}