Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:26:08+00:00 2026-05-27T17:26:08+00:00

Is there a way in JPA to convert random select clause values using createNativeQuery

  • 0

Is there a way in JPA to convert random select clause values using createNativeQuery to a domain object. The domain object is not managed

I have the following sql query

select name, count(*) as cnt, sum(average_events)/count(*) as avg_events from (complex subquery)

I want to convert the values a,b,c into a domain object with three instance variables a, b and c. This domain object is not managed by JPA and hence does not have @Entity and no corresponding table.

Currently I am doing the following, which returns a list of objects.

 Query query = objectManager.getEntityManager().createNativeQuery(sqlStr);
 List resultList = query.getResultList();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:26:08+00:00Added an answer on May 27, 2026 at 5:26 pm

    Use this syntax:

    SELECT new foo.MyCustomObject(a, b, c) FROM ...
    

    Where MyCustomObject is any class with matching consdtructor:

    public class MyCustomObject {
      private final String name;
      private final int cnt;
      private final float avg;
      public MyCustomObject(String name, int cnt, float avg) {
        this.name = name;
        this.cnt = cnt;
        this.avg = avg;
      }
      //...getters
    }
    

    Scala bonus: equivalent class:

    class MyCustomObject(name: String, cnt: Int, avg: Float)
    //no, actually that's it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to specify using JPA that there should be multiple unique
Possible Duplicate: Specifying an index (non unique key) using JPA Is there a way
using Jboss5.1.x, EJB3.0, JPA. Is there a way of running a query with DBLINK
Is there way to map immutable Value objects like email address using JPA? @Immutable
Is there any way if executing batch updates (EntityManager persist() or merge()) using JPA
How to unit test JPA code? is there any way to generate Unit Test
Is there any way to check whether a file is locked without using a
Is there a way to make a query in jpa with a left join
Is there a generic way to tell JPA/Hibernate to transform all column names of
I'm using Hibernate JPA. Suppose I have these classes: AbstractPerson |--> ConcreteEmployee |--> ConcreteCustomer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.