Spring Data JPARepository: How to conditionally fetch children entites

前端 未结 5 1379
余生分开走
余生分开走 2020-12-08 14:28

How can one configure their JPA Entities to not fetch related entities unless a certain execution parameter is provided.

According to Spring\'s documentation, 4.3.9

5条回答
  •  执笔经年
    2020-12-08 14:57

    If you are trying to send the resultset of your entities to a client, I recommend you use data transfer objects(DTO) instead of the entities. You can directly create a DTO within the HQL/JPQL. For example

    "select new com.test.MyTableDto(my.id, my.name) from MyTable my"
    

    and if you want to pass the child

    "select new com.test.MyTableDto(my.id, my.name, my.child) from MyTable my"
    

    That way you have a full control of what is being created and passed to client.

提交回复
热议问题