-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: LazyLoadingException when using EntityManager
PostPosted: Tue Apr 25, 2006 7:10 pm 
Newbie

Joined: Tue Apr 25, 2006 6:42 pm
Posts: 5
Location: Sao Paulo, Brazil
Hi All!

I've post this in the JBoss forum but got no response, maybe you will be able to help me...

I'm trying to send an EJB Persistent Entity to my JSP but when trying to display it's properties they are enhanced by cglib and considered lazy as per Hibernate 3 defaults and I get a LazyLoadingException. I've been working with Hibernate 2 and 3 for long and I know that to be able to read a lazy property I should have an open persistence context (session), which is not what I want as I want to send a simple object to my page and display its props.

I was able to disable the lazy loading by creating the hibernate hbm file and setting the class as lazy="false" but I'd like to use just pure EJB3 elements (class + annotations) and no hibernate mappings. So how can I turn off simple properties lazy loading?

I'm using JBoss 4.0.4CR2.

Thanks in advance.

Daniel Amadei


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 9:43 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Code:
/**
* Lazy and proxy configuration of a particular class
*
* @author Emmanuel Bernard
*/
@Target(TYPE) @Retention(RUNTIME)
public @interface Proxy {
   /**
    * Whether this class is lazy or not (default to true)
    */
   boolean lazy() default true;

   /**
    * Proxy class or interface used. Default entity class name.
    */
   Class proxyClass() default void.class;
}

@Entity
@Lazy(lazy=false)
public class MyEntity implements Serializable {
...
}



Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
just to be sure, properties are *not* lazy by default, associations are

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:55 am 
Newbie

Joined: Tue Apr 25, 2006 6:42 pm
Posts: 5
Location: Sao Paulo, Brazil
Hi Emmanuel,

I'm not setting anything saying that the properties should be lazy but they still are requiring a open session to be read as they are being enhanced by CGLIB, so I believe the properties are being lazy loaded.

I've got the same problems in a system I built using hibernate 2.0 when I migrated to hibernate 3.0, which forced me to include the lazy=false in the class mapping and keeping lazy=true for the associations which I found ok to be lazy.

I'm not saying you are wrong, but I can be doing something which is causing my props to be lazy by default, and I must be doing this for a long time :)

Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 12:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
properties might be lazy if you specifically enhance your classes at build time and you change the default value to @Basic(fetch=LAZY)
otherwise properties are not lazy and you can access them outside the scope of a session.

associations (*tomany or *to one) are a different story

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 1:27 pm 
Newbie

Joined: Tue Apr 25, 2006 6:42 pm
Posts: 5
Location: Sao Paulo, Brazil
Hi Emmanuel,

What you are saying, is not what is happening when I use Hibernate Entity Manager or JBoss EJB3 with the default options. I'm sending bellow my entity and the mapping, maybe you can help me and tell me what I'm doing wrong or what is missing.


Entity:

package test;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Pessoa implements Serializable {

@Id
@GeneratedValue
private Long id;

private String nome;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}
}

Mapping:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="manager1" transaction-type="JTA">
<jta-data-source>java:/MySqlDS</jta-data-source>
<class>test.Pessoa</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>

Exception:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:102)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:160)
test.Pessoa$$EnhancerByCGLIB$$c98b058b.toString(<generated>)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 2:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
show me the full stacktrace please and the piece of code raising the excepitob

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 7:25 am 
Newbie

Joined: Tue Apr 25, 2006 6:42 pm
Posts: 5
Location: Sao Paulo, Brazil
Hi Emmanuel,

Bellow the ST, I'm posting the JSP test page I'm using to lookup the session bean (session facade) and to display the toString of the Entity Bean and also my session bean which gets the reference for the persistent object (of Pessoa class). This is the full ST:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:102)
org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:160)
test.Pessoa$$EnhancerByCGLIB$$dfe1d78f.toString(<generated>)
java.lang.String.valueOf(String.java:2577)
org.apache.jasper.runtime.JspWriterImpl.print(JspWriterImpl.java:480)
org.apache.jsp.lookup_jsp._jspService(lookup_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


This is my test JSP:

<%@page import="javax.naming.*, test.*, javax.transaction.*"%>
<%
Service s = (Service)new InitialContext().lookup("ServiceImpl/remote");
%>
<%=s.buscar(new Long(1))%>



This is my test Session Facade:

package test;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class ServiceImpl implements Service {

@PersistenceContext
private EntityManager em;

public Pessoa buscar(Long id) {
return em.getReference(Pessoa.class, id);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 7:38 am 
Newbie

Joined: Tue Jan 03, 2006 7:30 am
Posts: 18
Location: Budapest, Hungary
danielamadei wrote:
Code:
   public Pessoa buscar(Long id) {
      return em.getReference(Pessoa.class, id);
   }

You aren't retrieving the entity, only a proxy to it. Use em.find() instead of em.getReference().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.