Using Java Entities (JPA) as Input for Algolia

Hi,

TL;DR; DTO vs full entity

we are exposing our entities with JAX RS and JSON Binding (Java context). Works fine. Now if an entity will be added, updated or deleted, we want to update Algolia.

It works fine so far, but I want to now a good/bad practice adivse.

Example: Entity Food

@NotNull
public String name;

@Embedded
@Valid
public Nutritions nutritions = new Nutritions();

private String description;

@OneToOne
@NotNull
@JsonbTypeAdapter(FoodCategoryAdapter.class)
@JsonbProperty("foodCategoryId")
public FoodCategory foodCategory;

All json annotations are for our own API. and obviously are not working for algolia.

Should I use my complete entity (which is already optimized for our own API) as input for algolia or should I create a DTO? Or should I only use annotations to achieve the correct behaviour?

Hi :wave:

It’s always better to have a separation of concerns between entities and DTOs. It will prevent you from a lots of errors - like exposing a non wanted property.

The Algolia Java API Client (v3) is meant to be used with POJOs and Jackson. You can use Jackson’s annotations to tailor your DTOs.

Hi @antoine.hemery!

Thanks for your answer, really appreciate it.

The problem I see here is: I already have a API with JSON-Annotations (JSON-B) and it is really confusing to have both annotations (one for own api and one for algolia). Dont you agree?

As for now I am creating/mapping a “search entity” without special annotations and so on. It works okay, but a lot of boiler plate cote. I must say, it is a clean and controlled way, but I dont know…

It’s less confusing IMO to have a separation of concerns! Moreover, Algolia requires Jackson’s annotations and not JSON-B one :slight_smile: It’s another good reason to craft DTOs.