【发布时间】:2020-01-14 13:37:19
【问题描述】:
我正在使用 Spring Boot JPA 进行数据库操作。在我的实体类中,我用我的表映射了每一列。在我的表中,我有很多列,但我需要在查询结果集中选择其中一些。我不需要这样做select * from table_name,这会给我的应用程序带来性能问题。
我的实体类:
@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;
}
在此之后,我为上述类生成了所有 getter 和 setter。
我的 userDAO 类
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);
}
当我使用上面的类运行这个 Spring Boot 应用程序时,它显示错误为
user_id 未包含在结果集中
这个错误是因为我没有从我的查询中选择 user_id。
在我的 userDAO 类中我有多个函数,每个函数需要不同的列集,但所有方法的返回类型应该是 UserInformation 类。
我的例外结果是,我没有从查询中选择的列在我的结果 UserInformation 对象中应该有空值。
我也为此搜索了很多资源,但我找不到任何相关的答案。
任何帮助。提前致谢
【问题讨论】:
-
如何获取 3 列并期望创建整个对象?
-
@Andronicus 是的,但这些值可能为空。
-
粘贴完整的日志以更好地了解确切的错误。
标签: java spring spring-boot jpa spring-data-jpa