Re: How to avoid Lazy loading exception

2008-05-05 Thread lars vonk
Did you put the filter-mapping of the opensessioninviewfilter before the
wicketfilter? The order of filter-mapping definitions in web.xml is the
order in which the filters are executed.

- Lars

On Sun, May 4, 2008 at 7:01 PM, Mathias P.W Nilsson [EMAIL PROTECTED]
wrote:


 I have session in view filter but still gets LazyLoadingException even if
 I
 use detached models! Ahhh...

 Solved it temporarily by using Eager fetch but this is going to end up bad
 if everything is eager.
 --
 View this message in context:
 http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17048739.html
 Sent from the Wicket - User mailing list archive at Nabble.com.


 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




Re: How to avoid Lazy loading exception

2008-05-05 Thread Mathias P.W Nilsson

Please do. This would be very helpful.

In my former ( first ) wicket application I used dto's but I would like to
use detached hibernate objects instead for this project.

Your posts has been most helpful.
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17053990.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-05 Thread Mathias P.W Nilsson

Hi!

I will check out the Object that was posted thanks!

Yes I put the opensessioninview filter before.

I think the problem is this.

Lets say I list all of my items in one page. 

Name, Description
TestName, TestDescription

When clicking the name link I do the following 

setResponsePage( new ItemPage( item ) );

I pass the item object to the constructor. The item has references to store
and some other objects.
The store is never loaded in the previous page and to do
item.getStore().getId() is impossible I will get a lazy loading exception.
What is the solution to this.

I have downloaded the wicket phonebook and see that the id is passed to the
constructor.

Do you always load every object that has a reference to an entity? Example
ItemTranslation has reference to Store, Item 

When editing a ItemTranslation I will pass the object to the Wicket page
constructor. The store and item is not loaded since they are lazy.

What is the approach here? load the store and the item and set them in the
ItemTranslation? This sounds very tedious¨. Any other suggestions. The lazy
loading exception occurs when the entity has reference to lazy entities that
is not loaded in the page. 


-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17062281.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-05 Thread James Carman
On Mon, May 5, 2008 at 9:58 AM, Mathias P.W Nilsson
[EMAIL PROTECTED] wrote:

  Hi!

  I will check out the Object that was posted thanks!

  Yes I put the opensessioninview filter before.

  I think the problem is this.

  Lets say I list all of my items in one page.

  Name, Description
  TestName, TestDescription

  When clicking the name link I do the following

  setResponsePage( new ItemPage( item ) );


Try using a bookmarkable page link and just pass in the item's id?

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-05 Thread Igor Vaynberg
where do you get the Item from when you pass it as constructor arg to
the page? and then in constructor of that page you try to access lazy
collections on item?

maybe if you posted some code we could help you better/faster.

-igor

On Mon, May 5, 2008 at 6:58 AM, Mathias P.W Nilsson
[EMAIL PROTECTED] wrote:

  Hi!

  I will check out the Object that was posted thanks!

  Yes I put the opensessioninview filter before.

  I think the problem is this.

  Lets say I list all of my items in one page.

  Name, Description
  TestName, TestDescription

  When clicking the name link I do the following

  setResponsePage( new ItemPage( item ) );

  I pass the item object to the constructor. The item has references to store
  and some other objects.
  The store is never loaded in the previous page and to do
  item.getStore().getId() is impossible I will get a lazy loading exception.
  What is the solution to this.

  I have downloaded the wicket phonebook and see that the id is passed to the
  constructor.

  Do you always load every object that has a reference to an entity? Example
  ItemTranslation has reference to Store, Item

  When editing a ItemTranslation I will pass the object to the Wicket page
  constructor. The store and item is not loaded since they are lazy.

  What is the approach here? load the store and the item and set them in the
  ItemTranslation? This sounds very tedious¨. Any other suggestions. The lazy
  loading exception occurs when the entity has reference to lazy entities that
  is not loaded in the page.


  --
  View this message in context: 
 http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17062281.html


 Sent from the Wicket - User mailing list archive at Nabble.com.


  -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-05 Thread Mathias P.W Nilsson
().toString() ));
}



public void initiateTranslation(){


itemTranslation = getTranslationDao().getTranslation( 
getStore(), getItem(
));

if( itemTranslation == null ){
itemTranslation = new ItemTranslation();
itemTranslation.setItem( getItem() );
itemTranslation.setStore( store );
}

}

public Item getItem() {
return item;
}

public Store getStore() {
return store;
}

public ItemTranslation getItemTranslation() {
return itemTranslation;
}
}


-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17065528.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-05 Thread Igor Vaynberg
;
 protected Object load()
 {
 return getCategoryDao().getElements( 
 getItem().getSubCategory() );
 }
 };


 Form itemStoreForm = new Form( itemStoreForm );
 itemStoreForm.add( availableStoresChoice );
 itemStoreForm.add( unusedStoresChoice );

 Link attributeLink = new Link( attributeLink ){

 @Override
 public void onClick() {
 setResponsePage( new AttributePage( 
 getItem() ) );
 }

 @Override
 public boolean isVisible(){
 ListElement elements = 
 (ListElement )elementModel.getObject();
 if( elements == null || 
 elements.size() == 0 ){
 return false;
 }

 return true;
 }

 };
 itemStoreForm.add( attributeLink );
 add( itemStoreForm );

 }

 }
  }

  And when adding translation to the item I get lazy loading exception
  public class ItemStorePage extends BaseAdministrationPage {

 private Item item;
 private Store store;
 private ItemTranslation itemTranslation;


 public ItemStorePage( Item item, Store store ){
 this.item = item;
 this.store = store;

 initiateTranslation();


 final FeedbackPanel panel = new FeedbackPanel( feedback );
 panel.setOutputMarkupId(true);
 add( panel );



 Form itemForm = new Form( itemForm, new 
 CompoundPropertyModel(
  getItemTranslation()) ){

 private static final long serialVersionUID = 1L;

 @Override
 protected void onSubmit()
 {
 if( getItemTranslation().getId() == null ){
 /**
  * Add Item to stort
  */

 //getItem().addStore( getStore() );

 ListStore stores = new 
 LinkedListStore();
 stores.add( getStore() );
 getItem().setStores( stores );

 getItemDao().save( getItem() );
 }
 getTranslationDao().save(  
 getItemTranslation(), getItem() );
 info( Item updated! );

 }
 };

 /** Adding data for translation */



 TextArea shortDescription = new TextArea( shortDescription 
 );
 TextArea longDescription = new TextArea( longDescription );
 TextField name = new TextField( name );
 shortDescription.add( StringValidator.lengthBetween( 1, 500 
 ));
 shortDescription.setRequired( true );
 longDescription.add( StringValidator.lengthBetween( 1, 1000 
 ));
 longDescription.setRequired( true );
 name.add( StringValidator.lengthBetween( 1, 255 ));
 name.setRequired( true );
 itemForm.add( shortDescription );
 itemForm.add( longDescription );
 itemForm.add( name );
 add( itemForm );

 add( new Label( itemId, getItem().getId().toString() ));
 }



 public void initiateTranslation(){


 itemTranslation = getTranslationDao().getTranslation( 
 getStore(), getItem(
  ));

 if( itemTranslation == null ){
 itemTranslation = new ItemTranslation();
 itemTranslation.setItem( getItem() );
 itemTranslation.setStore( store );
 }

 }

 public Item getItem() {
 return item;
 }

 public Store getStore() {
 return store;
 }

 public ItemTranslation getItemTranslation() {
 return itemTranslation;
 }
  }


  --
  View this message in context: 
 http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17065528.html


 Sent from the Wicket - User mailing list archive at Nabble.com

Re: How to avoid Lazy loading exception

2008-05-05 Thread Mathias P.W Nilsson

Thanks igor! 

I think I have missunderstood the whole thing about detachable models. I
must re-read this.  

Thanks for your time and effort. 
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17067785.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-04 Thread Johan Compagner
for which request does it through there the exception?
first you render it ? then you change it and it throws the exception on the
second request?
does load get called at that point?

