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;
}
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
|
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
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
|
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
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
java hibernate jpa spring-hateoas hal
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
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;
|
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)
|
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).
|
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
|
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;
|
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;
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;
answered Nov 25 '18 at 3:33
Jonathan JohxJonathan Johx
1,2361418
1,2361418
|
|
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)
|
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)
|
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)
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)
answered Nov 25 '18 at 4:58
SatyaSatya
113212
113212
|
|
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).
|
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).
|
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).
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).
answered Nov 25 '18 at 14:45
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