DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Hibernate Query Language

Hibernate Query Language

In this tutorial, we will discuss the Hibernate Query Language, which is an object-oriented query language.

Akash Tomar user avatar by
Akash Tomar
·
Jul. 18, 18 · Tutorial
Like (8)
Save
Tweet
Share
26.01K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, we will discuss the 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.  From Employee emp.

Where Employee is an entity class which is associated with employee table and emp is the alias name of Employee class.

It is an alternative to SQL query Select* from employee.

All hibernate queries are translated by hibernate into structured query for further processing. Hibernate also provides a way to use a structured query in hibernate. HQL syntax is not case sensitive, but class name and variable name are case sensitive.

Query Interface: It is n object-oriented representation of a Hibernate query. We can get Query instance by Session.createQuery(). 

This interface provides the various method:

  1. public int  executeUpdate() — Execute the update or delete statement.
  2. public String getNamedParameters() — It returns the names of all named parameters of the query.
  3. public String[] getQueryString() — Get the query string.
  4. public List list() —  It returns the query results as a List.
  5. public Query setMaxResults(int maxResults) — It is used to set the maximum number of rows to retrieve.
  6. public Object uniqueResult() — It is used to return a single instance that matches the query, or null if the query returns no results.

Let’s take a look at CRUD operations using HQL.

1. Select Query: Retrieve an employee detail where employee ID is 255.

Query query = session.createQuery("From Employee where employeeId= :id ");
query.setParameter("id", "255");
List list = query.list();
  1. Update Query: Update Employee name where ID is 255.
Query query = session.createQuery("Update Employee set employeeName =:eName" +  " where employeeId= :id");
query.setParameter("employeeName ", "AKASH");
query.setParameter("id", "255");
int result = query.executeUpdate();

3.DELETE Query: Delete employee where ID is 255.

Query query = session.createQuery("Delete Employee where employeeName = :id");
query.setParameter(":id", "255");
int result = query.executeUpdate();
  1. Insert Query: In HQL, we can only insert values from another table.
Query query = session.createQuery("insert into Employee (employee_code, employee_name)" +"select employee_code, employee_name from employee_info");
int result = query.executeUpdate();

Note: The query.executeUpdate() will return how many number of record has been inserted, updated, or deleted.

Database Hibernate Query language

Published at DZone with permission of Akash Tomar. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Ways That You Can Operate Record Beyond DTO [Video]
  • The New DevOps Performance Clusters
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them
  • 9 Ways You Can Improve Security Posture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: