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