On Sun, May 4, 2008 at 12:37 PM, Mathias P.W Nilsson [EMAIL PROTECTED]
wrote:


 Thanks!

 This is what I'm trying to do but I still get the exception.

 This is from my DAO

 @SuppressWarnings( unchecked )
 public ListCollection getCollections(){
   org.hibernate.Session hibernateSession =
 (org.hibernate.Session)getEntityManager().getDelegate();
   return hibernateSession.createCriteria Collection.class

 ,se.edgesoft.hairless.model.criteria.alias.Alias.COLLECTION.getAlias()).list();
 }

 I use the
 org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
 before the wicket filter.
 This is Entity manager and not sessionInView.

 In my base class I spring inject my dao's using @SpringBean annotation.

 Here is the model I'm using

 IModel collectionModel = new LoadableDetachableModel(){
  private static final long serialVersionUID = 1L;
  protected Object load(){
return getCollectionDao().getCollections();
  }
 };


 And it complains in my renderer

 public class CollectionChoiceRenderer extends ChoiceRenderer {
private static final long serialVersionUID = 1L;
public Object getDisplayValue(Object object) {
if (object instanceof Collection) {
Collection  collection = ( Collection ) object;

return collection.getIdentifier();
}
return null;
}
public String getIdValue(Object key, int index) {
if (key instanceof Collection) {
Collection collection = ( Collection ) key;
return collection.getId().toString();
}
return null;
}
 }

 Lazy loading exception when I try collection.getId().toString()

 I thought this was detaching hibernate entities. The Collection class is a
 hibernate annotated pojo.


 --
 View this message in context:
 http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17044892.html
 Sent from the Wicket - User mailing list archive at Nabble.com.


 -
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




Re: How to avoid Lazy loading exception

2008-05-04 Thread Mathias P.W Nilsson

Thanks!

This is what I'm trying to do but I still get the exception.

This is from my DAO

@SuppressWarnings( unchecked )
public ListCollection getCollections(){
   org.hibernate.Session hibernateSession =
(org.hibernate.Session)getEntityManager().getDelegate();
   return hibernateSession.createCriteria Collection.class 
,se.edgesoft.hairless.model.criteria.alias.Alias.COLLECTION.getAlias()).list();
}

I use the org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
before the wicket filter.
This is Entity manager and not sessionInView.

In my base class I spring inject my dao's using @SpringBean annotation.

Here is the model I'm using

IModel collectionModel = new LoadableDetachableModel(){
  private static final long serialVersionUID = 1L;
  protected Object load(){
return getCollectionDao().getCollections();
  }
};


And it complains in my renderer

public class CollectionChoiceRenderer extends ChoiceRenderer {
private static final long serialVersionUID = 1L;
public Object getDisplayValue(Object object) {
if (object instanceof Collection) {
Collection  collection = ( Collection ) object;

return collection.getIdentifier();
}
return null;
}
public String getIdValue(Object key, int index) {
if (key instanceof Collection) {
Collection collection = ( Collection ) key;
return collection.getId().toString();
}
return null;
}
}

Lazy loading exception when I try collection.getId().toString()

I thought this was detaching hibernate entities. The Collection class is a
hibernate annotated pojo.


-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17044892.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-04 Thread Mathias P.W Nilsson

I have a page that list a Items that is hibernated annotated. When clicking
on a wicket link
using setResponsePage( new ItemPage( item ) ); I get the exception when
trying to render my DropDownChoice. 

I use Detached model for this. load is not called, if you mean
hibernatesession.load or entitymanager.load
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17045071.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-04 Thread Stefan Fußenegger

Do you have OpenSessionInViewFilter in your web.xml? If no, you will get this
exception whenever you try to do lazy loading outside your DAOs.

add to web.xml:
  filter
filter-nameOpenSessionFilter/filter-name
filter-class
  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
/filter-class
init-param
  param-namesessionFactoryBeanName/param-name
  param-valuehibernateSessionFactory/param-value
/init-param
  /filter

  filter-mapping
