Spring Data + JPA 如何避免加载列 [英] Spring Data + JPA how to avoid loading a column

查看:31
本文介绍了Spring Data + JPA 如何避免加载列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 6 列的表,其中一个是 CLOB 列,在针对该表进行搜索时,我不想携带此列数据,但是当用户请求详细信息时,我将无法加载它,我该如何实现使用 Spring Data JPA,我尝试在没有帮助的情况下使用 Projections + MetaModel Object

i have a table with 6 columns and one of them is a CLOB column, during search against this table i don't want to bring this column data but when user requests for the details then ill load it, how do i achieve this with Spring Data JPA, I try to use Projections + MetaModel Object with no help

推荐答案

我是通过将两列移动到一个单独的表中来做到这一点的,但是即使您无法接触到数据库,您始终可以将两个不同的实体映射到不同的列同一张桌子.

I did this by moving two columns into a separate table, but even if you can't touch the database, you can always map two different entities to different columns of the same table.

重量级子实体:

@Entity
public class RawImage implements Serializable {
    // other properties
    private String type;
    private byte[] data;
}

轻量级父实体:

@Entity
public class Model implements Serializable {
    // other properties (ID etc.)

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_image_id")
    private RawImage image;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fk_thumb_id")
    private RawImage thumb;
}

这意味着默认情况下不加载图像/拇指数据,我只是在需要它们时添加了一个获取,如下所示:

This means the image/thumb data isn't loaded by default, and I just added a fetch when I do need them with something like the following:

String jpql = "select m from Model m join fetch m.image i";

本答案中描述的其他技术,但它们并不像我认为的这个简单的解决方案那么干净.

There are other techniques described in this answer but they weren't as clean as I think this simple solution is.

这篇关于Spring Data + JPA 如何避免加载列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