I use Spring Boot v1.2.5 and have the following single table inheritance entities:
@Entity
@table(name="view_items")
@DiscriminatorColumn(name="type")
public abstract class ViewItem {
private int position;
}
@Entity
@DiscriminatorValue(value="table")
public class Table extends ViewItem {}
@Entity
@DiscriminatorValue(value="chart")
public class Chart extends ViewItem {}
public interface ViewItemRepository
extends JpaRepository<ViewItem, Integer>{}
And the following association:
@Entity
@Table(name="views")
public class View {
@OneToMany
@JoinColumn(name="view_id")
@OrderBy("position")
private List<ViewItem> viewItems = new ArrayList<ViewItem>();
...
}
Calling GET /views/{id}/viewItems
return the collection ordered by subclasse type (some parts have been removed for readability) :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 23 Sep 2015 07:41:13 GMT
{
"_embedded" : {
"tables" : [ {
"position": 4,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}
} ],
"charts" : [ {
"position": 2,
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
}
} ]
}
Is there a simple way to change the JSON serialisation having view_items
sorted by position like this ? :
{
"_embedded" : {
"2": {
"type": "chart",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/2"
}
},
"4": {
type: "table",
...
"_links" : {
"self" : {
"href" : "http://localhost:8080/viewItems/3"
}
}