gpt4 book ai didi

java - spring mvc 4 + hibernate 5 懒加载配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:23 25 4
gpt4 key购买 nike

我有一个由 spring mvc4 + hibernate5 配置的企业项目,它的所有关系都是渴望的并且性能非常糟糕......所以我正在逐步将所有渴望的关系转变为惰性......但我看到了很多错误在每一步中......它有时工作正常,有时不......

在此示例中,HeaderFromStore 是 RequestHeaders 的实例和 RequestLine 的子项。 DeliveryPoint 是 requestHeader 的子项,我不想获取 requestHeader 的 deliveryPoint...但是如果不在选择查询中使用它,则无法获取 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

请求行.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):

No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: domain.RequestLine["HeaderFromStore"]->.domain.RequestHeaders["DeliveryPoint"]->domain.DeliveryPoint_$$_jvst393_f["handler"])

请注意,我使用了 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"})

详情说明herehere

希望这些能解决您的问题。

关于java - spring mvc 4 + hibernate 5 懒加载配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46806570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com