6

I have created a simple Spring boot project with Spring data.

I have a TagGroup Entity which has one to many relation with Tags.

 @Entity
 @Table(name = "TAG_GROUP")
 public class TagGroup{

  @OneToMany(fetch=FetchType.LAZY,mappedBy = "tagGroup")
  private Set<Tag> tagList;

 }

The Tag Entity is as below

  @Entity
  @Table(name = "TAGS")
  public class Tag {

      @ManyToOne(optional = false,fetch=FetchType.LAZY)
       @JoinColumn(name = "TAG_GROUP_ID")
       private TagGroup tagGroup;

  }

I am using Spring data extending the JPArepository and using its findAll method.

The problem , the Lazy fetch doesn't work BUT Infact it is loading the tag list also without calling the tagList explicitly as If it is EAGER...

Can anybody please tell me what I am doing wrong here ?

DragonZoned
  • 201
  • 3
  • 14
  • Can you show the method/class where you are retrieving this object and expecting the LazyInitialization exception. – Madhusudana Reddy Sunnapu Mar 29 '16 at 10:44
  • Its not that I am expecting the lazy Initialization exception. Its just that the behaviour is eager when it should be lazy . I am using standard MVC stucture . The Spring data interface is autowired to service which is being called by Controller. Besides as I said this is Basic Spring boot project and nothing more here. – DragonZoned Mar 29 '16 at 11:07

1 Answers1

5

This is because of property spring.jpa.open-in-view=true.

As per the spring-boot-configuration Spring boot applications use spring.jpa.open-in-view=true.

With this property it

Register OpenEntityManagerInViewInterceptor. Binds a JPA EntityManager to the thread for the entire processing of the request.

So in your case, subsequently when you call the getTagList() i.e., retrieve the tagList, it subsequently fires another query to fetch the tagList as the the EntityManager is still open.

As you might know, LazyInitializationException is never thrown if the entityManager that has loaded the parent is still open.

To override this, you can add spring.jpa.open-in-view=false in your application.properties/application.yml and then you should see LazyInitializationException.

  • Sorry Madhusudana , spring.jpa.open-in-view=true is to keep the same EntityManager for current web request to avoid lazy initialization exception which I am not getting anyways !!! So, please forget that part. Perhaps You misread the question, I have rephrased my question. Please re read it. – DragonZoned Mar 29 '16 at 11:28
  • 1
    @DragonZoned I think I got it. So you are **not calling the tagList explicitly** still the tagList entities. That is interesting. Can you please post the sql queries that is getting fired when the call to `findAll` happens. – Madhusudana Reddy Sunnapu Mar 29 '16 at 11:34
  • @DragonZoned As per the mapping it should be lazy. I suspect, may be the tagList is getting called implicitly or `toString()` of `TagGroup`. To rule out that possibility, since, I think you must be calling the `findAll()` method from some `service` class, can you put a `System.out.println("Loaded Taggroup")` immediately after that line, where you are calling `findAll()` method. If we see any `select` query being fired to fetch the taglist after that the above `sysout` we can atleast be sure that the loading of tagList is not EAGER and bcos of some implicit call that is happening later. – Madhusudana Reddy Sunnapu Mar 29 '16 at 11:54
  • 1
    Got the problem. The problem is the serialization when I send the list as response from controller. The model bean is calling the tag list during serialization which is causing the problem. Either I will need a way to avoid hibernate calling the taglist when serializating or Use a view object to populate the model bean properties and send it to response. – DragonZoned Mar 29 '16 at 12:19
  • @DragonZoned That's cool. Is it a JSOn serialization, if so you may want to consider using `@JsonIgnore`. – Madhusudana Reddy Sunnapu Mar 29 '16 at 12:21
  • With JsonIgnore I wont be able to access TagList in UI no matter what . kinda pointless to put the taglist in the bean in the first place ... – DragonZoned Mar 30 '16 at 12:31
  • 1
    use `@JsonView` to determine when to serialize and when not to – f.khantsis Jan 19 '17 at 05:34
  • Madhusudana Reddy , thank you, it worked for me. Just like most hibernate related problems, the answer from StackOverflow works for one out of 15 people.. So i am not surprised from reading the comments. – Все Едно Aug 28 '17 at 08:33