How to have hibernate not query children?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm implementing HATEOAS in my spring boot java application, and one of the things to do in HATEOAS is to not return the children objects in the json response. Instead, you have links that can fetch the children of that object.



An example is a company can have many employees. If you do a GET call for a specific company, the usual response would be a JSON object of company that contains a list of employees. I want the JSON to not have that employees collection. In theory, the only query hibernate should have to run is a select statement on the company table.



Any help is much appreciated.



Thanks in advance









share























  • Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

    – chrylis
    Nov 25 '18 at 3:58


















0















I'm implementing HATEOAS in my spring boot java application, and one of the things to do in HATEOAS is to not return the children objects in the json response. Instead, you have links that can fetch the children of that object.



An example is a company can have many employees. If you do a GET call for a specific company, the usual response would be a JSON object of company that contains a list of employees. I want the JSON to not have that employees collection. In theory, the only query hibernate should have to run is a select statement on the company table.



Any help is much appreciated.



Thanks in advance









share























  • Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

    – chrylis
    Nov 25 '18 at 3:58














0












0








0








I'm implementing HATEOAS in my spring boot java application, and one of the things to do in HATEOAS is to not return the children objects in the json response. Instead, you have links that can fetch the children of that object.



An example is a company can have many employees. If you do a GET call for a specific company, the usual response would be a JSON object of company that contains a list of employees. I want the JSON to not have that employees collection. In theory, the only query hibernate should have to run is a select statement on the company table.



Any help is much appreciated.



Thanks in advance









share














I'm implementing HATEOAS in my spring boot java application, and one of the things to do in HATEOAS is to not return the children objects in the json response. Instead, you have links that can fetch the children of that object.



An example is a company can have many employees. If you do a GET call for a specific company, the usual response would be a JSON object of company that contains a list of employees. I want the JSON to not have that employees collection. In theory, the only query hibernate should have to run is a select statement on the company table.



Any help is much appreciated.



Thanks in advance







java hibernate jpa spring-hateoas hal





share












share










share



share










asked Nov 25 '18 at 3:23









ToastToast

3515




3515













  • Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

    – chrylis
    Nov 25 '18 at 3:58



















  • Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

    – chrylis
    Nov 25 '18 at 3:58

















Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

– chrylis
Nov 25 '18 at 3:58





Unless you're using something like Spring Data REST, you'll usually need to write DTOs that translate JPA relationships into collections of links.

– chrylis
Nov 25 '18 at 3:58












3 Answers
3






active

oldest

votes


















0














Can use fetch Lazy in order to not show childs, here is an example:



@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
private List<Team> teams;




share































    0














    View the code for type association you are using,



    for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched.
    similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy.
    Depending on the type of association you are using there will be always some default strategy but you can change this strategy



    If you don't want this behavior then you need to change
    @ManyToOne(fetch=FetchType.LAZY)





    share































      0














      Realized Jackson was the one making the extra call when it was serializing the objects before sending it back.
      Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).





      share































        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Can use fetch Lazy in order to not show childs, here is an example:



        @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
        private List<Team> teams;




        share




























          0














          Can use fetch Lazy in order to not show childs, here is an example:



          @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
          private List<Team> teams;




          share


























            0












            0








            0







            Can use fetch Lazy in order to not show childs, here is an example:



            @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
            private List<Team> teams;




            share













            Can use fetch Lazy in order to not show childs, here is an example:



            @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
            private List<Team> teams;





            share











            share


            share










            answered Nov 25 '18 at 3:33









            Jonathan JohxJonathan Johx

            1,2361418




            1,2361418

























                0














                View the code for type association you are using,



                for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched.
                similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy.
                Depending on the type of association you are using there will be always some default strategy but you can change this strategy



                If you don't want this behavior then you need to change
                @ManyToOne(fetch=FetchType.LAZY)





                share




























                  0














                  View the code for type association you are using,



                  for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched.
                  similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy.
                  Depending on the type of association you are using there will be always some default strategy but you can change this strategy



                  If you don't want this behavior then you need to change
                  @ManyToOne(fetch=FetchType.LAZY)





                  share


























                    0












                    0








                    0







                    View the code for type association you are using,



                    for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched.
                    similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy.
                    Depending on the type of association you are using there will be always some default strategy but you can change this strategy



                    If you don't want this behavior then you need to change
                    @ManyToOne(fetch=FetchType.LAZY)





                    share













                    View the code for type association you are using,



                    for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched.
                    similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy.
                    Depending on the type of association you are using there will be always some default strategy but you can change this strategy



                    If you don't want this behavior then you need to change
                    @ManyToOne(fetch=FetchType.LAZY)






                    share











                    share


                    share










                    answered Nov 25 '18 at 4:58









                    SatyaSatya

                    113212




                    113212























                        0














                        Realized Jackson was the one making the extra call when it was serializing the objects before sending it back.
                        Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).





                        share




























                          0














                          Realized Jackson was the one making the extra call when it was serializing the objects before sending it back.
                          Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).





                          share


























                            0












                            0








                            0







                            Realized Jackson was the one making the extra call when it was serializing the objects before sending it back.
                            Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).





                            share













                            Realized Jackson was the one making the extra call when it was serializing the objects before sending it back.
                            Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).






                            share











                            share


                            share










                            answered Nov 25 '18 at 14:45









                            ToastToast

                            3515




                            3515















                                這個網誌中的熱門文章

                                Tangent Lines Diagram Along Smooth Curve

                                Yusuf al-Mu'taman ibn Hud

                                Zucchini