you are not using a detachable model for your items
List<se.edgesoft.hairless.entities.item.Item> items = new
ArrayList<se.edgesoft.hairless.entities.item.Item>();
you are keeping them in that ^ list across requests, which detaches
them from the session, and then you are passing one of those to the
page.
instead of using listdataprovider you should use a dataprovider in
that uses detachablemodels for each individual item
-igor
On Mon, May 5, 2008 at 9:30 AM, Mathias P.W Nilsson
<[EMAIL PROTECTED]> wrote:
>
> Yes, sorry. I have tried removing unessesary code. The dao objects is from
> the base class that is sping injected via @SpringBean
>
>
> This is the class that lists the items. When clicking on a link that edits
> or adds an item see ItemPage
>
>
> public class ItemListPage extends BaseAdministrationPage{
>
> private List<ItemFilter> filters = new LinkedList<ItemFilter>();
> private AggregatedItem aggregatedItem;
> private WebMarkupContainer itemContainer;
> List<se.edgesoft.hairless.entities.item.Item> items = new
> ArrayList<se.edgesoft.hairless.entities.item.Item>();
>
> public List<ItemFilter> getFilters() {
> return filters;
> }
>
> public ItemListPage(){
>
> IModel categoryModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
> protected Object load()
> {
> List<Category> categories = new
> LinkedList<Category>();
> List<MainCategory> mainCategories =
> getCategoryDao().getMainCategories();
> for( MainCategory main : mainCategories ){
> categories.add( main );
> List<SubCategory> subCategories =
> getCategoryDao().getSubCategories(
> main );
> for( SubCategory sub : subCategories
> ){
> categories.add( sub );
> }
> }
>
> return categories;
> }
> };
> final DropDownChoice categoryChoice = new
> DropDownChoice("categories", new
> Model(), categoryModel, new CategoryChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"0\">"+
> getLocalizer().getString("category.choice.default", ItemListPage.this ) +
> "</option>";
> }
>
> protected boolean
> wantOnSelectionChangedNotifications() {
> return true;
> }
>
> protected void onSelectionChanged(final Object obj )
> {
> if( obj instanceof Category ){
> addFilter( new CategoryFilter(
> (Category) obj ));
> }else{
> removeFilter( CategoryFilter.class );
> }
>
> refreshItemView();
> }
> };
> categoryChoice.setEscapeModelStrings(false);
>
> IModel brandsModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
> protected Object load()
> {
> return getBrandDao().getBrands();
> }
> };
> final DropDownChoice brandChoice = new
> DropDownChoice("brands", new
> Model(), brandsModel,new BrandChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"0\">"+
> getLocalizer().getString("brand.choice.default", ItemListPage.this ) +
> "</option>";
> }
>
> protected boolean
> wantOnSelectionChangedNotifications() {
> return true;
> }
>
> protected void onSelectionChanged(final Object obj )
> {
> if( obj instanceof Brand ){
> addFilter( new BrandFilter( (Brand)
> obj ));
> }else{
> removeFilter( BrandFilter.class );
> }
>
> refreshItemView();
> }
> };
>
>
> Form itemForm = new Form( "itemForm" );
> itemForm.add( brandChoice );
> itemForm.add( categoryChoice );
> add( itemForm );
>
> itemContainer = new WebMarkupContainer( "itemContainer" ){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> public boolean isVisible(){
>
> if( getItems() == null ) return false;
>
> if( getItems().isEmpty() ) return false;
>
> return true;
> }
> };
>
> add( itemContainer );
> refreshItemView();
>
> add( new Link( "addItem" ){
>
> private static final long serialVersionUID =
> 1L;
>
> @Override
> public void onClick() {
> setResponsePage( new ItemPage() );
> }
> });
> }
>
> private void updateItems(){
> aggregatedItem = getItemDao().getAggregatedItem(getFilters());
> items = getItemDao().getItems( getFilters() );
> }
>
> public List<se.edgesoft.hairless.entities.item.Item> getItems(){
> return items;
> }
>
> private void refreshItemView(){
> updateItems();
> itemContainer.addOrReplace( new ItemFragment( "itemPanel" ,
> "itemView") );
> }
>
> private final class ItemFragment extends Fragment
> {
> private static final long serialVersionUID = 0L;
>
> public ItemFragment( final String panel, final String id)
> {
> super(panel, id, ItemListPage.this );
> final DataView view = new DataView( "itemDataView",
> new
> ListDataProvider( getItems() ) ) {
>
> private static final long serialVersionUID =
> 1L;
>
> public void populateItem(final Item item) {
> final
> se.edgesoft.hairless.entities.item.Item i =
> (se.edgesoft.hairless.entities.item.Item) item.getModelObject();
> Link itemLink = new Link(
> "itemLink" ){
>
> private static final
> long serialVersionUID = 1L;
>
> @Override
> public void onClick()
> {
>
> setResponsePage( new ItemPage( i ) );
>
> }
>
> };
> item.add( new Label( "id" ,
> i.getId().toString() ) );
> itemLink.add( new Label(
> "identifier" , i.getIdentifier() ) );
> item.add( itemLink );
> item.add( new Label(
> "cachedBalance" , i.getCachedBalance().toString()
> ) );
> item.add( new Label( "value"
> , "" + i.getValue() ) );
> item.add( new Label( "price"
> , "" + i.getPrice() ) );
> item.add( new Label(
> "memberPrice" , "" + i.getMemberPrice() ) );
> }
> };
> view.setItemsPerPage(10);
> add(view);
> add(new PagingNavigator("navigator", view));
>
> }
> }
>
> @SuppressWarnings( "unchecked" )
> public void removeFilter( Class clazz ){
> Iterator<ItemFilter> iter = getFilters().iterator();
> while( iter.hasNext() ){
> ItemFilter filter = (ItemFilter)iter.next();
>
> if( clazz.equals( filter.getClass() )){
> iter.remove();
> break;
> }
> }
> }
>
> public void removeFilter( ItemFilter f ){
> Iterator<ItemFilter> iter = getFilters().iterator();
> while( iter.hasNext() ){
> ItemFilter filter = (ItemFilter)iter.next();
>
> if( f.getClass().equals( filter.getClass() )){
> iter.remove();
> break;
> }
> }
> }
>
> public void addFilter( ItemFilter f ){
> removeFilter( f );
> getFilters().add( f );
> }
>
>
> public AggregatedItem getAggregatedItem() {
> return aggregatedItem;
> }
> }
>
>
> ItemPage
>
> public class ItemPage extends BaseAdministrationPage {
>
> private Form itemForm;
> private Item item;
> public ItemPage(){
> this( new Item() );
> }
>
> public ItemPage( Item item ){
>
> this.item = item;
>
> IModel categoryDetachedFromCompound;
>
> if( item.getId() == null ){
> item.setEarliestDeliveryDate( new Date() );
> categoryDetachedFromCompound = new Model();
> }else{
> categoryDetachedFromCompound = new Model(
> getItem().getSubCategory() );
> }
>
> final CSSFeedbackPanel panel = new CSSFeedbackPanel(
> "feedback" );
> panel.setOutputMarkupId(true);
> add( panel );
>
> IModel collectionModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
>
> protected Object load(){
> return getCollectionDao().getCollections();
> }
> };
>
> IModel colorStoryModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
> protected Object load(){
> if( getItem().getCollection() != null &&
> getItem().getBrand() != null ){
> return
> getColorStoryDao().getColorStories( getItem().getCollection() ,
> getItem().getBrand());
> }else{
> return Collections.EMPTY_LIST;
> }
> }
> };
>
> IModel brandsModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
> protected Object load(){
> return getBrandDao().getBrands();
> }
> };
>
> IModel categoryModel = new LoadableDetachableModel(){
>
> private static final long serialVersionUID = 1L;
> protected Object load(){
> List<Category> categories = new
> LinkedList<Category>();
> List<MainCategory> mainCategories =
> getCategoryDao().getMainCategories();
> for( MainCategory main : mainCategories ){
> categories.add( main );
> List<SubCategory> subCategories =
> getCategoryDao().getSubCategories(
> main );
> for( SubCategory sub : subCategories
> ){
> categories.add( sub );
> }
> }
> return categories;
> }
> };
>
> final DropDownChoice colorStoryChoice = new
> DropDownChoice("colorStory",
> colorStoryModel, new ColorStoryChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"\">"+
> getLocalizer().getString("colorstory.choice.default", ItemPage.this ) +
> "</option>";
> }
> };
> colorStoryChoice.setNullValid( true );
> colorStoryChoice.setOutputMarkupId( true );
>
> final DropDownChoice collectionChoice = new
> DropDownChoice("collection",
> collectionModel, new CollectionChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"\">"+
> getLocalizer().getString("collection.choice.default", ItemPage.this ) +
> "</option>";
> }
> };
> collectionChoice.add(new
> AjaxFormComponentUpdatingBehavior("onchange"){
>
> private static final long serialVersionUID = 1L;
>
> protected void onUpdate(AjaxRequestTarget target){
> if( getItem().getCollection() != null && getItem().getBrand()
> != null ){
> target.addComponent(colorStoryChoice);
>
> }
> }
> });
> collectionChoice.setRequired( true );
>
> final DropDownChoice categoryChoice = new
> DropDownChoice("subCategory",
> categoryDetachedFromCompound, categoryModel, new CategoryChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"0\">"+
> getLocalizer().getString("category.choice.default", ItemPage.this ) +
> "</option>";
> }
> };
> categoryChoice.add(new
> AjaxFormComponentUpdatingBehavior("onchange"){
>
> private static final long serialVersionUID = 1L;
>
> protected void onUpdate(AjaxRequestTarget target){
> target.addComponent(panel);
> if( ! ( categoryChoice.getModelObject()
> instanceof SubCategory ) ){
> info( "Please choose a subcategory" );
> }
> }
> });
> categoryChoice.setRequired( true );
> categoryChoice.setEscapeModelStrings(false);
>
> final DropDownChoice brandChoice = new DropDownChoice("brand",
> brandsModel,new BrandChoiceRenderer()){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object selected){
> return "<option value=\"0\">"+
> getLocalizer().getString("brand.choice.default", ItemPage.this ) +
> "</option>";
> }
>
> };
> brandChoice.add(new
> AjaxFormComponentUpdatingBehavior("onchange"){
>
> private static final long serialVersionUID = 1L;
>
> protected void onUpdate(AjaxRequestTarget target){
> if( getItem().getCollection() != null && getItem().getBrand()
> != null ){
> target.addComponent(colorStoryChoice);
> }
> }
> });
> brandChoice.setRequired( true );
>
> TextField identifier = new TextField( "identifier" );
> identifier.add( StringValidator.lengthBetween( 1, 255));
> identifier.setRequired( true );
>
> TextField earliestDeliveryDate = new TextField(
> "earliestDeliveryDate" );
> earliestDeliveryDate.setRequired( true );
>
> TextField discount = new TextField( "discount" );
> discount.setRequired( true );
> discount.add( NumberValidator.range( 0 , 100 ));
>
> TextField memberPrice = new TextField( "memberPrice" );
> memberPrice.setRequired( true );
>
> TextField price = new TextField( "price" );
> price.setRequired( true );
>
> TextField value = new TextField( "value" );
> value.setRequired( true );
>
> CompoundPropertyModel itemModel = new CompoundPropertyModel(
> item );
>
> itemForm = new Form( "itemForm", itemModel){
>
> private static final long serialVersionUID = 1L;
>
> @Override
> protected void onSubmit()
> {
>
>
> Item item = (Item) itemForm.getModelObject();
>
> if( ! ( categoryChoice.getModelObject()
> instanceof SubCategory ) ){
> info( "Please choose a subcategory" );
> return;
> }
>
> item.setSubCategory( (SubCategory)
> categoryChoice.getModelObject() );
>
> if( ! item.hasGender() ){
> info( "You must choose at least one
> gender" );
> return;
> }
>
> if( item.getId() == null ){
> item.setAddedDate( new Date() );
> item.setCachedBalance( new Long(0));
> }
> getItemDao().save( item );
> info( "Item saved!" );
> getPage().addOrReplace( new
> ItemFragment("itemSelectionPanel",
> "itemFragment"));
> }
> };
>
> itemForm.add( colorStoryChoice );
> itemForm.add( collectionChoice );
> itemForm.add( brandChoice );
> itemForm.add( categoryChoice );
> itemForm.add( memberPrice );
> itemForm.add( price );
> itemForm.add( value );
>
> itemForm.add( new CheckBox( "female" ) );
> itemForm.add( new CheckBox( "male" ) );
> itemForm.add( new CheckBox( "junior" ) );
> itemForm.add( identifier );
> itemForm.add( earliestDeliveryDate );
> itemForm.add( discount );
> add( itemForm );
>
> if( getItem().getId() != null ){
> add( new ItemFragment("itemSelectionPanel",
> "itemFragment"));
> }else{
> add( new Label("itemSelectionPanel"));
> }
> }
>
> public Item getItem() {
> return item;
> }
>
> /**
> * Fragment for adding , editing translation for a
> * store.
> * The item must exists before this fragment is visible.
> * @author Mathias Nilsson
> *
> */
> private final class ItemFragment extends Fragment
>
> {
> private static final long serialVersionUID = 1L;
> public ItemFragment( String panel, String id ){
> super(panel, id, ItemPage.this );
>
> IModel availableStoresModel = new
> LoadableDetachableModel(){
>
> private static final long serialVersionUID =
> 1L;
> protected Object load()
> {
> return
> getStoreDao().getAvailableStores( getItem() );
> }
> };
>
> IModel unusedStoresModel = new
> LoadableDetachableModel(){
>
> private static final long serialVersionUID =
> 1L;
> protected Object load()
> {
> return getStoreDao().getUnusedStores(
> getItem() );
> }
> };
> final DropDownChoice availableStoresChoice = new
> DropDownChoice(
> "availableStores", new Model( "" ),availableStoresModel, new
> StoreChoiceRenderer()){
>
> private static final long serialVersionUID =
> 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object
> selected){
> return "<option value=\"\">--- Edit
> item for store ---</option>";
> }
> protected boolean
> wantOnSelectionChangedNotifications() {
>
> return true;
> }
>
> protected void onSelectionChanged(final
> Object obj ) {
> if( obj instanceof Store ){
> Store store = (Store) obj;
> setResponsePage( new
> ItemStorePage( getItem(), store ) );
> }
> }
>
> @Override
> public boolean isVisible(){
> if( getChoices().size() > 0 ){
> return true;
> }
> return false;
> }
> };
> final DropDownChoice unusedStoresChoice = new
> DropDownChoice(
> "unusedStores", new Model( "" ),unusedStoresModel, new
> StoreChoiceRenderer()){
>
> private static final long serialVersionUID =
> 1L;
>
> @Override
> protected java.lang.CharSequence
> getDefaultChoice(final Object
> selected){
> return "<option value=\"\">--- New
> item for store ---</option>";
> }
> protected boolean
> wantOnSelectionChangedNotifications() {
>
> return true;
> }
>
> protected void onSelectionChanged(final
> Object obj ) {
> if( obj instanceof Store ){
> Store store = (Store) obj;
> setResponsePage( new
> ItemStorePage( getItem(), store ) );
> }
> }
>
> @Override
> public boolean isVisible(){
> if( getChoices().size() > 0 ){
> return true;
> }
> return false;
> }
>
> };
>
>
> final IModel elementModel = new
> LoadableDetachableModel(){
>
> private static final long serialVersionUID =
> 1L;
> 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(){
> List<Element> elements =
> (List<Element> )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() );
>
> List<Store> stores = new
> LinkedList<Store>();
> 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.
>
>
> ---------------------------------------------------------------------
> 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]