繁体   English   中英

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

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

问题如下:

我们有实体:

@Entity
public class Feedback {
    @Id
    @GeneratedValue(generator="token")
    private String id; 

    @ManyToOne
    private Product product;

    private String message;

    // other fields
}

我们有一个服务器端点,可以从客户端收到反馈。 以多部分/基于表单的格式收到的反馈,包含以下字段:

 ProductId - product identifier
 Message - feedback message
 Some other fields

要设置Feedback.product,我们需要从JPA加载Product对象 - 这可能非常耗时并且会创建不必要的查询。

是否可以存储实体,但是传递产品ID而不是产品对象? 我们需要一些方法来修改INSERT查询。

我们在Spring和Vaadin中使用EclipseLink JPA。

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

暂无
暂无

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

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