filter-nameOpenSessionFilter/filter-name
url-pattern/*/url-pattern
  /filter-mapping

I always use a self-written class called PersistentObjectModel (a
LoadableDetachableModel) and let all my Persisent classes implement an
interface called IPersistentObject with a single method: Serializable
getId()

public class PersistentObjectModelT extends IPersistentObject extends
LoadableDetachableModel {

private static final long serialVersionUID = 1L;

private final ClassT _clazz;

private final int _id;

@SpringBean(name = myDao)
private IMyDao _myDao;

@SuppressWarnings(unchecked)
public PersistentObjectModel(final T object) {
super(object);

if (object instanceof HibernateProxy) {
_clazz = (ClassT) ((HibernateProxy)
object).getHibernateLazyInitializer()
.getImplementation().getClass();
} else {
_clazz = (ClassT) object.getClass();
}
_id = object.getId();
// inject the bean
InjectorHolder.getInjector().inject(this);
}

public PersistentObjectModel(final ClassT clazz, final int id) {
_clazz = clazz;
_id = id;
// inject the bean
InjectorHolder.getInjector().inject(this);
}

@Override
protected T load() {
return _myDao.getById(_clazz, _id);
}

@SuppressWarnings(unchecked)
@Override
public T getObject() {
return (T) super.getObject();
}

}


This makes working with hibernate objects really easy:

setModel(new PersistentObjectModel(Foo.class, 1)); // option 1: class and id
setModel(new PersistentObjectModel(foo)); // option 2: persistent object

-
---
Stefan Fußenegger
http://talk-on-tech.blogspot.com // looking for a nicer domain ;)
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17045620.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-04 Thread Mathias P.W Nilsson

I have session in view filter but still gets LazyLoadingException even if I
use detached models! Ahhh... 

Solved it temporarily by using Eager fetch but this is going to end up bad
if everything is eager. 
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17048739.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



How to avoid Lazy loading exception

2008-05-03 Thread Mathias P.W Nilsson

Is there any good tutorial on wicket, hibernate and spring? When dealing with
my lazy many to one relations I get lazy loading exception. Very annoing.

Lets say I have an Item class that has many to one relations to a category. 
I list the items in a wicket page and when click a link I do
setResponsePage( new ItemPage( item ) );

when getting to this page I get a lazy loading exception on the category.
How's that? I thought that open session in view should allow this. Is there
any way of dealing with this?
-- 
View this message in context: 
http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17040941.html
Sent from the Wicket - User mailing list archive at Nabble.com.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: How to avoid Lazy loading exception

2008-05-03 Thread Igor Vaynberg
this is not very difficult to fix. there are really only two basic rules:
1) never keep direct references to hibernate entities. this is because
they will become detached between request and unless you call
session.lock/refresh on them before accessing a collection you will
get the lazy exception. instead, use detachable models such as the
loadabledetachablemodel to give components access to entities.

2) install spring's opensessioninview filter that runs around the
wicket filter. by default spring's transactionam support will create a
session per transaction, by the time wicket will render the page the
session will be closed along with the transaction. having the filter
will ensure spring uses session-per-request pattern and reuse the same
session for multiple transactions when possible. since the filter is
around wicket's the session will still be open when wicket renders the
page.

for an example you can see wicket-phonebook app in wicket-stuff's svn.

-igor


On Sat, May 3, 2008 at 4:20 PM, Mathias P.W Nilsson
[EMAIL PROTECTED] wrote:

  Is there any good tutorial on wicket, hibernate and spring? When dealing with
  my lazy many to one relations I get lazy loading exception. Very annoing.

  Lets say I have an Item class that has many to one relations to a category.
  I list the items in a wicket page and when click a link I do
  setResponsePage( new ItemPage( item ) );

  when getting to this page I get a lazy loading exception on the category.
  How's that? I thought that open session in view should allow this. Is there
  any way of dealing with this?
  --
  View this message in context: 
 http://www.nabble.com/How-to-avoid-Lazy-loading-exception-tp17040941p17040941.html
  Sent from the Wicket - User mailing list archive at Nabble.com.


  -
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]



-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]