In typical situation with embedded resources:
@Entity @Data
class Item {
id, name
...
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
}
@Entity @Data
class Status {
id, name
...
@JsonIgnore //break infinite reference loop during serialization
@OneToMany(mappedBy="status")
private List<Item> items;
}
Instead of having links to Status id's in Item JSON, I want to INCLUDE Status object in Item JSON
{
"itemName": "abc",
... ,
"status": {
"statusName":"ACTIVE",
...
}
"_links": {
...
}
}
I managed embedding doing any of the following:
- Marking Item class status property as @RestResource(exported=false)
@Entity @Data
class Item {
...
@RestResource(exported=false) // <-- HERE
@ManyToOne
@JoinColumn(name="status", referencedColumnName="ID")
private Status status;
- Marking Status repo interface @RepositoryRestResource(..., exported=false)
@RepositoryRestResource(collectionResourceRel="statuses", path="status", exported=false)
public interface StatusRepository extends JpaRepository<Status, String>
- Deleteting repository for Status entity
// DELETED
@RepositoryRestResource
public interface StatusRepository extends JpaRepository<Status, String>{}
QUESTION:
Any of that embeds Status into Item JSON like I want, but I do not have an access to Status Repository anymore to get a Status object by it's ID or do any CRUD on it.
How to embed status in parent Item JSON and still CRUD status via url?
I implemented it as follows:
Repositories for both Entities exist so it is possible to do CRUD on Status entity as well
For future generations I'll leave it here.
For convenience:
1) Exposed ids for entities in JSON
So getting list of items looks like: