如何延迟加载实体

如何延迟加载实体

本文介绍了Jhipster,如何延迟加载实体... Pom配置,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型:您可以将其粘贴到 https://start.jhipster.tech/jdl-studio/

I have this model:You can paste it here 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:

EmployeeLog.java:

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

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

EmployeeLogRepository:

EmployeeLogRepository:

@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行,我得到:

I get all the employee, user, domains, tenants info connected with lots of images.So very slow ...for only 1 row in EmployeeLog I get:

[
  {
    "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并仅选择这些字段.有用: 我什么也没得到.

If I create an interface or a DTO and select just those fields. It works: I don't get nothing extra.

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

But I need to do this for most of the entities ...

是否有一种方法可以指示实体中的延迟负载?

还是一些简单的方法可以指示我想要的字段和不需要的字段,而无需对所有实体进行DTO?

Or some simple way to indicate the fields I want and those I don't want without the need to do DTO to all the entities?

推荐答案

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/春季最新杰克逊整合改进

AllViews.java:

AllViews.java :

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

EmployeeLog.java:

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;

EmployeeLogResource:

EmployeeLogResource:

@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(我想我已经忘记了)

I add this to pom.xml and make ./mvn clean compile (I think I had forget that)

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

这篇关于Jhipster,如何延迟加载实体... Pom配置,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:49