Hibernate Lazy Mode doesn't work with Spring Boot


#1

I created Spring boot Application with JPA, I have two entity classes( Account , Instance ). my entity classes look like below. this configurations are working well. but doesn’t working Hibernate Lazy Loading. anyone’s can help me please.

my English not good. i know my explanations is not good. sorry about that.

@Entity
@Table(name = "accounts")
public class Account extends Base {

private static final long serialVersionUID = 1L;
@Column(name = "name")
private String name;
  @OneToMany(mappedBy = "account", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<Instance> instances = new HashSet<>();

... getters() and Setters()...
}

@Entity
@Table(name = "instances")
public class Instance extends Base {
private static final long serialVersionUID = 1L;

@Column(name = "name")
private String name;

@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "account_id")
@JsonIdentityReference(alwaysAsId = true)
private Account account;

... getters() and Setters()...
}


#2

Most likely it’s due to the default Open Session in View antipattern employed by Spring Boot. Check out this StackOverflow answer for more details about deactivating OSIV.


#3

Thank you so much! i think lazy mode is working now. but after this implementation i got this error.

2018-Oct-18 10:09:48.245 WARN  [http-nio-8080-exec-3] o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: dev.model.Account.instances, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: dev.model.Account.instances, could not initialize proxy - no Session (through reference chain: dev.model.Account["instances"])


#4

Check out this article for more details about how you should handle the LazyInitializationException.


#5

thank you! ill read this article. :slightly_smiling_face:


#6

hello, @vlad
I read above your link and i enabled ‘ibernate.enable_lazy_load_no_trans: true’ , its working well. thank you! but still load Account With all Instances. ( look like lazy doesn’t work )

  jpa:
    open-in-view: false
    properties:
      hibernate.enable_lazy_load_no_trans: true


#7

It’s a bad idea that you activated that property. It’s worse than Open Session in View.


#8

@vlad , oh really. i understand!, ill remove it. I’m beginner (Spring boot & JPA ). so what can i do for it? ( for above i mentioned error )


#9

You need to use a JPQL query with JOIN FETCH the lazy associations.


#10

@vlad , I wrote query for it. but still i have Lazy loading problem. fetching all Instances with Account.

@Repository
public interface AccountRepository extends BaseJpaRepository<Account> {

    @Query("select c from Account c join fetch c.instances where c.id = :id")
    Account getAccount(@Param("id") UUID id);
}

#11

Since you said you were a beginner, you need to read the entire Hibernate User Guide first. Not only that you will fix this issue, but many others that you would bump into if you don’t read the documentation.


#12

i understand ill do it first!