简体   繁体   English

使用给定的字段对象id存储JPA实体而不是对象本身

[英]Store JPA entity with given field object id instead of object itself

The problem is following: 问题如下:

We have 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-based format, with fields: 以多部分/基于表单的格式收到的反馈,包含以下字段:

 ProductId - product identifier
 Message - feedback message
 Some other fields

To set Feedback.product we need to load Product object from JPA - this can be time-consuming and it creates unnecessary queries. 要设置Feedback.product,我们需要从JPA加载Product对象 - 这可能非常耗时并且会创建不必要的查询。

Is it possible to store entity, but pass the product id instead of the product object? 是否可以存储实体,但是传递产品ID而不是产品对象? We need some way to modify the INSERT query. 我们需要一些方法来修改INSERT查询。

We use EclipseLink JPA with Spring and Vaadin. 我们在Spring和Vaadin中使用EclipseLink JPA。

使用EntityManager.getReference() :如果实体尚未加载,它只是在ID周围创建一个延迟初始化的代理,而不会导致执行任何SQL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM