'To get list of records with @NamedNativequery for one to many association
I am new to hibernate NamedNativequeries and would like to know how can we form a JSON from list of records
Currently my query returns:
[
{
"Id": "1",
"Name" : "someName",
"subName": one,
"SubCategory": null
},
{
"Id": "1",
"Name" : "someName",
"subName": two,
"SubCategory": null
}
]
Desired output
[
{
"Id": "1",
"Name": "someName",
"subList": [
{
"subName": "one",
"SubCategory": ""
},
{
"subName": "two",
"SubCategory": ""
}
]
}
]
Basically, I need to know the mapping to be done at the entity class level.
My class structure looks like,
@NamedNativeQuery{<my query to fetch id , name , subName , subCategory>}
@sqlResultSetMapping <@fieldResult and @ Column setting >
class Test
{
@column
private String id;
@column
private String name ;
private List<Sub> sub;
//getter setter..
}
//new entity
class Sub {
subName
subCategory
}
update -
So I got the format i was looking for using @oneToMany(cascade = cascadeType.ALL) private List<Sub> sub; in class test and @ManytoOne private Test test; in class sub
with @joinCol as a primary key.
but now the issue is it gives duplicate values from the second entity it should be one and two both coming inside JSON array -
[
{
"Id": "1",
"Name": "someName",
"subList": [
{
"subName": "one",
"SubCategory": ""
},
{
"subName": "one",
"SubCategory": ""
}
]
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
