我有一个由spring mvc4 + hibernate5配置的企业项目,它的所有关系都渴望并且它的性能非常差...所以我正在逐步将所有渴望的关系转变为懒惰的...但是我在每个步骤中看到很多错误...它有时能正常工作,有时却不能...
在此示例中,HeaderFromStore是RequestHeaders的实例,也是RequestLine的子级。 DeliveryPoint是requestHeader的子代,我不想获取requestHeader的deliveryPoint ...但是,如果不在select查询中使用它,就无法获取HeaderFromStore!
我使用此查询,但出现错误!
select m from MAMRequestLines m join fetch m.mamRequestHeaders r
left join fetch m.requestHeaderFromStore rr where m.id =:id
如果我使用此查询,不会出现错误
select m from MAMRequestLines m join fetch m.mamRequestHeaders r
left join fetch m.requestHeaderFromStore rr
join fetch rr.mamDeliveryPoints
left join fetch r.mamDeliveryPoints
join fetch where m.id =:id
RequestLine.java
@Entity(name = "RequestLines")
@Table(name = "_REQUEST_LINES")
//@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "@id")
public class RequestLines extends Entity implements Serializable {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private RequestHeaders requestHeaders;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private RequestHeaders requestHeaderFromStore;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "_REQUEST_Line_SEQ")
@SequenceGenerator(name = "_REQUEST_Line_SEQ", sequenceName = "_REQUEST_Line_SEQ")
@Column(name = "REQUEST_LINE_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_HEADER_ID", nullable = false)
public RequestHeaders getRequestHeaders() {
return RequestHeaders;
}
public void setRequestHeaders(RequestHeaders requestHeaders) {
this.RequestHeaders = requestHeaders;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_HEADER_FROM_STORE")
public RequestHeaders getRequestHeaderFromStore() {
return requestHeaderFromStore;
}
public void setRequestHeaderFromStore(RequestHeaders requestHeaderFromStore) {
this.requestHeaderFromStore = requestHeaderFromStore;
}
}
RequestHeader.java
@Entity(name = "RequestHeaders")
@Table(name = "REQUEST_HEADERS")
//@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class RequestHeaders extends Entity implements Serializable {
private long id;
// @JsonInclude(JsonInclude.Include.NON_EMPTY)
// @JsonIgnore
private DeliveryPoints DeliveryPoints;
@JsonIgnore
private Set<RequestLines> RequestLinesSet;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "_REQUEST_HEADERS_SEQ")
@SequenceGenerator(name = "_REQUEST_HEADERS_SEQ", sequenceName = "_REQUEST_HEADERS_SEQ")
@Column(name = "REQUEST_HEADER_ID")
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DELIVERY_POINT_ID", nullable = false)
public DeliveryPoints getDeliveryPoints() {
return DeliveryPoints;
}
public void setDeliveryPoints(DeliveryPoints DeliveryPoints) {
this.DeliveryPoints = DeliveryPoints;
}
@OneToMany(mappedBy = "RequestHeaders", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@OnDelete(action = OnDeleteAction.CASCADE)
public Set<RequestLines> getRequestLinesSet() {
return RequestLinesSet;
}
public void setRequestLinesSet(Set<RequestLines> RequestLinesSet) {
this.RequestLinesSet = RequestLinesSet;
}
}
异常(exception):
请注意,我在字段和类上使用了JsonIgnore和JsonInclude,但是它们都不起作用...
编辑:
我终于找到了这种解决方案,以避免出现异常并忽略不需要的属性。
我将这部分代码添加到
WebMvcConfig extends WebMvcConfigurerAdapter
类中:{
public MappingJackson2HttpMessageConverter jacksonMessageConverter(){
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
//Registering Hibernate4Module to support lazy objects
mapper.registerModule(new Hibernate4Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//Here we add our custom-configured HttpMessageConverter
converters.add(jacksonMessageConverter());
super.configureMessageConverters(converters);
}
但是我现在有另一个问题...所有的发布请求都将在请求正文中接收到具有null属性的....例如,在此代码中,输入中“requestHeaders”的所有属性均为null或为空!
@RequestMapping(value = "/requestHeader/", method = RequestMethod.POST, consumes = {"application/json"})
public ResponseEntity<Void> createRequestHeaders(@RequestBody RequestHeaders requestHeaders, UriComponentsBuilder ucBuilder) {
requestHeaders.setDeliveryPoints(deliveryPointsService.find(requestHeaders.getDeliveryPointsId()));
requestHeadersService.add(requestHeaders);
HttpHeaders headers = new HttpHeaders();
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
最佳答案
您应该在类定义的顶部添加与该注释相关的每个实体。
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
详细信息here和here
希望这些可以解决您的问题。