Spring-hateoas: Remove Identifiable

Created on 28 Feb 2019  Â·  6Comments  Â·  Source: spring-projects/spring-hateoas

Identifiable really doesn't naturally fit into the API anymore as its move to Optional has made obvious that the optionality really gets in the way of link creation etc. Also, with the rename of ResourceSupport to RepresentationModel, the idea of a model being identifiable feels rather weird as well.

Most helpful comment

Removing this has caused me a fair bit of pain. Other than this GitHub issue it isn't documented (that I could find) and isn't mentioned at all in the migration guide. The removal of the slash(Identifiable identifiable) method from LinkBuilder caused my code to fall back to the slash(Object object) method which then produced bad links without throwing any errors. Luckily my tests saved the day.
Might be worth adding it to the migration guide to save others the trouble of having to figure this out for themselves.

All 6 comments

Oh, the usage of Identifiables in code paths currently supporting it seems to be broken anyway as they now handle an Optional instead of an object which is unlikely to create the expected results. 😬

The only place Identifiable possibly made sense was mapping to a primary key in a database for the domain object. And that's a weak reason for an entire interface.

@odrotbohm @gregturn Just wondering I am trying to implement Json Api using the new custom types. However Json Api requires identifiable and resource name. Is there another way to get id and resource name from RepresentationModel or EntityModel

{
  "type": "ResourceType",
  "id": "ResourceId",
  "attributes": {
    .....
  },
  "links": {
    "self": "href"
  }
}
public class EntityModel<T> extends RepresentationModel<EntityModel<T>> {

    private final T content;

What is the equivalent of Identifiable?

You'd manually unfold the entity into it's type and whatever constitutes its. EntityLinks now exposes a ….forType(Function<T, ?>) that effectively takes an extractor function and returns a TypedEntityLinks that will automatically apply the given extractor if you hand instances of a certain type:

TypedEntityLinks<Order> links = entityLinks.forType(Order::getId);
links.linkToItemResource(order) // <- will call getId() on the order to extract the identifier

For more details, please consult the reference documentation.

Removing this has caused me a fair bit of pain. Other than this GitHub issue it isn't documented (that I could find) and isn't mentioned at all in the migration guide. The removal of the slash(Identifiable identifiable) method from LinkBuilder caused my code to fall back to the slash(Object object) method which then produced bad links without throwing any errors. Luckily my tests saved the day.
Might be worth adding it to the migration guide to save others the trouble of having to figure this out for themselves.

Was this page helpful?
0 / 5 - 0 ratings