HQL joined query to eager fetch a large number of relationships

前端 未结 1 743
清歌不尽
清歌不尽 2020-12-02 01:32

My project has recently discovered that Hibernate can take multiple levels of relationship and eager fetch them in a single join HQL to produce the filled object we need. W

相关标签:
1条回答
  • 2020-12-02 02:16

    Our team uses the special strategy to work with associations. Collections are lazy, single relations are lazy too, except references with simply structure (for an example a countries reference). And we use fluent-hibernate to load what we need in a concrete situation. It is simply because of fluent-hibernate supports nested projections. You can refer this unit test to see how complex object net can be partially loaded. A code snippet from the unit test

     List<Root> roots = H.<Root> request(Root.class).proj(Root.ROOT_NAME)
                .innerJoin("stationarFrom.stationar", "stationar")
                .proj("stationar.name", "stationarFrom.stationar.name")
                .eq(Root.ROOT_NAME, rootName).transform(Root.class).list();
    

    See also

    How to transform a flat result set using Hibernate

    0 讨论(0)
提交回复
热议问题