【问题标题】:Exclude byte field of an object排除对象的字节字段
【发布时间】:2021-03-21 08:41:35
【问题描述】:

我试图从我的对象查询中排除字节字段,因为有数百或数千个报告,并且从数据库中查询它需要很长时间。

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

我尝试为此How to setup Hibernate Gradle plugin for bytecode enhancement? 设置休眠字节增强,但当我查询所有报告时仍然得到文件。我错过了什么吗?

【问题讨论】:

    标签: spring-boot hibernate maven


    【解决方案1】:

    在 JPA 中,您可以使用 @Transient 注释字段以表明它不是持久的。

    【讨论】:

      【解决方案2】:

      字节码增强应该会有所帮助,但可能是您没有正确配置它,或者您使用的 Hibernate 版本存在错误。我需要了解详细信息或查看重现的测试用例来帮助您。

      您可以尝试使用java.sql.Blob 来代替,这可以保证是惰性的并且不需要字节码增强。

      除此之外,我建议您使用 DTO 投影来实际获取您需要的数据。我认为这是Blaze-Persistence Entity Views 的完美用例。

      我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义模型之间轻松映射,例如 Spring Data Projections on steroids。这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。

      使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:

      @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();
          }
      }
      

      查询是将实体视图应用于查询的问题,最简单的就是通过 id 进行查询。

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

      Spring Data 集成让您可以像使用 Spring Data Projections 一样使用它:https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features

      DTO 投影已针对实体模型进行验证,它只会获取必要的内容。当投影过于复杂时,Spring Data Projections 会回退到“仅”包装实体,而 Blaze-Persistence Entity-Views 会改变查询,就好像您是手动编写的一样 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多