java - JPA Hibernate: How to limit fetch depth for a self referencing entity -


i write example reduce complexity:


human.java

@entity @table(name = "human") @dynamicinsert(value = true) @dynamicupdate(value = true) public class human {      @id     @generatedvalue(strategy = generationtype.auto)     private long id;      @column(nullable = false, name = "name")     private string name;      @manytoone(cascade=cascadetype.all, fetch=fetchtype.lazy)     @joincolumn(name = "parent_id")     private human parent;      @onetomany(fetch=fetchtype.lazy, mappedby ="parent")     private collection<human> children;      // getters , setters ... } 

humandao.java

@repository public interface humandao  extends crudrepository<human, long>{     @query(value =              "select * " +             "from humans " +              "where parent_id = ?1",             nativequery = true)     public iterable<human> getchildren(long parentid);  } 

testclass.java

public class testclass {      @autowired     private humandao dao;      public void test(long parentid) {         iterable<human> children = dao.getchildren(parentid);         system.out.println(children);     } } 

application.properties:

spring.datasource.url=jdbc:mysql://localhost/db spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.driver spring.jpa.properties.max_fetch_depth=1 

database:

id | name | parent 1  | mike | null 2  | john | 1 3  | bill | 2 4  | carl | 3 

problem: when getchildren mike, expect see john there, references carl included.

how can limit depth of self referencing entity doesn't go deeper specified number limit.

i constructed entity following this example (step 3)

update: found similar problem, didn't solve either.