Store a JPA object with a given field object identifier instead of the object itself

The problem is as follows:

We have an entity:

@Entity public class Feedback { @Id @GeneratedValue(generator="token") private String id; @ManyToOne private Product product; private String message; // other fields } 

And we have a server endpoint that receives feedback from clients. Feedback received in multipart / form format, with fields:

  ProductId - product identifier Message - feedback message Some other fields 

To set Feedback.product, we need to load the Product object from JPA - this can take a lot of time and creates unnecessary queries.

Is it possible to save the object, but pass the product identifier instead of the product object? We need to somehow modify the INSERT request.

We use EclipseLink JPA with Spring and Vaadin.

+5
source share
1 answer

Use EntityManager.getReference() : if the object is not already loaded, it simply creates a lazy initialized proxy server around the identifier, without causing any SQL query to execute.

+7
source

Source: https://habr.com/ru/post/1215394/


All Articles