user1817436
user1817436

Reputation: 2855

Spring JPA selecting specific columns

I am using Spring JPA to perform all database operations. However I don't know how to select specific columns from a table in Spring JPA?

For example:
SELECT projectId, projectName FROM projects

Upvotes: 280

Views: 642679

Answers (22)

Cause Chung
Cause Chung

Reputation: 1485

Now, a record based projection:

record NamesOnly(String firstname, String lastname) {
}
List<NamesOnly> findAll();

https://docs.spring.io/spring-data/jpa/reference/repositories/projections.html

Upvotes: 2

Suranjan Kumar
Suranjan Kumar

Reputation: 1

If you use projections from Spring Data JPA And Create an Interface to get specific Columns, then it will cause performance issues. Because it executes select all Query. And you will find this query in console.

Example: - select * from table; Better solution to use Criteria Builder to get specific columns. Example: -

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();
Root<EntityName> entityRoot = criteriaQuery.from(EntityName.class);
criteriaQuery.multiselect(entityRoot.get("columnName"), entityRoot.get("columnName")).where(criteriaBuilder.equal(entityRoot.get("columnName"), entityRoot.get("columnName"));

Without distinct and group by: -

criteriaQuery.multiselect(entityRoot.get("columnName"), entityRoot.get("columnName")).where(criteriaBuilder.equal(entityRoot.get("columnName"), entityRoot.get("columnName"));
return entityManager.createQuery(criteriaQuery).getResultList();

With distinct: -

criteriaQuery.multiselect(entityRoot.get("columnName"), entityRoot.get("columnName")).where(criteriaBuilder.equal(entityRoot.get("columnName"), entityRoot.get("columnName")).distinct(true);
return entityManager.createQuery(criteriaQuery).getResultList();

With group by: -

criteriaQuery.multiselect(entityRoot.get("columnName"), entityRoot.get("columnName")).where(criteriaBuilder.equal(entityRoot.get("columnName"), entityRoot.get("columnName")).groupBy(entityRoot.get("columnName"));
return entityManager.createQuery(criteriaQuery).getResultList();

Upvotes: 0

fforfabio
fforfabio

Reputation: 181

You can perform it in a lot of ways.

  • By using nativeQuery, so by selecting directly from your database table (it must be defined inside your application.properties/yml).
    So simply declare your repository by extending JpaRepository:

      @Repository
      public interface ProjectRepository extends JpaRepository<Project, Long>{
      }
    

    Then, you should define the query (something like this) inside the Repository:

    @Repository
    public interface ProjectRepository extends JpaRepository<Project, Long>{
    
        @Query(value = "SELECT projectId, projectName FROM projects", nativeQuery = true)
        public List<Project> getProjectColumns();
    
    }
    

    Finally, you can call this method from your controller like that:

    @CrossOrigin(origins = "http://localhost:8080")
    @RestController
    @RequestMapping("/project")
    public class Controller {
        private ProjectRepository projectRepo;
    
        public Controller(ProjectRepository projectRepo) {
            this.projectRepo = projectRepo;
        }
    
        @GetMapping("/columns")
        public ResponseEntity<List<Project>> getProjectColumns(){
            List<Project> projects = new ArrayList<Project>();
            projects = projectRepo.getProjectColumns();
            return projects;   
        }
    }
    
  • By using JPQL.
    The only thing that must be change from the nativeQuery example is the query itself. In fact with JPQL you are going to perform the query on the entity itself.
    So:

    @Repository
    public interface ProjectRepository extends JpaRepository<Project, Long>{
    
        @Query("SELECT p.projectId, p.projectName FROM Project p")
        public List<Project> getProjectColumns();
    
    }
    

    Note that Project is the entity and not the database table (in the previous example is projects).
    I left here a useful link I found on the Internet.

  • Finally, you can use this other technique. I'll advise you that it is a little bit more complicated, but the result is the same.
    First thing is to define only the method signature in the repository:

    @Repository
    public interface ProjectRepository extends JpaRepository<Project, Long>{
    
        public List<Project> getProjectColumns();
    
    }
    

    Second thing is to modify your entity Project by writing the query inside the @NamedNativeQueries annotation, and then by writing the result mapping inside the @SqlResultSetMappings annotation. In this annotation you are going to define how the object that you are going to return will be, so you may also define a new constructor inside the Project entity.
    This is how it should look:

    @NamedNativeQueries(value = {
        @NamedNativeQuery(name = "Project.getProjectColumns", // The name of the repository method
            query = "SELECT p.projectId, p.projectName FROM projects p",
            resultSetMapping = "projectColumns")})
    @SqlResultSetMappings(value = {
        @SqlResultSetMapping(name = "projectColumns", // It must match with the resultSetMapping defined inside @NamedNativeQuery
           classes = @ConstructorResult(targetClass = Project.class,
                                        columns = {@ColumnResult(name = "projectId"),
                                                   @ColumnResult(name = "projectName")}))})
    @Entity
    @Table(name = "projects")
    public class Project {
    
        // Columns of the entity Project
        @Id
        private long projectId;
    
        private String projectName;
    
    
        // Some other attribute can be defined here
    
    
        // Default constructor
        public Project() {
        }
    
    
        // Constructor for getProjectColumns query. Note that the number of the
        // parameters and their type must be the same of the one returned by the query.
        // All the other attributes present in this entity will be set to their default value.
        public Project(long projectId, String projectName) {
            this.projectId = projectId;
            this.projectName = projectName;
        }
    
        // Other constructor can be available
    
        // Getter and setter here
    }  
    

    Finally your controller can be the same as the previous one. The only important thing is that all the names that are present (query, method, ...) must be the same or the query will note be performed.

I'll hope that this is clear and will help.

Upvotes: 0

Gil
Gil

Reputation: 69

Use:

@Query("SELECT e FROM #{#entityName} e where e.userId=:uid")
List<ClienteEnderecoEntity> findInfoByUid(@Param("uid") UUID uid);

Upvotes: -4

Saurabhcdt
Saurabhcdt

Reputation: 1158

With current version I'm using (JDK 11, Spring Boot 3) It's very simple:

  1. You need to add a constructor in your entity class for limited fields which you want to select:
    class Projects {
        private String projectId;
        private String projectName;
        // other fields
    
        // No argument / all arguments  constructor as needed
    
        public Projects(String projectId, String projectName) {
            this.projectId = projectId;
            this.projectName = projectName;
        }
    }
  1. In your Repository, you can add Query & method as below (add WHERE condition as needed):
    @Query("SELECT new Projects (p.projectId, p.projectName) FROM Projects p")
    List<Projects > findAllProjects();

For me, this works & I get list of all objects with only 2 fields specified.

Upvotes: 1

Yehouda
Yehouda

Reputation: 122

You can use a DTO like that

 @Data
    public class UserDtoLight implements Serializable {
       private final Long id;
       private final String name;
    }

and in your repository

 List<UserDtoLight> findAll();

Upvotes: 1

Tarique Anwer
Tarique Anwer

Reputation: 21

You can update your JPARepository as below.

@Query("select u.status from UserLogin u where u.userId = ?1 or u.email = ?1 or u.mobile = ?1")
public UserStatus findByUserIdOrEmailOrMobile(String loginId);

Where UserStatus is a Enum

public enum UserStatus
{
    New,
    Active,
    Deactived,
    Suspended,
    Locked
}

Upvotes: 1

vijay
vijay

Reputation: 41

public static final String FIND_PROJECTS = "select ac_year_id,ac_year from tbl_au_academic_year where ac_year_id=?1";

    @Query(value = FIND_PROJECTS, nativeQuery = true)
    public  List<Object[]> findByAcYearId(Integer ac_year_id);

this works for me

Upvotes: 2

SR Ranjan
SR Ranjan

Reputation: 123

Using Spring Data JPA there is a provision to select specific columns from database

---- In DAOImpl ----

@Override
    @Transactional
    public List<Employee> getAllEmployee() throws Exception {
    LOGGER.info("Inside getAllEmployee");
    List<Employee> empList = empRepo.getNameAndCityOnly();
    return empList;
    }

---- In Repo ----

public interface EmployeeRepository extends CrudRepository<Employee,Integer> {
    @Query("select e.name, e.city from Employee e" )
    List<Employee> getNameAndCityOnly();
}

It worked 100% in my case. Thanks.

Upvotes: 12

jm0
jm0

Reputation: 3444

I don't like the syntax particularly (it looks a little bit hacky...) but this is the most elegant solution I was able to find (it uses a custom JPQL query in the JPA repository class):

@Query("select new com.foo.bar.entity.Document(d.docId, d.filename) from Document d where d.filterCol = ?1")
List<Document> findDocumentsForListing(String filterValue);

Then of course, you just have to provide a constructor for Document that accepts docId & filename as constructor args.

Upvotes: 213

foxbit
foxbit

Reputation: 341

You can use the answer suggested by @jombie, and:

  • place the interface in a separate file, outside the entity class;
  • use native query or not (the choice depended on your needs);
  • don't override findAll() method for this purpose but use name of your choice;
  • remember to return a List parametrized with your new interface (e.g. List<SmallProject>).

Upvotes: 1

Evgeni Atanasov
Evgeni Atanasov

Reputation: 520

In my opinion this is great solution:

interface PersonRepository extends Repository<Person, UUID> {

    <T> Collection<T> findByLastname(String lastname, Class<T> type);
}

and using it like so

void someMethod(PersonRepository people) {

  Collection<Person> aggregates =
    people.findByLastname("Matthews", Person.class);

  Collection<NamesOnly> aggregates =
    people.findByLastname("Matthews", NamesOnly.class);
}

Upvotes: 12

jombie
jombie

Reputation: 271

With the newer Spring versions One can do as follows:

If not using native query this can done as below:

public interface ProjectMini {
    String getProjectId();
    String getProjectName();
}

public interface ProjectRepository extends JpaRepository<Project, String> { 
    @Query("SELECT p FROM Project p")
    List<ProjectMini> findAllProjectsMini();
}

Using native query the same can be done as below:

public interface ProjectRepository extends JpaRepository<Project, String> { 
    @Query(value = "SELECT projectId, projectName FROM project", nativeQuery = true)
    List<ProjectMini> findAllProjectsMini();
}

For detail check the docs

Upvotes: 22

ukchaudhary
ukchaudhary

Reputation: 387

Using Native Query:

Query query = entityManager.createNativeQuery("SELECT projectId, projectName FROM projects");
List result = query.getResultList();

Upvotes: 0

Atal
Atal

Reputation: 351

In my situation, I only need the json result, and this works for me:

public interface SchoolRepository extends JpaRepository<School,Integer> {
    @Query("select s.id, s.name from School s")
    List<Object> getSchoolIdAndName();
}

in Controller:

@Autowired
private SchoolRepository schoolRepository;

@ResponseBody
@RequestMapping("getschoolidandname.do")
public List<Object> getSchool() {
    List<Object> schools = schoolRepository.getSchoolIdAndName();
    return schools;
}

Upvotes: 35

ajaz
ajaz

Reputation: 39

You can apply the below code in your repository interface class.

entityname means your database table name like projects. And List means Project is Entity class in your Projects.

@Query(value="select p from #{#entityName} p where p.id=:projectId and p.projectName=:projectName")

List<Project> findAll(@Param("projectId") int projectId, @Param("projectName") String projectName);

Upvotes: 3

hahn
hahn

Reputation: 3658

It is possible to specify null as field value in native sql.

@Query(value = "select p.id, p.uid, p.title, null as documentation, p.ptype " +
            " from projects p " +
            "where p.uid = (:uid)" +
            "  and p.ptype = 'P'", nativeQuery = true)
Project findInfoByUid(@Param("uid") String uid);

Upvotes: 2

Durandal
Durandal

Reputation: 5663

You can set nativeQuery = true in the @Query annotation from a Repository class like this:

public static final String FIND_PROJECTS = "SELECT projectId, projectName FROM projects";

@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<Object[]> findProjects();

Note that you will have to do the mapping yourself though. It's probably easier to just use the regular mapped lookup like this unless you really only need those two values:

public List<Project> findAll()

It's probably worth looking at the Spring data docs as well.

Upvotes: 106

Sachin Sharma
Sachin Sharma

Reputation: 1496

In my case i created a separate entity class without the fields that are not required (only with the fields that are required).

Map the entity to the same table. Now when all the columns are required i use the old entity, when only some columns are required, i use the lite entity.

e.g.

@Entity
@Table(name = "user")
Class User{
         @Column(name = "id", unique=true, nullable=false)
         int id;
         @Column(name = "name", nullable=false)
         String name;
         @Column(name = "address", nullable=false)
         Address address;
}

You can create something like :

@Entity
@Table(name = "user")
Class UserLite{
         @Column(name = "id", unique=true, nullable=false)
         int id;
         @Column(name = "name", nullable=false)
         String name;
}

This works when you know the columns to fetch (and this is not going to change).

won't work if you need to dynamically decide the columns.

Upvotes: 16

mpr
mpr

Reputation: 3906

You can use projections from Spring Data JPA (doc). In your case, create interface:

interface ProjectIdAndName{
    String getId();
    String getName();
}

and add following method to your repository

List<ProjectIdAndName> findAll();

Upvotes: 328

kszosze
kszosze

Reputation: 341

I guess the easy way may be is using QueryDSL, that comes with the Spring-Data.

Using to your question the answer can be

JPAQuery query = new JPAQuery(entityManager);
List<Tuple> result = query.from(projects).list(project.projectId, project.projectName);
for (Tuple row : result) {
 System.out.println("project ID " + row.get(project.projectId));
 System.out.println("project Name " + row.get(project.projectName)); 
}}

The entity manager can be Autowired and you always will work with object and clases without use *QL language.

As you can see in the link the last choice seems, almost for me, more elegant, that is, using DTO for store the result. Apply to your example that will be:

JPAQuery query = new JPAQuery(entityManager);
QProject project = QProject.project;
List<ProjectDTO> dtos = query.from(project).list(new QProjectDTO(project.projectId, project.projectName));

Defining ProjectDTO as:

class ProjectDTO {

 private long id;
 private String name;
 @QueryProjection
 public ProjectDTO(long projectId, String projectName){
   this.id = projectId;
   this.name = projectName;
 }
 public String getProjectId(){ ... }
 public String getProjectName(){....}
}

Upvotes: 10

Henrik
Henrik

Reputation: 1807

You can use JPQL:

TypedQuery <Object[]> query = em.createQuery(
  "SELECT p.projectId, p.projectName FROM projects AS p", Object[].class);

List<Object[]> results = query.getResultList();

or you can use native sql query.

Query query = em.createNativeQuery("sql statement");
List<Object[]> results = query.getResultList();

Upvotes: 4

Related Questions