Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotations to support using JPA Entities for JSON serialization #358

Open
derekm opened this issue Aug 20, 2024 · 3 comments
Open

Annotations to support using JPA Entities for JSON serialization #358

derekm opened this issue Aug 20, 2024 · 3 comments

Comments

@derekm
Copy link

derekm commented Aug 20, 2024

Jackson has added @JsonManagedReference, @JsonBackReference, & @JsonIdentityInfo to support putting JPA Entities directly to the wire.

Similar annotations in JSON-B would be useful for getting rid of entity-object-to-value-object and value-object-to-entity-object transformers as an unnecessary layer of code.

What I think I'd like is something akin to @JsonbSerializeAsSingleField("person.personId"), as in:

@ManyToOne
@JoinColumn(name = "person_id")
@JsonbSerializeAsSingleField("person.personId")
public Person person;

But I suspect what I want is slightly more involved than this, and might require JSON deserialization to know more about JPA entities and entity manager, since IDs should become managed references on deserialization.

@rmannibucau
Copy link

@derekm why not using an adapter or (de)serializer? works smoothly and avoids to do this kind of API which will quickly require @JsonbSerializeAsMultipleFields({"id", "date"}) for example, and later another model cause one field needs a specific serialization and ultimately requires deduplication only to use jsonpointers?

@derekm
Copy link
Author

derekm commented Aug 20, 2024

@rmannibucau -- Even if JPA Entities could produce the same structure as their respective value-objects that transform join-reference columns back into object IDs, then I think maybe you can't avoid defining and distributing your value-objects anyway as client interfaces, since you don't really want the fat Person field on the client objects (unless we get a marriage between JPA and JAX-RS for graphql-like object-graph retrieval via MicroProfile Rest Client-like web clients).

@rmannibucau
Copy link

@derekm if ambiguous I agree, you often (90+%?) cut the relationship at their id + common fields, my point was just that this kind of API becomes quickly greedy whereas it is already doable to solve that issue with at least 2 (3) annotations, agree they require code but it is likely not that bad IMHO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants