[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Calling One Session From Another And Getting LazyInitializat

tlien do-not-reply at jboss.com
Wed Jan 31 16:33:57 EST 2007


I am passing an entity with a lazy loaded property from one session bean to another sessionbean and getting a LazyInitializationException. It's still inside of jboss so I don't understand why it can't load it. Is this expected behavior? If so, what is the correct way to do it. Heres a simple example of what I am talking about.


@Entity
@Table(name = "LazyChild")
public class LazyChild implements java.io.Serializable {
	private Integer key;
	private String name;
	
	@Id @GeneratedValue
	@Column(name="LazyChildKey")
	public Integer getKey() { return key; }
	public void setKey(Integer key) { this.key = key; }
	
	public String getName() { return name; }
	public void setName(String s) { name = s; }
}

@Entity
@Table(name = "Test")
public class TestEntity implements java.io.Serializable {
	private Integer key;
	private LazyChild lazyChild;
	
	@Id @GeneratedValue
	@Column(name="TestKey")
	public Integer getKey() { return key; }
	public void setKey(Integer key) { this.key = key; }
	
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "LazyChildKey")
	public LazyChild getLazyChild() { return lazyChild; }
	public void setLazyChild(LazyChild l) { lazyChild = l; }
	
}

@Stateless
@RemoteBinding (jndiBinding="TestSessionHelper")
@Remote(TestSessionHelper.class)
public class TestSessionHelperBean {
	@PersistenceContext
	protected EntityManager em;
	
	public void useLazyChild(TestEntity entity) {
		entity.getLazyChild().getName();
	}	

}

@Stateless
@RemoteBinding (jndiBinding="TestSession")
@Remote(TestSession.class)
public class TestSessionBean {
	@PersistenceContext
	protected EntityManager em;

	@EJB TestSessionHelper m_sessionHelper;
	
	public void doIt() {
		TestEntity entity = em.find(TestEntity.class, 1);
		m_sessionHelper.useLazyChild(entity);
	}

}

public class ClientSideTest {
	public static void main(String[] args) throws Exception {
		InitialContext ctx = new InitialContext();
		TestSession session = (TestSession)ctx.lookup("TestSession");
		session.doIt();
	}
}


When I deploy these session beans and run ClientSideTest.main I get the following exception on the server side.

15:28:52,109 ERROR [LazyInitializationException] could not initialize proxy - no
 Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Sessi
on
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyIn
itializer.java:57)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(Abstrac
tLazyInitializer.java:111)
        at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.intercept(CGLIBLa
zyInitializer.java:160)
        at alliancetechnical.ejb3.session.LazyChild$$EnhancerByCGLIB$$6f310ca4.g
etName()
        at alliancetechnical.ejb3.session.TestSessionHelperBean.useLazyChild(Tes
tSessionHelperBean.java:14)
...
        at $Proxy670.useLazyChild(Unknown Source)
        at alliancetechnical.ejb3.session.TestSessionBean.doIt(TestSessionBean.j
ava:19)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4009039#4009039

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4009039



More information about the jboss-user mailing list