2017-09-13 158 views
2

我試圖發展與休眠春天REST API。 在谷歌搜索後,我還沒有找到解決延遲加載。 我有兩個實體,如下圖所示:彈簧安置延遲加載和Hibernate

University.java

@Entity() 
@Table(schema = "core", name = "university") 
public class University extends BaseEntity { 

    private String uniName; 
    private String uniTelephon; 


    @LazyCollection(LazyCollectionOption.FALSE) 
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "university", cascade = CascadeType.ALL) 
    @JsonManagedReference 
    private List<Student> students; 

//setter and getter 
} 

Student.java

@Entity 
@Table(schema = "core",name = "student") 
public class Student { 

    @Id 
    @GeneratedValue    
    private long id;   

    private String firstName;   

    private String lastName;   

    private String section; 

    @ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name = "UNIVERSITY_ID",nullable = false) 
    @JsonBackReference 
    private University university; 
    // setter and getter 
} 

任我休息終點

@GetMapping("/list") 
public ResponseEntity list() throws Exception { 
    // I need to return just Universities But it return it eagerly with their students 
    return new ResponseEntity(this.universityService.findAll(), HttpStatus.OK); 
} 

調用REST API後,它與所有學生一起返回大學。

有一種方式來告訴傑克遜不序列化未提取對象或集合?

有人可以幫助我已驗證解決方案?

回答

3

嘗試添加以下扶養(取決於你的Hibernate版本):

<dependency> 
    <groupId>com.fasterxml.jackson.datatype</groupId> 
    <artifactId>jackson-datatype-hibernate5</artifactId> 
    <version>${jackson.version}</version> 
</dependency> 

然後(假設你有一個基於Java的配置)中添加以下的WebMvcConfigurerAdapter類:

@Override 
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
    converters.add(jackson2HttpMessageConverter()); 
} 

@Bean 
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() { 
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
    converter.setObjectMapper(this.jacksonBuilder().build()); 

    return converter; 
} 

public Jackson2ObjectMapperBuilder jacksonBuilder() { 
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); 

    Hibernate5Module hibernateModule = new Hibernate5Module(); 

    hibernateModule.configure(Feature.FORCE_LAZY_LOADING, false); 

    builder.modules(hibernateModule); 

    // Spring MVC default Objectmapper configuration 
    builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
    builder.featuresToDisable(MapperFeature.DEFAULT_VIEW_INCLUSION); 

    return builder; 
} 

它應該強制Jackson的objectMapper不會獲取延遲加載的值。

+0

其通過以下異常HTTP狀態500 - 無法寫入內容:無法懶惰地初始化角色集合:a2.asena.cashModel.University.students,無法初始化代理 - 無會話(通過引用鏈:java。 util.ArrayList [0] - > a2.asena.cashModel.University [ 「學生」]);嵌套的異常是com.fasterxml.jackson.databind.JsonMappingException:未能懶惰地初始化一個角色集合:a2.asena.cashModel.University.students,無法初始化代理 - 沒有會話(通過引用鏈:java.util.ArrayList [ 0] - > a2.asena.cashModel.University [「students」]) – Generic

+0

for above exception solution is overriding Filter in Appinitializer @Override protected Filter [] getServletFilters(){ return new Filter [] {new HiddenHttpMethodFilter(),新MultipartFilter(),新OpenEntityManagerInViewFilter()};是 – Generic

+0

一些文章說這是不正確的解決方案 – Generic