今天用SpringDataJPA查询数据,往redis中存储时遇到这个问题,
在网页上一搜千篇一律的解决办法都是在实体类上面加上注解
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
尝试了一下,没能解决,最后发现不是加不加注解的原因,因为SpringDataJPA的getOne()
方法查询出来的是一个动态代理对象,
而我需要的是一个实体对象,所有自己在dao层写了一个查询方法,查询出对应的学生实体信息:
public interface StudentDao extends JpaRepository<Student,Integer>, JpaSpecificationExecutor<Student> {
/**
* 根据学生Id查询出对应学生实体信息
* @param id
* @return
*/
@Query(value = "select * from Student where id = ?1",nativeQuery = true)
Student findBystuId(Integer id);
}