问题
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:
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnoreProperties("nmsEmployeeLogs")
private NmsEmployee employee;
@ManyToOne
@JsonIgnoreProperties("nmsEmployeeLogs")
private NmsZone zone;
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);
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 ...
instead of just these fields:
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();
}
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 ...
Is there a way to indicate lazy load in the entity?
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:
1 - The json view annotation (more flexible)
https://mkyong.com/java/jackson-jsonview-examples/ or https://www.logicbig.com/tutorials/misc/jackson/json-view-annotation.html or 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;
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());
}
result:
[
{
"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- or The Lazy Way (not so flexile)
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>
来源:https://stackoverflow.com/questions/59987797/jhipster-how-to-lazy-load-entities-pom-config