Hibernate many-to-many lazy initialization exception
<p>I'm new in JEE and recently started learning Spring and Hibernate. I'm trying to develop RESTful service for tracking items with categories. I have many-to-many association (one item can have many categories and one category can have many items), three tables: item, category and join table item_category. </p>

<p>I'm using MySQL server, Spring Boot 1.3.1 and Hibernate 4.3</p>

<p>What I want to do right now is to get list of items with categories and categories with list of items. I wrote entire app with API for that and ended up with LazyInitializationException. I tried many approaches but it didn't help. (I can set fetch type EAGER but this is not a solution).</p>

<p>for example, this is API to list all items: <code>GET /api/item</code></p>

<p>and this is output: (not expected!):</p>

<blockquote>
  <p>Caused by: org.hibernate.LazyInitializationException: failed to lazily >initialize a collection of role: com.sk.itemlist.domain.Item.categories, could not initialize proxy - no Session
      at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
      at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
      at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
      at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160) ~[hibernate-core-4.3.10.Final.jar:4.3.10.Final]
      at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:101) ~[jackson-databind-2.6.5.jar:2.6.5]
      at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24) ~[jackson-databind-2.6.5.jar:2.6.5]
      at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.5.jar:2.6.5]
      at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675) ~[jackson-databind-2.6.5.jar:2.6.5]</p>
</blockquote>

<p>Same error if you try to get all categories <code>GET /api/category</code>, or category by id <code>GET /api/category/1</code></p>

<p>So here's my questions:</p>

<ol>
<li>How to get rid of LazyInitializationException? How can I configure hibernate to re-open session? Or should I initialize collection in DAO layer?</li>
<li>I read about DTO pattern, I don't fully understand what it is, should I dig deeper here?</li>
<li>I noticed issue when I try to insert data into items. It inserts only into item table, join table and category are empty. I manually added data into DB for testing</li>
<li>What is your feeling about entire app? What can I improve here or what should I avoid (if any)</li>
</ol>

<p>And finally, I uploaded full project here: <a href="https://gitlab.com/sksh/ItemList.git" rel="nofollow">https://gitlab.com/sksh/ItemList.git</a><br/>
In resources you can find database dump with some random data. Update application.properties with valid db credentials.</p>