{"id":59,"date":"2016-10-25T17:16:10","date_gmt":"2016-10-25T17:16:10","guid":{"rendered":"http:\/\/codeinsightacademy.com\/cprogramming\/?p=59"},"modified":"2016-10-25T17:37:41","modified_gmt":"2016-10-25T17:37:41","slug":"creating-hibernate-session-with-database","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/creating-hibernate-session-with-database\/","title":{"rendered":"Creating Hibernate Session with Database"},"content":{"rendered":"<ol>\n<li>model.dao.interfaces.EmployeeDAOInterface.java<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<pre>package model.dao.interfaces;\r\n\r\nimport java.util.List;\r\nimport model.Employee;\r\n\r\n\/**\r\n *\r\n * @author Shailesh Sonare\r\n *\/\r\npublic interface EmployeeDAOInterface {\r\n    public List getAllEmplyees();\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>2. model.dao.EmployeeDAO.java<\/p>\n<p>&nbsp;<\/p>\n<pre>package model.dao;\r\n\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\nimport model.Employee;\r\nimport model.dao.interfaces.EmployeeDAOInterface;\r\nimport org.hibernate.Session;\r\nimport org.hibernate.SessionFactory;\r\nimport org.hibernate.boot.registry.StandardServiceRegistryBuilder;\r\nimport org.hibernate.cfg.Configuration;\r\n\r\n\/**\r\n *\r\n * @author Shailesh Sonare\r\n *\/\r\npublic class EmployeeDAO implements EmployeeDAOInterface {\r\n\r\n    @Override\r\n    public List getAllEmplyees() {\r\n        Configuration cnf = new Configuration();\r\n        cnf.configure(\"hibernate.cfg.xml\");\r\n        \r\n        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cnf.getProperties());\r\n        \r\n        SessionFactory sf = cnf.buildSessionFactory(ssrb.build());\r\n        Session session = sf.openSession();\r\n        \r\n        String hql = \"FROM Employee where id = 2\";\r\n        \r\n        List list = session.createQuery(hql).list();\r\n        \r\n        for(Iterator iterator = list.iterator(); iterator.hasNext();) {\r\n            Employee e = (Employee)iterator.next();\r\n            \r\n            System.out.println(e.getName());\r\n        }\r\n        \r\n        return list;\r\n    }\r\n    \r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>model.dao.interfaces.EmployeeDAOInterface.java &nbsp; package model.dao.interfaces; import java.util.List; import model.Employee; \/** * * @author Shailesh Sonare *\/ public interface EmployeeDAOInterface { public List getAllEmplyees(); } &nbsp; 2. model.dao.EmployeeDAO.java &nbsp; package model.dao; import java.util.Iterator; import java.util.List; import model.Employee; import model.dao.interfaces.EmployeeDAOInterface; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; \/** * * @author Shailesh Sonare *\/ public class EmployeeDAO [&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\/59"}],"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=59"}],"version-history":[{"count":7,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":66,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/59\/revisions\/66"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}