0

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

gnanesh
  • 321
  • 1
  • 3
  • 12
  • 1
    Try annotating the field with `@Basic(fetch=FetchType.LAZY)` – Predrag Maric Mar 02 '15 at 14:53
  • I have it but i do conversion at the Web tier from Entity Object to Resource object, so how do i avoid it – gnanesh Mar 02 '15 at 15:02
  • 1
    Don't call getter for that field, because doing so will trigger the query to fetch CLOB data. Also, note that this might not work, different JPA implementations handle this in different ways. – Predrag Maric Mar 02 '15 at 15:07
  • i am using a ResourceAssembler's OrderResource toResource(Order entity), as this method is used for both Details call and Query, here i have to call getter – gnanesh Mar 02 '15 at 15:12

1 Answers1

1

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.

Heavyweight child entity:

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

Lightweight parent entity:

@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.

DJDaveMark
  • 2,669
  • 23
  • 35
  • what is your jpa implementation? do you believe that one-to-one lazy works or checked it by tracing real sql queries? – Simon Logic Aug 10 '23 at 06:24