ChatGPT解决这个技术问题 Extra ChatGPT

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

The idea behind JPA not looking for specific fields is that is cost (efficiency wise) the same to bring one column or all columns from one row of the table.
@Desorder -- the cost is not always the same. It's probably not a big deal for simpler, primitive sort of datatypes but the reason I ended up on this page is because I noticed a simple "list documents" query was running slow. That entity has a BLOB column (needs it for file upload/storage) & I suspect it is slow because it is loading the BLOBs into memory even though they're not required for listing the docs.
@jm0 As far as you remember, how many tables had BLOB columns?
@Desorder it was just one table but I was doing a "list" function (multirow -- list all docs created by a given id). The only reason I noticed this issue was because this simple list query was taking several seconds, whereas more complex queries on other tables were happening almost instantly. Once I realized, I knew it would suffer more and more as rows are added because Spring JPA is loading every BLOB into memory even tho they are not used. I found a decent solution for Spring data (posted below) but I think I have an even better one that is pure JPA annotation, I will post tmrw if it works

m
mpr

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();

This is a clean solution. it may have boiler template but the interface can be the inner class of the entity. Making it quite clean.
awesome, just remember not to implement the interface on your Entity or it won't work
This solution doesn't work when extending JpaRepository, any one knows a workaround?
I had no issues getting this to work with JpaRepository, try this method signature List findAllProjectedBy() inside your repository class. I would also suggest keeping your projection interfaces close to your DTOs in terms of packaging. IMO projection interfaces behave like DTOs.
You can not use findAll(); as it will clash with JPARepositorys method. You need to use something like List findAllBy();
i
ifnotak

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.


(and btw I verified, you don't need to provide the fully qualified classname if "Document" is imported -- just had it that way because that's how it was done in the only sample I was able to find)
this should be the accepted answer. It works perfectly and really selects only the necessary fields.
The unnecessary fields are also included, but with the value 'null', would those fields occupy memory?
yes but so minimal that in vast majority of cases it would be really ridiculous to try to engineer around this -- stackoverflow.com/questions/2430655/… you'd have to make specialized lightweight objects without these fields & have them point to same table? which IMO is undesired when using ORMs and leveraging them for their relationships... hyper-optimization is maybe more in the realm of just using some lightweight query DSL and mapping directly to DTOs, & even then i think redundancy is discouraged
jm0 it did not work for me without fully qualified classname, though it was imported. It did compile successfully though.
D
Durandal

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.


no need for native queries. You should avoid using them, for they ruin the advantages of JPQL. see Atals answer.
For me I had to qualify the first attribute (above FIND_PROJECTS) with the value attribute name (hence if this was my code I would have had to write it as @Query(value = FIND_PROJECTS, nativeQuery = true), etc.
b
buræquete

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;
}

you should substitute Object with a custom interface as described by mpr. works flawlessly
S
Sachin Sharma

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.


Hi sachin, I have one doubt if i will create the entity like as you mention above. when JPA will run and it will try to create table with the name of user. which entity will use.
never create a table with JPA, create your tables manually in the db, use JPA to map relational world to object world.
Why can't you make use of inheritance here ?
You'd inherit the column you were trying to exclude. @deadbug
j
jombie

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


No converter found capable of converting from type
E
Evgeni Atanasov

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);
}

Why not return List instead of collection?!
@AbdullahKhan because the result may not always have an order.
It will always be a list, and they'll always be in the order returned by the database. Abdullah is right - it makes sense to return these as List, not as Collection.
k
kszosze

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(){....}
}

Y
Yash

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.


H
Henrik

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();

S
Sneha

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);

h
hahn

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);

f
foxbit

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).


u
ukchaudhary

Using Native Query:

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

Y
Yang Yu
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


T
Tarique Anwer

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
}

S
Saeed Zhiany
{
   "Comments":"Why not using JDBCTemplate",
   "Url":"https://www.baeldung.com/spring-jdbc-jdbctemplate"
}