0

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" } } 
2
  • @OrderBy("position asc") ? Commented Sep 23, 2015 at 8:41
  • No luck. The problem is Spring Data Rest serialize into subcollections indexed by type. Maybe subcollections are ordered but it is not the wanted behaviour . Commented Sep 23, 2015 at 11:09

1 Answer 1

1

So i found the answer.

Replacing the ViewItems collection mapping type of the View entity from List to Map and adding a read only type field on the ViewItem entity does the trick:

@Entity @Table(name="views") public class View { @OneToMany @JoinColumn(name="view_id") @MapKey("position") private Map<Integer,ViewItem> viewItems = new HashMap<Integer,ViewItem>(); ... } @Entity @table(name="view_items") @DiscriminatorColumn(name="type") public abstract class ViewItem { @Column(insertable = false, updatable = false) public String type; ... } 

EDIT

Months after, and thanks to @aux (see here) here is the solution. A RelProvider class must be implemented, as explained here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.