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 Tips: How to Map Native Query Results to Entities

Hibernate Tips: How to Map Native Query Results to Entities

Check out some Hibernate tips on how to map native query results to entities. Also take a look at a video.

Thorben Janssen user avatar by
Thorben Janssen
·
Oct. 30, 18 · Tutorial
Like (1)
Save
Tweet
Share
26.82K Views

Join the DZone community and get the full member experience.

Join For Free

Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. Some of the most popular tips are also available as a book.

If you have a question for a future Hibernate Tip post, please leave a comment below.

Question

My query is too complex for JPQL, and I have to use a native query. Is there a way to map the result of the query to managed entities?

Solution

If your query returns all columns that are mapped by an entity, you can tell Hibernate to map the result to a managed entity. Afterwards, you can use the entity in the same way as any other entity.

There are 2 options to define the mapping:

  1. You can use an implicit mapping if your query result uses the same column names as your entity mapping.
  2. You can create your own mapping if the column names do not match the entity mapping

Implicit Mapping

The implicit mapping is easier to use and the better approach for most use cases. You only need to provide the class of the entity as the second parameter to the createNativeQuery method.

Book b = (Book) em.createNativeQuery("SELECT * FROM book b WHERE id = 1", Book.class).getSingleResult();

Explicit Mapping If the column names of your query result do not match the column names of your entity mapping, you have to define the mapping yourself. You can do this with a @SqlResultSetMapping which specifies the mapping for each entity attribute.

@SqlResultSetMapping(
name = "BookMapping", 
entities = @EntityResult(
entityClass = Book.class, 
fields = {
@FieldResult(name = "id", column = "id"),
@FieldResult(name = "version", column = "version"),
@FieldResult(name = "title", column = "title"),
@FieldResult(name = "publishingDate", column = "publishingDate"),
@FieldResult(name = "publisher", column = "publisherid")}))

As you can see in the code snippet, the @SqlResultSetMapping requires a name and an @EntityResult annotation which defines the mapping to the entity.

You, therefore, need to specify the class of the entity and a set of @FieldResult annotation which define the mapping between the result set column and the entity attribute.

You can then use this mapping by providing its name as the 2nd parameter to the createNativeQuery method.

em.createNativeQuery("SELECT * FROM book b WHERE id = 1", "BookMapping").getSingleResult();
Database Hibernate

Published at DZone with permission of Thorben Janssen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • SAST: How Code Analysis Tools Look for Security Flaws
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin

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: