0

I'm trying to exclude byte field from my object query since there are several hundreds or thousand of reports and it takes a long time to query it from the database.

public class Reports
{
     private int id;
     
     private String reportName;
     
     @Lob
     @Basic(fetch= FetchType.LAZY)
     private byte[] file;
     
     private Date createdDate;
}

I tried setting up the hibernate byte enhancement for this How to setup Hibernate Gradle plugin for bytecode enhancement? but I'm still getting the file when I query all the reports. Did I missed something here?

chakez
  • 69
  • 9

2 Answers2

0

In JPA, you can annotate a field with @Transient to indicate that it is not persistent.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127
0

Bytecode enhancement should help, but maybe you didn't configure it correctly or the Hibernate version you are using has a bug. I'd need to know details or see a reproducing test case to help you with that.

You could try to use java.sql.Blob instead which is guaranteed to be lazy and doesn't require byte code enhancement.

Apart from that, I would recommend you use DTO projections for actually fetching just the data that you need. I think this is a perfect use case for Blaze-Persistence Entity Views.

I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids. The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.

A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:

@EntityView(Reports.class)
public interface ReportsDto {
    @IdMapping
    int getId();
    String getReportName();
    Date getCreatedDate();
    Set<ReportRowDto> getRows();

    @EntityView(ReportRows.class)
    interface ReportRowDto {
        @IdMapping
        Long getId();
        String getName();
    }
}

Querying is a matter of applying the entity view to a query, the simplest being just a query by id.

ReportsDto a = entityViewManager.find(entityManager, ReportsDto.class, id);

The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

The DTO projection is validated against the entity model and it will only fetch what is necessary. Spring Data Projections falls back to "just" wrapping entities when the projection is too complex, whereas Blaze-Persistence Entity-Views will alter the query as if you had written it by hand :)

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58