A
A
Alexey Yukhnovets2018-07-20 22:52:32
Java
Alexey Yukhnovets, 2018-07-20 22:52:32

Spring Data Rest. How can I force the categoryId for a OneToMany relationship to be output when a GET_ONE request is made?

Kotlin code:

@Entity data class Page(
        val name: String, 
        @OneToMany val category: Category, 
        @Id @GeneratedValue val id: Int = -1
)
@Entity data class Category(
        val name: String, 
        @Id @GeneratedValue val id: Int = -1
)
@RepositoryRestResource(collectionResourceRel = "pages", path="pages")
interface PageRepository : PagingAndSortingRepository<Page, Int>

When requested, "GET /pages/1" returns json with three fields: name, id(exposeIdsFor is used) and _links. In the database, "ddl-auto=create" uses the category_id field for this relationship. How to display this field next to name and id when requesting? Something like "exposeIdsFor" only for links, and even better a calculated field, as in projection via @Value
Tried to use projection, but as I understand it only works for those requests where _embedded is returned (i.e. not with GET_ONE) . And anyway, it turns out to be a crutch, writing an interface with duplication of all fields from Entity for the sake of adding one field somehow does not sound very attractive, but if this is the only option, then why not. Maybe there is a way to extend projection to GET_ONE and other requests without _embedded?
@Value("#{target.category?.id}")
fun getCategoryId(): Int

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question