Jhipster,如何延迟加载实体...... Pom 配置,

Jhipster, How to Lazy Load entities ... Pom config,

我有这个型号: 你可以把它贴在这里https://start.jhipster.tech/jdl-studio/

entity NmsDomain {
  name String required,
  logo ImageBlob,
  brandImg ImageBlob
}


entity NmsTenant {
  name String required,
  image ImageBlob
}


entity NmsZone {
  name String required,
  description String,
  active Boolean,
  lastModifiedDate ZonedDateTime,
  lastModifiedBy String 
}

entity NmsEmployee {
  extensionNumber String,
  photo ImageBlob
}


entity NmsEmployeeLog {
  begin ZonedDateTime,
  end ZonedDateTime,
  deviceType String,
  make String, 
  model String, 
  imei String, 
  serialNumber String, 
  ipAddress String, 
  macAddress String, 
  wifiNetwork String
}

relationship OneToOne {
  NmsEmployee{user(firstName)} to User
  }


  relationship ManyToMany {
  NmsEmployee{possibleZones(name)} to NmsZone{employeePossible}, 
  NmsEmployee{activeZones(name)} to NmsZone{employeeActive}, 
  NmsEmployee{possibleTenants(name)} to NmsTenant{employeePossible}, 
  NmsDomain{user(firstName)} to User{NmsDomain}
}


relationship ManyToOne {
  NmsEmployeeLog{employee} to NmsEmployee, 
  NmsEmployeeLog{zone(name)} to NmsZone, 
  NmsEmployee{domain(name)} to NmsDomain,
  NmsEmployee{tenant(name)} to NmsTenant, 
 NmsTenant{domain(name)} to NmsDomain, 
  NmsZone{tenant(name)} to NmsTenant
}

EmployeeLog.java:

    @ManyToOne(fetch = FetchType.LAZY) 
    @JsonIgnoreProperties("nmsEmployeeLogs")
    private NmsEmployee employee;

    @ManyToOne
    @JsonIgnoreProperties("nmsEmployeeLogs")
    private NmsZone zone;

员工日志存储库:

@Query(value ="SELECT entity FROM NmsEmployeeLog entity WHERE entity.employee.id = :empId",
countQuery ="select count(entity) FROM NmsEmployeeLog entity WHERE entity.employee.id = :empId")

Page<NmsEmployeeLog> findByEmpId(@Param("empId") Long empId, Pageable pageable);

我得到了所有与大量图像相关的员工、用户、域、租户信息。 所以很慢... 对于 EmployeeLog 中只有 1 行,我得到:

