spring mvc 4 + hibernate 5延迟加载配置

问题描述 投票:10回答:1

我有一个由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

request line.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;
    }

}

request header.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;
        }


    }

例外:

没有找到类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过引用链:domain.RequestLine [“HeaderFromStore”] - >。 domain.RequestHeaders [ “DeliveryPoint”] - > domain.DeliveryPoint _ _ $$ jvst393_f [ “处理程序”])

注意我使用了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);

    }
java spring hibernate spring-mvc lazy-loading
1个回答
0
投票

你应该在类定义的顶部添加每个与这个注释有关系的实体。

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

细节解释herehere

我希望这些能解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.