Spring JPA Selecting Specific Columns

Select specific columns in JPA Spring Boot

Instead of returning Page you can change the return type to Page<Object[]> :

@Query(value = "SELECT name, image FROM characters", countQuery = "SELECT COUNT(name) FROM characters", nativeQuery = true)
Page<Object[]> getCharacters(Pageable pageable);

I want to select specific columns specified by user in Spring boot JPA and Angular 7

For dynamic filters you can use Query By Example in simple cases or Specifications in more advanced cases.

Limiting the columns selected is normally done in Spring Data JPA by using a projection interface as return value.
But doing that dynamically is not possible and either way you can't combine it with the options for dynamic where clauses.

Therefore the solution is to implement a custom method, get an EntityManager injected, construct your query as required using the Criteria API or String concatenation of a JPQL statement (not recommended due to the risk of SQL injection).

Fetching specific columns from multiple joined tables in Spring Data JPA

For projections, you can try a "select new".
I assume the MessageData is in package com.foo :

select new com.foo.MessageData(ab.status, buyer_rate, buyer_name, buyer_tel, bid_price, ADDTIME(complete_dt,"23:00:0.000000"), brand, model) FROM ...

JPA Query selecting only specific columns without using Criteria Query?

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.

how to select specific column which is an object in jpa spring boot

I have could solve it, i create a interface projection, like this...

public interface MovieCharactersDto {
CharacterEntity getCharacter();
interface CharacterEntity {
Long getId();
String getName();
String getImage();
BigDecimal getWeight();
Integer getAge();
String getStory();
}
}

And the query was as follows:

@Repository
public interface MovieDetailRepository extends JpaRepository<MovieDetailEntity, Long> {
Page<MovieCharactersDto> findByMovie(Pageable pageable, MovieEntity movie);
}


Related Topics



Leave a reply



Submit