[jboss-user] [EJB 3.0] - Re: Detach entities - Obtaining a clear pojo
ALRubinger
do-not-reply at jboss.com
Mon Sep 4 12:14:46 EDT 2006
Not sure if I'm necessarily understanding the issue but...
Most times we encounter a LazyInitilizationException when trying to iterate over relationships outside the scope of the session/transaction; when sent to a remote client, for example.
In these cases I like to call on a little utility method to ensure the association's objects are stored in a Collection of type I dictate:
| class PersistenceUtils
|
| /**
| * Returns a Collection of all objects in the specified persistentCollection
| * without binding to any persistence context or session.
| *
| * @param <T>
| * @param targetCollection
| * @param persistentCollection
| * @return
| */
| public static <T> Collection<T> getCollectionItemsRemovedFromPersistenceContext(
| Collection<T> targetCollection, Collection<T> persistentCollection) {
| // If runtime type of persistentCollection is not PersistentCollection,
| // take no action
| if (!(persistentCollection instanceof PersistentCollection))
| return persistentCollection;
|
| // Clear existing target
| targetCollection.clear();
|
| // Place all items in persistent collection into target
| for (T item : persistentCollection) {
| targetCollection.add(item);
| }
|
| // Return target
| return targetCollection;
| }
...and I can easily call it by:
Collection<MyType> associations = new ArrayList<MyType>();
|
| associations = PersistenceUtils
| .getCollectionItemsRemovedFromPersistenceContext(
| associations,
| myAttachedObject.getAttachedAssociations());
|
...also, while responding here, found this post with a similar solution:
http://www.mojavelinux.com/blog/archives/2006/06/hibernate_get_out_of_my_pojo/
S,
ALR
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3969262#3969262
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3969262
More information about the jboss-user
mailing list