Hibernate fetch相關

來源:互聯網
上載者:User

標籤:配置   2.4   ati   boot   mvc   需要   方式   子集   gis   

fetch=FetchType.LAZY 時,spring boot jackson 返回資料時會出錯。

可配置使用Hibernate4Module 協助解決:

@Configuration
public class JacksonConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(jacksonMessageConverter());
        super.configureMessageConverters(converters);
    }

    private MappingJackson2HttpMessageConverter jacksonMessageConverter() {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new Hibernate4Module());
        messageConverter.setObjectMapper(mapper);
        return messageConverter;
    }
}

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-hibernate4</artifactId>
  <version>2.4.6</version>
</dependency>

 

fetch=FetchType.EAGER,遇到查詢很快,但hibernate 處理很慢的情況。

究其原因在於hibernate 預設使用FetchMode.JOIN,產生的sql 雖然執行很快,幾十或幾百毫秒,但是,結果記錄有幾萬甚至幾十萬,hibernate 處理需要花費數秒或數十秒。

在屬性上(實體集合)配置註解@Fetch(FetchMode.SUBSELECT),使用子查詢方式查詢子集合。結果花費時間為幾百毫秒。

附上註解說明:

public enum FetchMode {
    /**
     * use a select for each individual entity, collection, or join load.
     */
    SELECT,
    /**
     * use an outer join to load the related entities, collections or joins.
     */
    JOIN,
    /**
     * use a subselect query to load the additional collections.
     */
    SUBSELECT
}

Hibernate fetch相關

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.