[
  {
    "id": 6,
    "begin": "2019-02-03T07:54:00Z",
    "end": "2019-02-03T07:54:00Z",
    "deviceType": null,
    "make": null,
    "model": null,
    "imei": null,
    "serialNumber": null,
    "ipAddress": null,
    "macAddress": null,
    "wifiNetwork": null,
    "employee": {
      "id": 11,
      "extensionNumber": "1223",
      "photo": "iVBORw0KGgoAAAANSUhEUg ….LOTS OF IMAGE CODE.”,
      "photoContentType": "image/png",

      "user": {
        "id": 3,
        "login": "admin",
        "firstName": "Admin",
        "lastName": "IGomes",
        "email": “m…”,
        "activated": true,
        "langKey": "pt-pt",
        "imageUrl": "",
        "resetDate": null
      },
      "domain": {
        "id": 2,
        "name": "Fe ",
        "logo": "/9j/4 ….LOTS OF IMAGE CODE”,
        "brandImgContentType": "image/jpeg",
        "brandLink": null

      },
      "tenant": {
        "id": 4,
        "name": "Heathlands V",
        "image": “LOTS OF IMAGE CODE…..”,
        "imageContentType": "image/jpeg",
        "domain": {
          "id": 2,
          "name": "Feds ",
          "logo": "/9j/4A== ….”,
          "logoContentType": "image/jpeg",
          "brandImg": "/9j/4AAQSkAAH//Z …..LOTS OF IMAGE CODE”,
          "brandImgContentType": "image/jpeg"

        }
      },
      "possibleZones": null,
      "activeZones": null,
      "possibleTenants": null
    },
    "zone": {
      "id": 27,
      "name": "EHBlk EH1",
      "description": "EH Block 1",
      "active": true,
      "lastModifiedDate": "2019-09-28T11:39:32Z",
      "lastModifiedBy": "Louis ",
      "tenant": {
        "id": 4,
        "name": "Heathlands V",
        "image": "/9j/4AAQSkZJRgABAQAAA …. LOTS OF IMAGE CODE”,
        "imageContentType": "image/jpeg",
        "domain": {
          "id": 2,
          "name": "Fed ",
          "logo":  LOTS OF IMAGE CODE….”,
          "brandImgContentType": "image/jpeg",
        }
      }
    }
  },
  {
Next Row Log ...

而不只是这些字段:

public interface EmployeeLog {
     Long getId();
     ZonedDateTime getBegin();
     ZonedDateTime getEnd();
     String getDevice();
     String getType();
     String getMake();
     String getModel();
     String getImei();
     String getSerialNumber();
     String getIpAddress();
     String getMacAddress();
     String getWifiNetwork();
}

如果我创建一个接口或 DTO 并且 select 只是那些字段。有用: 我没有得到任何额外的东西。

但我需要为大多数实体执行此操作...

有没有办法在实体中指示延迟加载?

或者一些简单的方法来指示我想要的字段和我不需要的字段而不需要对所有实体执行 DTO?

1 - json 视图注释(更灵活)

https://mkyong.com/java/jackson-jsonview-examples/ 要么 https://www.logicbig.com/tutorials/misc/jackson/json-view-annotation.html 要么 https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

AllViews.java :

public class AllViews {
     public interface List {}
     public interface Edit {}
}

EmployeeLog.java:

   @JsonView(AllViews.List.class)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JsonView(AllViews.List.class)
    @Column(name = "begin")
    private ZonedDateTime begin;

    @JsonView(AllViews.List.class)
    @Column(name = "end")
    private ZonedDateTime end;

    @JsonView(AllViews.List.class)
    @Column(name = "device_type")
    private String deviceType;

    @Column(name = "make")
    private String make;

    @JsonView(AllViews.List.class)
    @Column(name = "model")
    private String model;

    @Column(name = "imei")
    private String imei;

    @JsonView(AllViews.List.class)
    @Column(name = "serial_number")
    private String serialNumber;

    @JsonView(AllViews.List.class)
    @Column(name = "ip_address")
    private String ipAddress;

    @Column(name = "mac_address")
    private String macAddress;

    @JsonView(AllViews.List.class)
    @Column(name = "wifi_network")
    private String wifiNetwork;

员工日志资源:

@JsonView(AllViews.List.class)
    @GetMapping("/nms-employee-logsByEmployeeId/{empId}")
    public ResponseEntity<List<NmsEmployeeLog>> getResidentsByTenantId(final Pageable pageable,
            @PathVariable final Long empId) {
        log.debug("REST request to get a page of NmsEmployeeLog ByTenantId");

        final Page<NmsEmployeeLog> page = nmsEmployeeLogRepository.findByEmpId(empId, pageable);
        final HttpHeaders headers = PaginationUtil
                .generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

结果:

[

  {
    "id": 6,
    "begin": "2019-02-03T08:54:00+01:00",
    "end": "2019-02-03T08:54:00+01:00",
    "deviceType": some device,
    "model": some model,
    "serialNumber": xxxxx,
    "ipAddress": xx.xx.xx,
    "wifiNetwork": wifi
  },

2- 或 The Lazy Way(不那么灵活)

我将其添加到 pom.xml 并制作 ./mvn clean compile(我想我忘记了)

        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <version>${hibernate.version}</version>
            <executions>
                <execution>
                    <configuration>
                        <failOnError>true</failOnError>
                        <enableLazyInitialization>true</enableLazyInitialization>
                    </configuration>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>