You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by lucast <lu...@hotmail.com> on 2011/04/13 14:16:15 UTC

Refreshing loadable detachable model object inside an ajax call

Hi there,
I have a problem reloading/refreshing Parent object P from inside an Ajax
call.

The conditions are as follow:

I have Parent object P and child object C. 
Child object has a unique time-stamp constraint.
Child objects can be created or updated from two different pages, page A
and/or page B.


In order to avoid unique constraint exception, I want to check if child with
time-stamp T exists for parent P. It is my intention, therefore to refresh
parent P in order to check.

On the main panel I am using loadable detachable model and I am passing that
loadable detachable model object to the panel where I want to do the
checking.


When I call (Parent) model.getObject(); I get the following exception:

ERROR - RequestCycle               - a different object with the same
identifier value was already associated with the session:
[com.myProject.dbEntities.domain.Parent#1364]

org.hibernate.NonUniqueObjectException: a different object with the same
identifier value was already associated with the session:
[com.myProject.dbEntities.domain.Parent#1364]


The way I am implementing the load function for the Loadable Detachable
Model is just the standard: parentService().load(Parent.class, id);

How can I refresh the Loadable Detachable Model object without getting the
above exception and without having to refresh the entire page?


Any help will be greatly appreciated.


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3446979.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
I have finally solved this issue.
In my original scenario, I was implementing an anonymous class and inside
that anonymous class I was calling parentService().load(Parent.class, id);.
To solve this, created an inner class to replace the anonymous class and
when initialising the inner class objects, I passed the containing panel's
model (which is a loadableDetachableModel object).
I simply called the (Parent) model.getObject();  on the inner class function
that responds to ajax calls in order to have a fresh parent object from the
db.

That solved all the problems mentioned in my original post.


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3509490.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by Jeremy Thomerson <je...@wickettraining.com>.
On Thu, Apr 14, 2011 at 3:51 PM, Iain Reddick
<ia...@beatsystems.com>wrote:

> Firstly, that LDM code is broken - calling detach() on load() makes no
> sense.
>

Right - you should *not* call detach from within load().


> Also, it will hit hibernate on every call to getObject(), as you aren't
> caching the loaded Parent entity. This is probably the cause of the
> hibernate exceptions you are seeing.
>

Wrong - LDM caches the returned object within it.  That's the whole point of
LDM.


> Check out http://wicketinaction.com/2008/09/building-a-smart-entitymodel/for a solid persistence backed LDM implementation.
>
> Unrelated, your ParentsService.load() method probably doesn't need to take
> the entity class a parameter - the service should know the entity type it is
> querying hibernate for.
>

-- 
Jeremy Thomerson
http://wickettraining.com
*Need a CMS for Wicket?  Use Brix! http://brixcms.org*

Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
Thank you, Iain.
I shall try that.
Cheers,
Lucas

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3450740.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by Clint Checketts <ch...@gmail.com>.
Your issue with the generics is you need to tell it that 'T extends
Indetifiable', or take it out entirely if you are going to explicitly tell
it that you are using the Parent class.

Also feel free to make the argument match what your load method requires.
The article could be referencing code that is old or whatever. Also keep in
mind that the class Long implements Serializable, but the primitive long
(lower case 'l') isn't an object and doesn't have a type inheritance (even
though it will serialize, but that's a disfferent topic).


-Clint



public class EntityModel<T *extends Identifiable*> extends
AbstractEntityModel<T> {
       public EntityModel(Class<? extends T> clazz, Serializable id) {
               super(clazz, id);
       }
       @Override
       protected T load(Class clazz, Serializable id) {
               return WicketApplication.get().get_
service().load(clazz, id);
       }
}


On Mon, Apr 18, 2011 at 6:16 AM, lucast <lu...@hotmail.com> wrote:

> Hi jcgarciam,
> I have made sure that my original class Parent does implement class
> Identifiable<Serializable> and I have now modified EntityModel<T> to public
> class EntityModel<T> extends AbstractEntityModel<Parent> {. That got rid
> the
> error message.  So that's great. Thanks!
> I will try to have it running later on during the day.
>
> Why does AbstractEntityModel class on
> http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
> http://wicketinaction.com/2008/09/building-a-smart-entitymodel/  uses
> Serializable id, when later on,  under "Using EntityModel to bind to Forms"
> it is passing a value of type Long to initialise the EntityModel class?
> Based on the example of the EntityModel.load() function, should I change my
> WicketApplication.get().get_service().load(clazz, id) to accept
> Serialisable
> instead of Long?
> Maybe I'm missing something basic here. But your suggestion helped to get
> past the initial hurdle.
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3457227.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
Hi jcgarciam, 
I have made sure that my original class Parent does implement class
Identifiable<Serializable> and I have now modified EntityModel<T> to public
class EntityModel<T> extends AbstractEntityModel<Parent> {. That got rid the
error message.  So that's great. Thanks!
I will try to have it running later on during the day. 

Why does AbstractEntityModel class on 
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/  uses 
Serializable id, when later on,  under "Using EntityModel to bind to Forms" 
it is passing a value of type Long to initialise the EntityModel class? 
Based on the example of the EntityModel.load() function, should I change my
WicketApplication.get().get_service().load(clazz, id) to accept Serialisable
instead of Long? 
Maybe I'm missing something basic here. But your suggestion helped to get
past the initial hurdle.


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3457227.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by jcgarciam <jc...@gmail.com>.
Does your Person class implements the 'Identifiable' interface?

On Mon, Apr 18, 2011 at 4:27 AM, lucast [via Apache Wicket] <
ml-node+3456789-549093517-65838@n4.nabble.com> wrote:

> Hi Clint,
> Here is the example of EntityModel as taken from
> http://wicketinaction.com/2008/09/building-a-smart-entitymodel/.
> I've added the constructor that calls super().
> public class EntityModel<T> extends AbstractEntityModel<T> {
>         public EntityModel(Class<? extends T> clazz, Serializable id) {
>                 super(clazz, id);
>         }
>         @Override
>         protected T load(Class clazz, Serializable id) {
>                 return WicketApplication.get().get_service().load(clazz,
> id);
>         }
> }
> Now my I have two errors from my IDE (Eclipse), the first one on *extends
> AbstractEntityModel<T> {*. On type T, it says: Bound mismatch: The type T
> is not a valid substitute for the bounded parameter <T extends
> Identifiable<?>> of the type AbstractEntityModel<T>.
> Would you know what is going on here? I am following the exact example.
>
> The second error my IDE is complaining about is on *return
> WicketApplication.get().get_service().load(clazz, id);*: The method
> load(Class<T>, long) in the type Service is not applicable for the arguments
> (Class, Serializable).
> That's fair enough since all the objects ID i'm persisting to DB are of
> type Long.
> But why is the example using type Serializable instead of Long?
> If you look at the section "Using EntityModel to bind to Forms", from the
> link above you will see that the values passed are class and Long:
>  public EditPersonPage(Long personId)
>     {
>         this(new EntityModel<Person>(Person.class, personId));
>     }
>
> I fear I'm missing a very small detail that's stopping me from having this
> working.
> Thanks, Lucas
>
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3456789.html
>  To start a new topic under Apache Wicket, email
> ml-node+1842946-398011874-65838@n4.nabble.com
> To unsubscribe from Apache Wicket, click here<http://apache-wicket.1842946.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=1842946&code=amNnYXJjaWFtQGdtYWlsLmNvbXwxODQyOTQ2fDEyNTYxMzc3ODY=>.
>
>



-- 

JC


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3457165.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
Hi Clint, 
Here is the example of EntityModel as taken from 
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/ .
I've added the constructor that calls super().
public class EntityModel<T> extends AbstractEntityModel<T> {
	public EntityModel(Class<? extends T> clazz, Serializable id) {
		super(clazz, id);		
	}
	@Override
	protected T load(Class clazz, Serializable id) {
		return WicketApplication.get().get_service().load(clazz, id);
	}
}
Now my I have two errors from my IDE (Eclipse), the first one on extends
AbstractEntityModel<T> {. On type T, it says: Bound mismatch: The type T is
not a valid substitute for the bounded parameter <T extends Identifiable<?>>
of the type AbstractEntityModel<T>.
Would you know what is going on here? I am following the exact example.

The second error my IDE is complaining about is on return
WicketApplication.get().get_service().load(clazz, id);: The method
load(Class<T>, long) in the type Service is not applicable for the arguments
(Class, Serializable).
That's fair enough since all the objects ID i'm persisting to DB are of type
Long. 
But why is the example using type Serializable instead of Long? 
If you look at the section "Using EntityModel to bind to Forms", from the
link above you will see that the values passed are class and Long:
 public EditPersonPage(Long personId)
    {
        this(new EntityModel<Person>(Person.class, personId));
    }

I fear I'm missing a very small detail that's stopping me from having this
working.
Thanks, Lucas



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3456789.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by Clint Checketts <ch...@gmail.com>.
The 'implicit super constructor' warning means that in your constructor you
need to call 'super()'

The 'complains on type T' part I'd need a little more info. Feel free to
post the exact lines of code.

-Clint

On Sat, Apr 16, 2011 at 2:21 AM, lucast <lu...@hotmail.com> wrote:

> However, when I try to extend the AbstractEntityModel as shown on
> EntityModel<T> extends AbstractEntityModel<T> example, my IDE starts to
> complaint with things like "Implicit super constructor
> AbstractEntityModel<T>() is undefined for default constructor. Must define
> an explicit constructor".
> as for the extends AbstractEntityModel<T> it also complaints on type T.
>
>

Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
Hi Iain, 
Thank you for your reply.
I have tried to follow the examples from 
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/
http://wicketinaction.com/2008/09/building-a-smart-entitymodel/ .
I have successfully implemented both the Identifiable interface from article
and also, the last example of AbstractEntityModel<T extends Identifiable< ?
>> implements IModel<T>.
However, when I try to extend the AbstractEntityModel as shown on
EntityModel<T> extends AbstractEntityModel<T> example, my IDE starts to
complaint with things like "Implicit super constructor
AbstractEntityModel<T>() is undefined for default constructor. Must define
an explicit constructor". 
as for the extends AbstractEntityModel<T> it also complaints on type T.

I forgot that there are also good examples on 
https://cwiki.apache.org/WICKET/working-with-wicket-models.html
https://cwiki.apache.org/WICKET/working-with-wicket-models.html , in
particular on the Read/Write Detachable Model that I need to give it a
proper try since at the time of implementing my solution,
LoadableDetachableModel was good enough.

As soon as I have figured it out, I will post my findings. Since it is not a
show stopper, I shall park it for this week and come back to it in a week's
time.
Thank you, Iain and @Clint for your posts. They are much appreciated.
Kind regards,
Lucas


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3453635.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by Iain Reddick <ia...@beatsystems.com>.
Firstly, that LDM code is broken - calling detach() on load() makes no sense.

Also, it will hit hibernate on every call to getObject(), as you aren't caching the loaded Parent entity. This is probably the cause of the hibernate exceptions you are seeing.

Check out http://wicketinaction.com/2008/09/building-a-smart-entitymodel/ for a solid persistence backed LDM implementation.

Unrelated, your ParentsService.load() method probably doesn't need to take the entity class a parameter - the service should know the entity type it is querying hibernate for.

----- Original Message -----
From: "lucast" <lu...@hotmail.com>
To: users@wicket.apache.org
Sent: Wednesday, 13 April, 2011 7:20:15 PM
Subject: Re: Refreshing loadable detachable model object inside an ajax call

Hi Clint, thanks for your email.

After reading your post, this is how I've implemented the LDM class:


public class LoadableParentModel extends LoadableDetachableModel {

        Long id;

	public LoadableParentModel(Long id){

		this.id = id;

	}

	@Override	protected Parent load() {

		Parent Parent =
WicketApplication.get().getParents_service().load(Parent.class, id);

		detach();

		return Parent;

	}

}




Is that what you mean by explicitly calling .detach() on the LDM?

I apologise if I didn't get that right straight away. I have implemented it
as in the above example but when I call (Parent) model.getObject(); I get
the same exception as before.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3447904.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by lucast <lu...@hotmail.com>.
Hi Clint, thanks for your email.

After reading your post, this is how I've implemented the LDM class:


public class LoadableParentModel extends LoadableDetachableModel {

        Long id;

	public LoadableParentModel(Long id){

		this.id = id;

	}

	@Override	protected Parent load() {

		Parent Parent =
WicketApplication.get().getParents_service().load(Parent.class, id);

		detach();

		return Parent;

	}

}




Is that what you mean by explicitly calling .detach() on the LDM?

I apologise if I didn't get that right straight away. I have implemented it
as in the above example but when I call (Parent) model.getObject(); I get
the same exception as before.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3447904.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Refreshing loadable detachable model object inside an ajax call

Posted by Clint Checketts <ch...@gmail.com>.
Have you tried explicitly calling .detach() on the LDM?

The net getObject() call should force the load() method to get called again.

-Clint

On Wed, Apr 13, 2011 at 7:16 AM, lucast <lu...@hotmail.com> wrote:

> Hi there,
> I have a problem reloading/refreshing Parent object P from inside an Ajax
> call.
>
> The conditions are as follow:
>
> I have Parent object P and child object C.
> Child object has a unique time-stamp constraint.
> Child objects can be created or updated from two different pages, page A
> and/or page B.
>
>
> In order to avoid unique constraint exception, I want to check if child
> with
> time-stamp T exists for parent P. It is my intention, therefore to refresh
> parent P in order to check.
>
> On the main panel I am using loadable detachable model and I am passing
> that
> loadable detachable model object to the panel where I want to do the
> checking.
>
>
> When I call (Parent) model.getObject(); I get the following exception:
>
> ERROR - RequestCycle               - a different object with the same
> identifier value was already associated with the session:
> [com.myProject.dbEntities.domain.Parent#1364]
>
> org.hibernate.NonUniqueObjectException: a different object with the same
> identifier value was already associated with the session:
> [com.myProject.dbEntities.domain.Parent#1364]
>
>
> The way I am implementing the load function for the Loadable Detachable
> Model is just the standard: parentService().load(Parent.class, id);
>
> How can I refresh the Loadable Detachable Model object without getting the
> above exception and without having to refresh the entire page?
>
>
> Any help will be greatly appreciated.
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Refreshing-loadable-detachable-model-object-inside-an-ajax-call-tp3446979p3446979.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>