64

Is it possible to select, say, only properties A and B from an object using a JPA query without using criteria queries?

To select all properties I'd just do something like:

SELECT i FROM ObjectName i WHERE i.id = 10

But I have an object with many properties on a legacy system, and want to select just a few even though I'm aware selecting several properties is usually quick.

Is this possible without using criteria queries?

2
  • errm, you can do anything with string-based JPQL as you can with criteria. No idea why anyone would think otherwise. Jul 12, 2014 at 8:03
  • Projections in spring-data can be used to achieve the same. Jul 7, 2017 at 5:38

7 Answers 7

106

Yes, like in plain sql you could specify what kind of properties you want to select:

SELECT i.firstProperty, i.secondProperty FROM ObjectName i WHERE i.id=10

Executing this query will return a list of Object[], where each array contains the selected properties of one object.

Another way is to wrap the selected properties in a custom object and execute it in a TypedQuery:

String query = "SELECT NEW CustomObject(i.firstProperty, i.secondProperty) FROM ObjectName i WHERE i.id=10";
TypedQuery<CustomObject> typedQuery = em.createQuery(query , CustomObject.class);
List<CustomObject> results = typedQuery.getResultList();

Examples can be found in this article.

UPDATE 29.03.2018:

@Krish:

@PatrickLeitermann for me its giving "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate class ***" exception . how to solve this ?

I guess you’re using JPA in the context of a Spring application, don't you? Some other people had exactly the same problem and their solution was adding the fully qualified name (e. g. com.example.CustomObject) after the SELECT NEW keywords.

Maybe the internal implementation of the Spring data framework only recognizes classes annotated with @Entity or registered in a specific orm file by their simple name, which causes using this workaround.

6
  • 5
    So i need n kind of pojo for the same table? one for every need? i can't use just one object for all, leaving some fields null? Nov 25, 2015 at 14:53
  • what is alternative to TypedQuery ? I need to work on jpa 1.0. There is no typed query over there. Apr 4, 2016 at 9:46
  • @AndreaScarafoni, you can have multiples constructors on the same class and use it for each case.
    – Hugo Baés
    Oct 22, 2016 at 8:34
  • @PatrickLeitermann for me its giving "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate class ***" exception . how to solve this ?
    – Krish
    Mar 29, 2018 at 9:02
  • No, at the time I wasn't using Spring.
    – Edy Bourne
    May 22, 2018 at 20:52
26

You can use something like this:

List<Object[]> list = em.createQuery("SELECT p.field1, p.field2 FROM Entity p").getResultList();

then you can iterate over it:

for (Object[] obj : list){
    System.out.println(obj[0]);
    System.out.println(obj[1]);
}

BUT if you have only one field in query, you get a list of the type not from Object[]

15

Projections can be used to select only specific properties(columns) of an entity object.

From the docs

Spring Data Repositories usually return the domain model when using query methods. However, sometimes, you may need to alter the view of that model for various reasons. In this section, you will learn how to define projections to serve up simplified and reduced views of resources.

Define an interface with only the getters you want.

interface CustomObject {  
    String getA(); // Actual property name is A
    String getB(); // Actual property name is B 
}

Now return CustomObject from your repository like so :

public interface YOU_REPOSITORY_NAME extends JpaRepository<YOUR_ENTITY, Long> {
    CustomObject findByObjectName(String name);
}
2
  • I am trying to the same in micronaut but it shows ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Error instantiating type [com.example.transactions.T ransaction$DateOfTransactionAndDateAndAmount] from introspection: No default constructor exists Apr 15, 2022 at 10:13
  • @ajinzrathod Its better to post a seperate question for that. Apr 15, 2022 at 10:33
8

Excellent answer! I do have a small addition. Regarding this solution:

TypedQuery<CustomObject> typedQuery = em.createQuery(query , String query = "SELECT NEW CustomObject(i.firstProperty, i.secondProperty) FROM ObjectName i WHERE i.id=100";
TypedQuery<CustomObject> typedQuery = em.createQuery(query , CustomObject.class);
List<CustomObject> results = typedQuery.getResultList();CustomObject.class);

To prevent a class not found error simply insert the full package name. Assuming org.company.directory is the package name of CustomObject:

String query = "SELECT NEW org.company.directory.CustomObject(i.firstProperty, i.secondProperty) FROM ObjectName i WHERE i.id=10";
TypedQuery<CustomObject> typedQuery = em.createQuery(query , CustomObject.class);
List<CustomObject> results = typedQuery.getResultList();
0

I suppose you could look at this link if I understood your question correctly http://www.javacodegeeks.com/2012/07/ultimate-jpa-queries-and-tips-list-part_09.html

For example they created a query like:

 select id, name, age, a.id as ADDRESS_ID, houseNumber, streetName ' +
 20' from person p join address a on a.id = p.address_id where p.id = 1'
0

Yes, it is possible. All you have to do is change your query to something like SELECT i.foo, i.bar FROM ObjectName i WHERE i.id = 10. The result of the query will be a List of array of Object. The first element in each array is the value of i.foo and the second element is the value i.bar. See the relevant section of JPQL reference.

0

I was facing the same problem. I had a users table with an id column (the primary key) and a name column. I wanted to get the name field of all records, so I tried both findName and findNames but both did not work.

My solution was to create an interface with only a getName() method in the entity class:

@Entity
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
@Table(name = "users")
public class User {
    @Id
    private String id;
    private String name;
    ...

    public interface UserName {
        public String getName();
    }
}    

Then use "id not null" as the filter in my JPA repository:

public interface UserRepository extends JpaRepository<User, Integer> {
    ...
    User.UserName findNameByIdNotNull();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.