0

I have an entity with a String valued field which holds a base64encoded string inside a jsonb column. This string for all entities is not null-able and will always be set at persistence time. All of the values for that field are 20Megabytes+, so I don't want to have that field transferred over from the database each and every time I query that entity out.

I only display the contents of that field some of the time on a few of my pages. Most other pages that display my entity will not display the string from the field that I want to conditionally omit from being transferred from the db.

I only want to query out the huge string value only when needed. Is it possible to tell the jpa to only select out that string when needed?

I cannot use @Transient since I do want my entity to persist the string value.

Since the string I want to omit at query time most of the time is inside a jsonb column, I presume I can have to use a native query and query out all other fields inside the jsonb object and leave out the one column that I don't need. Is that true?

I'm using an interface projection to map out all the other fields which are listed as strings in the select statement of my native query to get to all the other fields except for the one field that I don't want most of the time.

Would you know of an easier route where I can maintain some more ease if that entity ever was refactored or had a column name change?

If I wasn't using an entity that had a jsonb type column and the image was just in a regular old varchar column, would it be possible to omit any particular field using the entity manager or query builder or jpql? Something to the effect of "select s from SecurityCameraEvent s join... where..." and then when I want to get the full entity I can choose otherwise?

My main concerns are about refactoring the entity's other fields and to find out if anyone has a better solution for values embedded inside jsonb. I already know I can use a native query with a jpa projection to get the other fields out

supun
  • 113
  • 1
  • 9
  • Possible duplicate of [JPA Query selecting only specific columns without using Criteria Query?](https://stackoverflow.com/questions/24710626/jpa-query-selecting-only-specific-columns-without-using-criteria-query) – Villat Oct 18 '19 at 05:30
  • thanks for the pointer, that question answers part of my question. I'm still curious if others in the community can chime in on some of my refactor-ability concerns and if they have ideas on the postgres jsonb embedded fields. – supun Oct 19 '19 at 01:00

0 Answers0