{"id":323,"date":"2017-06-02T12:56:53","date_gmt":"2017-06-02T12:56:53","guid":{"rendered":"http:\/\/www.codeinsightacademy.com\/blog\/?p=323"},"modified":"2017-06-02T12:56:53","modified_gmt":"2017-06-02T12:56:53","slug":"hibernate-native-sql","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/c-programming\/hibernate-native-sql\/","title":{"rendered":"Hibernate Native SQL"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\/*\r\n * To change this license header, choose License Headers in Project Properties.\r\n * To change this template file, choose Tools | Templates\r\n * and open the template in the editor.\r\n *\/\r\npackage corehibernatedemo;\r\n\r\nimport java.text.DateFormat;\r\nimport java.text.ParseException;\r\nimport java.text.SimpleDateFormat;\r\nimport java.util.Date;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\nimport java.util.Map;\r\nimport model.Person;\r\nimport model.Student;\r\nimport model.Users;\r\nimport org.hibernate.Criteria;\r\nimport org.hibernate.Query;\r\nimport org.hibernate.SQLQuery;\r\nimport org.hibernate.Session;\r\nimport org.hibernate.SessionFactory;\r\nimport org.hibernate.Transaction;\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 CoreHibernateDemo {\r\n    \r\n    \/**\r\n     * @param args the command line arguments\r\n     *\/\r\n    public static void main(String[] args) throws ParseException {\r\n        \/\/ TODO code application logic here\r\n        \/*\r\n        Configuration cfg = new Configuration();\r\n        cfg.configure(\"hibernate.cfg.xml\");\r\n        \r\n        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());\r\n        \r\n        SessionFactory factory = cfg.buildSessionFactory(ssrb.build());\r\n        \r\n        Session session = factory.openSession();\r\n        \r\n        Transaction t = session.beginTransaction();\r\n        \r\n        List users = session.createQuery(\"from Users\").list();\r\n        System.out.println(\"done...\");\r\n        \r\n        for (Iterator iterator = users.iterator(); iterator.hasNext();) {\r\n            Users e = (Users) iterator.next();\r\n            System.out.println(e.getFirstName());\r\n        }\r\n        \r\n        \r\n        \r\n        Date bdate = new SimpleDateFormat(\"yyyy-MM-dd\").parse(\"1992-11-15\");\r\n        \r\n        Users  usr = new Users(\"Sunita\", \"Sonare\", bdate);\r\n        \r\n        String hql = \"UPDATE Users SET dob = :dob WHERE id = :id\";\r\n        \r\n        Query query = session.createQuery(hql);\r\n        query.setParameter(\"dob\", bdate);\r\n        query.setParameter(\"id\", 4);\r\n        query.executeUpdate();\r\n        \r\n        \/\/session.save(usr);\r\n        t.commit();\r\n        session.close();\r\n        System.out.println(\"DOne....\");\r\n        *\/\r\n        \r\n        \/\/Student s = new Student();\r\n        \r\n        Configuration cfg = new Configuration().configure(\"hibernate.cfg.xml\");\r\n        \r\n        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());\r\n        \r\n        SessionFactory factory = cfg.buildSessionFactory(ssrb.build());\r\n        \r\n        Session session = factory.openSession();\r\n        \r\n\/\/        String hql = \"FROM Users\";\r\n\/\/        List&lt;Users&gt; list = session.createQuery(hql).list();\r\n\r\n        String sql = \"SELECT first_name, last_name, dob FROM users\";\r\n        \r\n        SQLQuery query = session.createSQLQuery(sql);\r\n        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); \/\/ important when you are using alias &lt;key&gt;&lt;value&gt; pair\r\n\/\/        List&lt;Object[]&gt; rows = query.list();\r\n\/\/        \r\n\/\/        for(Object[] row : rows){\r\n\/\/            \r\n\/\/            Users u = new Users(row[0].toString(), row[1].toString(), new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\").parse(row[2].toString()));\r\n\/\/            System.out.println(u);\r\n\/\/            System.out.println(\"--------------\");\r\n\/\/        }\r\n\r\n        List list = query.list();\r\n        \r\n        for(Object obj : list){\r\n            Map map = (Map)obj;\r\n            System.out.println(\"\" + map.get(\"first_name\"));\r\n            System.out.println(\"\" + map.get(\"last_name\"));\r\n            System.out.println(\"\" + map.get(\"dob\"));\r\n            System.out.println(\"-------------------------\");\r\n        }\r\n    }\r\n    \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Users.java<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">package model;\r\n\/\/ Generated 30 May, 2017 5:20:27 PM by Hibernate Tools 4.3.1\r\n\r\n\r\nimport java.util.Date;\r\n\r\n\/**\r\n * Users generated by hbm2java\r\n *\/\r\npublic class Users  implements java.io.Serializable {\r\n\r\n\r\n     private Integer id;\r\n     private String firstName;\r\n     private String lastName;\r\n     private Date dob;\r\n\r\n    public Users() {\r\n    }\r\n\r\n    public Users(String firstName, String lastName, Date dob) {\r\n       this.firstName = firstName;\r\n       this.lastName = lastName;\r\n       this.dob = dob;\r\n    }\r\n   \r\n    public Integer getId() {\r\n        return this.id;\r\n    }\r\n    \r\n    public void setId(Integer id) {\r\n        this.id = id;\r\n    }\r\n    public String getFirstName() {\r\n        return this.firstName;\r\n    }\r\n    \r\n    public void setFirstName(String firstName) {\r\n        this.firstName = firstName;\r\n    }\r\n    public String getLastName() {\r\n        return this.lastName;\r\n    }\r\n    \r\n    public void setLastName(String lastName) {\r\n        this.lastName = lastName;\r\n    }\r\n    public Date getDob() {\r\n        return this.dob;\r\n    }\r\n    \r\n    public void setDob(Date dob) {\r\n        this.dob = dob;\r\n    }\r\n\r\n    @Override\r\n    public String toString() {\r\n        return \"Users{\" + \"id=\" + id + \", firstName=\" + firstName + \", lastName=\" + lastName + \", dob=\" + dob + '}';\r\n    }\r\n\r\n}\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. *\/ package corehibernatedemo; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import model.Person; import model.Student; import model.Users; import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/323"}],"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=323"}],"version-history":[{"count":1,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"predecessor-version":[{"id":324,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/323\/revisions\/324"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}