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 6688165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:22:23+00:00 2026-05-26T05:22:23+00:00

I am using JPA in my web application and when I do not want

  • 0

I am using JPA in my web application and when I do not want the DB to return all of the object I do something like:

    Query query = super.entityManager.createQuery("select b.id, b.buyName from Buy b where b.id in (:list)");
    query.setParameter("list", buyIds);

    List<Object[]> result = query.getResultList();
    List<Buy> retval = new ArrayList<Buy>();

    for (Object[] item : result) {
        Buy buy = new Buy();
        buy.setId((Long)item[0]);
        buy.setBuyName((String)item[1]);
        retval.add(buy);
    }

    return retval;

That is , querying the fields I want and then manually creating a list of the desired object and populating it.
Is this a good practice? It feels like there is a better way of doing this.

Thank you.

  • 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-26T05:22:24+00:00Added an answer on May 26, 2026 at 5:22 am

    It’s possible to directly return a new object from the query, with just the information that you need. For instance, your query would look like this:

    select new BuyDTO(b.id, b.buyName) from Buy b where b.id in (:list) 
    

    And the data transfer object would include only the needed fields:

    public class BuyDTO {
    
        private Long id;
        private String buyName;
    
        public BuyVO(Long id, String name) {
            this.id = id;
            this.buyName = name;
        }
    
        // getters and setters
    
    }
    

    When the query is run, it will return a List<BuyDTO>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application using JPA and JTA with Spring. I would like
My web application is using Java, Hibernate's JPA implementation (EntityManager) and Spring. What are
Here's what I'm trying to do. I'm using JPA persistence in a web application,
I want to develop a web application,I work alone (not with a team),so I
I have a fairly standard Java EE6 web application using JPA 2 with dependency
I'm developing an example web-application, using JPA 2.0 entities, Hibernate 3.6.2 and Spring 3.
I'm using Hibernate for a Java-based Web Application and want to add full-text search
I am developping a web application using Spring (3.1.x), JSF 2, JPA 2 (Hibernate
I have created a simple java web (Vaadin) application project using JPA and it
I'm using JPA to load and persist entities in my Java EE-based web application.

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.