title: Query in Hibernate
body: <p>I am using hibernate to map the data to my mysql database in a Restful Web Service(using jersey).</p>
  
  <p>This is my Activity class : </p>
  
  <pre><code>@Entity
  public class Activity {
  
      private static final long serialVersionUID = 4937176332500551910L;
  
      @Id
      @GeneratedValue
      private long id;
  
      @Column(name = "short", unique = true, nullable = false)
      private String key;
  
      private String name;
  
      private String description;
  
      private float cost;
  
      private int orderIndex = 0;
  
      @ManyToOne(fetch = FetchType.LAZY)
      private Activity parent;
  
      @OneToMany(targetEntity = Activity.class, mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JsonIgnore
      private Collection&lt;Activity&gt; children;
  
      @OneToMany(targetEntity = Project.class, mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JsonIgnore
      private Collection&lt;Project&gt; projects;
  
      @OneToMany(targetEntity = Task.class, mappedBy = "activity",  fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JsonIgnore
      private Collection&lt;Task&gt; tasks;
  </code></pre>
  
  <p>This query works perfect :</p>
  
  <pre><code>activities = session.createQuery("from Activity").list();
  </code></pre>
  
  <p>The result as a Json array is : </p>
  
  <pre><code>[{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},{"id":101,"key":"c","name":"Consulting","description":"Just in special cases, authorized by Rüdiger after discussion with customer.","cost":990.0,"orderIndex":3,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":103,"key":"sales","name":"Sales","description":"Sales related activities","cost":0.0,"orderIndex":2,"parent":null},{"id":104,"key":"doit","name":"Genomför arbeten","description":"","cost":690.0,"orderIndex":1,"parent":{"id":103,"key":"sales","name":"Sales","description":"Sales related activities","cost":0.0,"orderIndex":2,"parent":null}},{"id":105,"key":"t","name":"Traveling","description":"Travel time","cost":300.0,"orderIndex":4,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":106,"key":"uni","name":"University Job","description":"Activities at university","cost":690.0,"orderIndex":3,"parent":null},{"id":107,"key":"work","name":"work","description":"One rate for everything: teaching, preparing, administration, meetings, etc.","cost":690.0,"orderIndex":1,"parent":{"id":106,"key":"uni","name":"University Job","description":"Activities at university","cost":690.0,"orderIndex":3,"parent":null}},{"id":108,"key":"v","name":"Vacation/Holidays","description":"Planned vacation (counting agains the 25 days)","cost":300.0,"orderIndex":5,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}}]
  </code></pre>
  
  <p>This query also works:</p>
  
  <pre><code>activities = session.createQuery("from Activity where id=100").list();
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>[{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}]
  </code></pre>
  
  <p><code>scrum</code> Activity is a parent as you see.</p>
  
  <p>but this query does not work : </p>
  
  <pre><code>activities = session.createQuery("from Activity where id=101").list();
  </code></pre>
  
  <p>Because Activity with id=101 is a kind of Child Activity.</p>
  
  <p>I got this error:</p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;&lt;title&gt;Apache Tomcat/7.0.23 - Error report&lt;/title&gt;&lt;style&gt;&lt;!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--&gt;&lt;/style&gt; &lt;/head&gt;&lt;body&gt;&lt;h1&gt;HTTP Status 500 - &lt;/h1&gt;&lt;HR size="1" noshade="noshade"&gt;&lt;p&gt;&lt;b&gt;type&lt;/b&gt; Exception report&lt;/p&gt;&lt;p&gt;&lt;b&gt;message&lt;/b&gt; &lt;u&gt;&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;description&lt;/b&gt; &lt;u&gt;The server encountered an internal error () that prevented it from fulfilling this request.&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;exception&lt;/b&gt; &lt;pre&gt;org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&amp;gt;se.softwerk.timelog.model.Activity[&amp;quot;parent&amp;quot;]-&amp;gt;se.softwerk.timelog.model.Activity_$$_javassist_3[&amp;quot;id&amp;quot;])
      org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
      org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
      org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
      org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
      org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
      org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
      org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
      org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
      org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122)
      org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71)
      org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
      org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
      org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
      org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
      org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:558)
      com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
      com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
      com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
      com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;root cause&lt;/b&gt; &lt;pre&gt;org.hibernate.LazyInitializationException: could not initialize proxy - no Session
      org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
      org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
      org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
      se.softwerk.timelog.model.Activity_$$_javassist_3.getId(Activity_$$_javassist_3.java)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      java.lang.reflect.Method.invoke(Unknown Source)
      org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)
      org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418)
      org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
      org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
      org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
      org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
      org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
      org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122)
      org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71)
      org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
      org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
      org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
      org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
      org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:558)
      com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
      com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
      com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
      com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
      com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
      com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  &lt;/pre&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;note&lt;/b&gt; &lt;u&gt;The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.&lt;/u&gt;&lt;/p&gt;&lt;HR size="1" noshade="noshade"&gt;&lt;h3&gt;Apache Tomcat/7.0.23&lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Can you help me?</p>