{"id":4816,"date":"2018-07-02T17:19:03","date_gmt":"2018-07-02T11:49:03","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4816"},"modified":"2023-01-20T18:55:39","modified_gmt":"2023-01-20T13:25:39","slug":"hibernate-query-language","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/","title":{"rendered":"Hibernate Query Language"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">In this tutorial will discuss about Hibernate Query Language. HQL is an object-oriented query language. Hibernate Query Language(HQL) is same as Structured Query language(SQL), except that in HQL we use entity class name instead of table name.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">HQL provides its own syntax and grammar <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ex. \u00a0<span class=\"lang:default decode:true crayon-inline\">From Employee emp<\/span>\u00a0<\/span><span style=\"font-weight: 400;\">. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">Where Employee is an entity class which are associate with employee table and emp is alias name of Employee class.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It is alternative of SQL query <span class=\"lang:default decode:true crayon-inline\">Select* from employe<\/span>\u00a0<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><b>\u00a0<\/b><span style=\"font-weight: 400;\">All hibernate query are translated by hibernate in to structured query for further processing. Hibernate also provide a way to use structured query in hibernate. HQL syntax is not case sensitive but class name and variables name are case sensitive.<\/span><\/p>\n<p><b><span style=\"font-weight: 400;\">Query Interface: It is <\/span><span style=\"font-weight: 400;\">n object-oriented representation of a Hibernate query. We can get <\/span><span style=\"font-weight: 400;\">Query<\/span><span style=\"font-weight: 400;\"> instance by <\/span><span style=\"font-weight: 400;\">Session.createQuery().<\/span>\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">This interface provide the various method\u00a0 :<\/span><\/p>\n<ol>\n<li><strong>public int\u00a0\u00a0executeUpdate()<\/strong> &#8211;\u00a0Execute the update or delete statement.<\/li>\n<li><strong>public String\u00a0getNamedParameters()<\/strong> &#8211;\u00a0It return the names of all named parameters of the query.<\/li>\n<li><strong>public String[]\u00a0getQueryString()<\/strong> &#8211;\u00a0Get the query string.<\/li>\n<li><strong>public List\u00a0list<\/strong><span style=\"font-size: 1rem;\"><strong>()<\/strong> &#8211;<\/span>\u00a0 It return the query results as a List.<\/li>\n<li><strong>public Query\u00a0setMaxResults(int maxResults)<\/strong> &#8211;\u00a0 It is used to <span style=\"font-size: 1rem;\">set the maximum number of rows to retrieve.<\/span><\/li>\n<li><strong>public Object\u00a0uniqueResult()<\/strong><span style=\"font-size: 1rem;\">\u00a0&#8211; It is used to return a single instance that matches the query, or null if the query returns no results.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Let\u2019s take a look of CRUD operations using HQL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Select Query: <\/span><span style=\"font-weight: 400;\">Retrieve an employee detail where employee id is 255. <\/span><\/p>\n<pre class=\"lang:java decode:true\">Query query = session.createQuery(\"From Employee where employeeId= :id \");\r\nquery.setParameter(\"id\", \"255\");\r\nList list = query.list();<\/pre>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\">Update Query : <\/span><span style=\"font-weight: 400;\">Update Employee name where id is 255.<\/span><\/li>\n<\/ol>\n<pre class=\"lang:java decode:true\">Query query = session.createQuery(\"Update Employee set employeeName =:eName\" + \u00a0\" where employeeId= :id\");\r\nquery.setParameter(\"employeeName \", \"AKASH\");\r\nquery.setParameter(\"id\", \"255\");\r\nint result = query.executeUpdate();<\/pre>\n<p><span style=\"font-weight: 400;\">3.DELETE Query: Delete employee where id is 255<\/span><\/p>\n<pre class=\"lang:java decode:true\">Query query = session.createQuery(\"Delete Employee where employeeName = :id\");\r\nquery.setParameter(\":id\", \"255\");\r\nint result = query.executeUpdate();<\/pre>\n<ol start=\"4\">\n<li><span style=\"font-weight: 400;\"> Insert Query : In HQL , we can only insert values from another table.<\/span><\/li>\n<\/ol>\n<pre class=\"lang:default decode:true\">Query query = session.createQuery(\"insert into Employee (employee_code, employee_name)\" +\"select employee_code, employee_name from employee_info\");\r\nint result = query.executeUpdate();\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Note: The query.executeUpdate() will return how many number of record has been inserted, updated or deleted.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In this tutorial will discuss about Hibernate Query Language. HQL is an object-oriented query language. Hibernate Query Language(HQL) is same as Structured Query language(SQL), except that in HQL we use entity class name instead of table name. HQL provides its own syntax and grammar Ex. \u00a0From Employee emp\u00a0. Where Employee is an entity class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4841,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[265,282,281,224,216],"class_list":["post-4816","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile","tag-hibernate","tag-hibernate-query","tag-hql","tag-java","tag-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hibernate Query Language - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hibernate Query Language - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"&nbsp; In this tutorial will discuss about Hibernate Query Language. HQL is an object-oriented query language. Hibernate Query Language(HQL) is same as Structured Query language(SQL), except that in HQL we use entity class name instead of table name. HQL provides its own syntax and grammar Ex. \u00a0From Employee emp\u00a0. Where Employee is an entity class [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-02T11:49:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1137\" \/>\n\t<meta property=\"og:image:height\" content=\"305\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Hibernate Query Language\",\"datePublished\":\"2018-07-02T11:49:03+00:00\",\"dateModified\":\"2023-01-20T13:25:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/\"},\"wordCount\":299,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/akash_blog_2.png\",\"keywords\":[\"Hibernate\",\"Hibernate Query\",\"HQL\",\"java\",\"sql\"],\"articleSection\":[\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/\",\"name\":\"Hibernate Query Language - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/akash_blog_2.png\",\"datePublished\":\"2018-07-02T11:49:03+00:00\",\"dateModified\":\"2023-01-20T13:25:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/akash_blog_2.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/akash_blog_2.png\",\"width\":1137,\"height\":305},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/hibernate-query-language\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hibernate Query Language\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hibernate Query Language - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/","og_locale":"en_US","og_type":"article","og_title":"Hibernate Query Language - InnovationM - Blog","og_description":"&nbsp; In this tutorial will discuss about Hibernate Query Language. HQL is an object-oriented query language. Hibernate Query Language(HQL) is same as Structured Query language(SQL), except that in HQL we use entity class name instead of table name. HQL provides its own syntax and grammar Ex. \u00a0From Employee emp\u00a0. Where Employee is an entity class [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-07-02T11:49:03+00:00","article_modified_time":"2023-01-20T13:25:39+00:00","og_image":[{"width":1137,"height":305,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Hibernate Query Language","datePublished":"2018-07-02T11:49:03+00:00","dateModified":"2023-01-20T13:25:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/"},"wordCount":299,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png","keywords":["Hibernate","Hibernate Query","HQL","java","sql"],"articleSection":["Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/","url":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/","name":"Hibernate Query Language - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png","datePublished":"2018-07-02T11:49:03+00:00","dateModified":"2023-01-20T13:25:39+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/akash_blog_2.png","width":1137,"height":305},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/hibernate-query-language\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Hibernate Query Language"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4816"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4816\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4841"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}