简体   繁体   中英

Spring Boot JPA how to select specific column from table with the entity class?

In am using spring boot JPA for my database operations. In my entity class, I mapped every columns with my table. In my table I have many columns, but I need to select some of them in my query result set. I does not need to do select * from table_name which brings the performance issue to my application.

My entity class:

@Entity
@Table(name = "user_table")
public class UserInformation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int userId;

    @Column(name = "user_name")
    private String userName;

    @Column(name = "user_type")
    private String userType;

    @Column(name = "user_age")
    private int userAge;

    @Column(name = "is_male")
    private boolean isMale;
}

After this I generated all getters and setters for above class.

My userDAO class

public interface UserInformationRepo extends 
         JpaRepository<UserInformation, String>{

    @Query(value="select user_name from user_table where user_age > 
               :userAge", nativeQuery=true)
    public UserInformation getInfo(int userAge);\

    @Query(value="select user_name,userType, user_id  from user_table where 
          user_age > :userAge", nativeQuery=true)
    public UserInformation getInfo(int userAge);
}

When I run this spring boot app with my above class it shows error as

user_id was not included in the ResultSet

This error because I did't select the user_id from my query.

In my userDAO class I have a multiple functions, each functions need different set of columns, but the return type of all methods should be UserInformation class.

My excepted result is, the columns which I does not select from query should have null value in my result UserInformation object.

I also searched many resources for this, but I cannot find any relevant answer.

Any Help. Thanks in advance

One way could be create a constructor of the UserInformation with userId and userName .

package com.demo.model;

public UserInformation( String userName, String userType, String userId){
    this.userName = userName;
    this.userType = userType;
    this.userId = userId;
}

then

 @Query(value="select new com.demo.model.UserInformation
               (user_name,userType, user_id)  
                from user_table where 
                user_age > :userAge", nativeQuery=true)

The user_id is the primary key in your UserInformation object. It would be best to include all columns in the UserInformation object in the Query:

public interface UserInformationRepo extends 
         JpaRepository<UserInformation, String>{

    @Query(value="select * from user_table where user_age > 
               :userAge", nativeQuery=true)
    public UserInformation getInfo(int userAge);
    @Query(value="select *  from user_table where 
          user_age > :userAge", nativeQuery=true)
    public UserInformation getInfo(int userAge);

        }

If your user_table has a great deal of information, you can list only the columns you care about, but be sure to include the @Id (primary key).

The easiest solution if you create a projection for your entity (containing only the necessary properties):

public interface UserInformationNameProjection {
  String getUserName();
}

Then you can define the repository method this way (You don't even need the id!):

@Query(value="select user_name from user_table where user_age > 
           :userAge", nativeQuery=true)
public UserInformationNameProjection getInfo(int userAge);

Let Spring Data JPA do the rest of the job. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM