illegal access to loading collection

G

graf.laszlo

Hi

Reading the Hibernate ref. I decided to try a small example, the
weblog.
I am using JBOSS-4.0.4.GA, EJB3 and hibernate-3.2. When I want to
create a new BlogItems object, by adding it to the list of BlogItems,
wich belongs to Blogs entity, I get the

illegal access to loading collection

error message.

These are the Java sources:

- the Blogs.java (ENTITY)

package weblog;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "weblog.blogs", uniqueConstraints =
{ @UniqueConstraint(columnNames = { "name" }) })
public class Blogs implements java.io.Serializable {

private static final long serialVersionUID = 255571063365822285L;
private int blogId;
private String name;
private Set<BlogItems> blogItemses = new HashSet<BlogItems>(0);

public Blogs() {}

public Blogs(int blogId) {
this.blogId = blogId;
}

public Blogs(int blogId, String name) {
this.blogId = blogId;
this.name = name;
}

public Blogs(int blogId, String name, Set<BlogItems> blogItemses) {
this.blogId = blogId;
this.name = name;
this.blogItemses = blogItemses;
}

@Id
@Column(name = "blog_id", unique = true, nullable = false, insertable
= true, updatable = true)
public int getBlogId() { return this.blogId; }
public void setBlogId(int blogId) { this.blogId = blogId; }

@Column(name = "name", unique = true, nullable = false, insertable =
true, updatable = true, length = 50)
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY,
mappedBy = "blogs")
public Set<BlogItems> getBlogItemses() { return this.blogItemses; }
public void setBlogItemses(Set<BlogItems> blogItemses)
{ this.blogItemses = blogItemses; }

@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + blogId;
result = PRIME * result + ((blogItemses == null) ? 0 :
blogItemses.hashCode());
result = PRIME * result + ((name == null) ? 0 : name.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Blogs other = (Blogs) obj;
if (blogId != other.blogId)
return false;
if (blogItemses == null) {
if (other.blogItemses != null)
return false;
} else if (!blogItemses.equals(other.blogItemses))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}

- the BlogItems (ENTITY)

package weblog;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "weblog.blog_items", uniqueConstraints = {})
public class BlogItems implements java.io.Serializable {

private static final long serialVersionUID = 288936795343606406L;
private int blogItemId;
private Blogs blogs;
private String title;
private String text;
private Date datetime;

public BlogItems() { }

public BlogItems(int blogItemId, String title) {
this.blogItemId = blogItemId;
this.title = title;
this.text = title;
this.datetime = new Date();
}

public BlogItems(int blogItemId, Blogs blogs, String title, String
text,
Date datetime) {
this.blogItemId = blogItemId;
this.blogs = blogs;
this.title = title;
this.text = text;
this.datetime = datetime;
}

@Id
@Column(name = "blog_item_id", unique = true, nullable = false,
insertable = true, updatable = true)
public int getBlogItemId() {
return this.blogItemId;
}

public void setBlogItemId(int blogItemId) {
this.blogItemId = blogItemId;
}

@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "blog", unique = false, nullable = false,
insertable = true, updatable = true)
public Blogs getBlogs() {
return this.blogs;
}

public void setBlogs(Blogs blogs) {
this.blogs = blogs;
}

@Column(name = "title", unique = false, nullable = false, insertable
= true, updatable = true, length = 50)
public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

@Column(name = "text", unique = false, nullable = false, insertable =
true, updatable = true, length = 50)
public String getText() {
return this.text;
}

public void setText(String text) {
this.text = text;
}

@Temporal(TemporalType.DATE)
@Column(name = "datetime", unique = false, nullable = false,
insertable = true, updatable = true, length = 4)
public Date getDatetime() {
return this.datetime;
}

public void setDatetime(Date datetime) {
this.datetime = datetime;
}

@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + blogItemId;
result = PRIME * result + ((blogs == null) ? 0 : blogs.hashCode());
result = PRIME * result + ((datetime == null) ? 0 :
datetime.hashCode());
result = PRIME * result + ((text == null) ? 0 : text.hashCode());
result = PRIME * result + ((title == null) ? 0 : title.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final BlogItems other = (BlogItems) obj;
if (blogItemId != other.blogItemId)
return false;
if (blogs == null) {
if (other.blogs != null)
return false;
} else if (!blogs.equals(other.blogs))
return false;
if (datetime == null) {
if (other.datetime != null)
return false;
} else if (!datetime.equals(other.datetime))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}

and the STATELESS beans:

package weblog;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Stateless
public class BlogsBean implements BlogsRemote, BlogsLocal,
java.io.Serializable {

private static final long serialVersionUID = 2078312303442351155L;
private static final Log log = LogFactory.getLog(BlogsBean.class);

@PersistenceContext(unitName="webloan")
private EntityManager entityManager;

public void persist(Blogs transientInstance) {
log.debug("persisting Blogs instance");
try {
entityManager.persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}

public void remove(Blogs persistentInstance) {
log.debug("removing Blogs instance");
try {
entityManager.remove(persistentInstance);
log.debug("remove successful");
} catch (RuntimeException re) {
log.error("remove failed", re);
throw re;
}
}

public Blogs merge(Blogs detachedInstance) {
log.debug("merging Blogs instance");
try {
Blogs result = entityManager.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public Blogs findById(int id) {
log.debug("getting Blogs instance with id: " + id);
try {
Blogs instance = entityManager.find(Blogs.class, id);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

public void addItem(int blog_id, int blogitem_id, String text) {
log.debug("1");
try {
Blogs instance = entityManager.find(Blogs.class, blog_id);
BlogItems bi = new BlogItems(blogitem_id, text);
instance.getBlogItemses().add(bi);
Blogs result = entityManager.merge(instance);
log.debug("merge successful");
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

}

package gl.weblog;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Stateless
public class BlogItemsBean implements BlogItemsRemote, BlogItemsLocal,
java.io.Serializable {

private static final long serialVersionUID = -6463759247311226709L;
private static final Log log =
LogFactory.getLog(BlogItemsBean.class);

@PersistenceContext(unitName="webloan")
private EntityManager entityManager;

public void persist(BlogItems transientInstance) {
log.debug("persisting BlogItems instance");
try {
entityManager.persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}

public void remove(BlogItems persistentInstance) {
log.debug("removing BlogItems instance");
try {
entityManager.remove(persistentInstance);
log.debug("remove successful");
} catch (RuntimeException re) {
log.error("remove failed", re);
throw re;
}
}

public BlogItems merge(BlogItems detachedInstance) {
log.debug("merging BlogItems instance");
try {
BlogItems result = entityManager.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public BlogItems findById(int id) {
log.debug("getting BlogItems instance with id: " + id);
try {
BlogItems instance = entityManager.find(BlogItems.class, id);
log.debug("get successful");
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
}

My client source looks like this:

package weblog;

import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;

public class Test01 {
Blogs ablogok;
BlogItems ablogitemek;

Test01(String blog_id, String blogitem_id, String blogitem_title) {
System.out.println("- 0:"+blog_id);
System.out.println("- 1:"+blogitem_id);
System.out.println("- 2:"+blogitem_title);
}

public void addItemToBlog(String blog_id, String blogitem_id, String
blogitem_title) {
Integer BLOG_ID = new Integer(blog_id);
Integer BLOGITEM_ID = new Integer(blogitem_id);
try {
Properties properties = new Properties();

properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");

properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:eek:rg.jnp.interfaces");
properties.put("java.naming.provider.url","192.1xx.1xx.1xx:1099");
InitialContext ctx = new InitialContext();
BlogsInterface bf = (BlogsInterface) ctx.lookup("BlogsBean/
remote");
bf.addItem(BLOG_ID.intValue(), BLOGITEM_ID.intValue(),
blogitem_title);
} catch(Exception ex) {
System.out.println("ERROR: "+ex.getMessage()+"\n");
//ex.printStackTrace();
}
}

public static void main(String[] args) {
Test01 t1 = new Test01(args[0], args[1], args[2]);
t1.addItemToBlog(args[0], args[1], args[2]);
t1 = null;
}
}



and the error stack
Code:

15:27:50,075 ERROR [LazyInitializationException] illegal access to
loading collection
org.hibernate.LazyInitializationException: illegal access to loading
collection
at
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollec
tion.java:341)
at
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.j
ava:86)
at
org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:
355)
at weblog.Blogs.hashCode(Blogs.java:58)
at weblog.BlogItems.hashCode(BlogItems.java:101)
at java.util.HashMap.put(HashMap.java:372)
at java.util.HashSet.add(HashSet.java:200)
at java.util.AbstractCollection.addAll(AbstractCollection.java:
238)
at
org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:273)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.jav
a:183)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.ja
va:268)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.ja
va:249)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:
866)
at
org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:
853)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:
1919)
at
org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:
36)
at
org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollect
ionPersister.java:541)
at
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(D
efaultInitializeCollectionEventListener.java:60)
at
org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:
1705)
at
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollec
tion.java:344)
at
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.j
ava:86)
at
org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersis
tentCollection.java:142)
at
org.hibernate.collection.PersistentSet.add(PersistentSet.java:162)
at weblog.BlogsBean.addItem(BlogsBean.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:589)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
112)
at
org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:
166)
at
org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor
..java:63)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedE
ntityManagerInterceptor.java:54)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:
47)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:
79)
at org.jboss.aspects.tx.TxInterceptor
$Required.invoke(TxInterceptor.java:197)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:
76)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor
..java:62)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.jav
a:78)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:
47)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:
1
06)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:
225)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at
org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandl
er.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:
828)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:
681)
at
org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:
358)
at
org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:
398)
at
org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:
239)
15:27:50,081 ERROR [BlogsBean] merge failed
org.hibernate.LazyInitializationException: illegal access to loading
collection
at
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollec
tion.java:341)
at
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.j
ava:86)
at
org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:
355)
at weblog.Blogs.hashCode(Blogs.java:58)
at weblog.BlogItems.hashCode(BlogItems.java:101)
at java.util.HashMap.put(HashMap.java:372)
at java.util.HashSet.add(HashSet.java:200)
at java.util.AbstractCollection.addAll(AbstractCollection.java:
238)
at
org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:273)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.jav
a:183)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.ja
va:268)
at
org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.ja
va:249)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:
866)
at
org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:
853)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at
org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:
1919)
at
org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:
36)
at
org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollect
ionPersister.java:541)
at
org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(D
efaultInitializeCollectionEventListener.java:60)
at
org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:
1705)
at
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollec
tion.java:344)
at
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.j
ava:86)
at
org.hibernate.collection.AbstractPersistentCollection.readElementExistence(AbstractPersis
tentCollection.java:142)
at
org.hibernate.collection.PersistentSet.add(PersistentSet.java:162)
at weblog.BlogsBean.addItem(BlogsBean.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:589)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
112)
at
org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:
166)
at
org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor
..java:63)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedE
ntityManagerInterceptor.java:54)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:
47)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:
79)
at org.jboss.aspects.tx.TxInterceptor
$Required.invoke(TxInterceptor.java:197)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:
76)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor
..java:62)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.jav
a:78)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:
47)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:
1
06)
at
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:
101)
at
org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:
225)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at
org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandl
er.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:
828)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:
681)
at
org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:
358)
at
org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:
398)
at
org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:
239)



Please help me.
Thank you
 
Joined
Mar 22, 2007
Messages
1
Reaction score
0
Hi,
I have exactly the same problem. (bidirectional relationship, lazy-loading, ejb3...). Sorry I don't have a solution but perhaps some more informations.

If think the exception is thrown by the hascode methode (that is my case). When trying to do blogs.hashCode(); Hibernate will try to fetch the blogs in order to compute their hascode. I think beacause of the lazy loading, this is not going to work...

Do you have any solution since the date you posted your question? If yes, please share your experience...
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top