You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Mathias P.W Nilsson" <ma...@snyltarna.se> on 2008/05/08 22:56:01 UTC
Detached models
I have some questions on detached models for Wicket
Let's say I create a detached model like this
IModel detachedModel = new LoadableDetachableModel(){
protected Object load(){
return ..... spring injected hibernate dao.... lets say Categories
}
};
So now I'll have a detached model.
now I'll use a ListView to populate data from the detachedModel
add( new ListView( "myId" , detachedModel ){
public void populateItem( ListItem item) {
final Category category = (Category) item.getModelObject();
item.add( new Link( "link" ){
public void onClick(){
setResponsePage( new CategoryPage( category ) );
}
} );
}
});
I hope this is right. This is obviously from the top of my head. My question
is.
When In my CategoryPage after a user has clicked the link in the list item
is the Category that I got
from a detached model detached? Or is it a hibernate proxy? If so, how can I
get it detached all the way?
Igor wrote something about a detached DataProvider. Do I need to use
DataProvider to get detached hibernate pojos? I would really want to know
how to do this. Right now, I'm in spagetti code with DAOS that throws
LazyLoading exceptions and Wicket pages that does the same.
--
View this message in context: http://www.nabble.com/Detached-models-tp17136199p17136199.html
Sent from the Wicket - User 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: Detached models
Posted by James Carman <ja...@carmanconsulting.com>.
Why do you need it "detached all the way"?
On Thu, May 8, 2008 at 4:56 PM, Mathias P.W Nilsson
<ma...@snyltarna.se> wrote:
>
> I have some questions on detached models for Wicket
>
> Let's say I create a detached model like this
>
> IModel detachedModel = new LoadableDetachableModel(){
> protected Object load(){
> return ..... spring injected hibernate dao.... lets say Categories
> }
> };
>
> So now I'll have a detached model.
>
> now I'll use a ListView to populate data from the detachedModel
>
> add( new ListView( "myId" , detachedModel ){
> public void populateItem( ListItem item) {
> final Category category = (Category) item.getModelObject();
>
> item.add( new Link( "link" ){
> public void onClick(){
> setResponsePage( new CategoryPage( category ) );
> }
> } );
> }
> });
>
> I hope this is right. This is obviously from the top of my head. My question
> is.
> When In my CategoryPage after a user has clicked the link in the list item
> is the Category that I got
> from a detached model detached? Or is it a hibernate proxy? If so, how can I
> get it detached all the way?
>
>
> Igor wrote something about a detached DataProvider. Do I need to use
> DataProvider to get detached hibernate pojos? I would really want to know
> how to do this. Right now, I'm in spagetti code with DAOS that throws
> LazyLoading exceptions and Wicket pages that does the same.
>
>
> --
> View this message in context: http://www.nabble.com/Detached-models-tp17136199p17136199.html
> Sent from the Wicket - User 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: Detached models
Posted by James Carman <ja...@carmanconsulting.com>.
Well, I have a similar class called DetachableEntityModel (Entity is
an interface we devised to mark something as a persistent entity):
public class DetachableEntityModel<T extends Entity> extends
LoadableDetachableModel<T>
{
private Repository<T> repository;
private String id;
public DetachableEntityModel(Repository<T> repository, String id)
{
this.repository = repository;
this.id = id;
}
public T load()
{
return repository.getById(id);
}
}
This way, my model isn't tied to the ORM implementation at all. I
just code to the Repository interface which can be implemented using
any ORM technology you like (Hibernate, JPA, iBATIS, etc.). I
understand that most folks aren't as "religious" as me about this, but
I have learned that when I go away from my "best practices" my code
becomes more difficult to unit test (I still stray from time to time,
though :).
On Thu, May 8, 2008 at 10:16 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> im not that religious about this. i dont like to pass daos around or
> have to look them up, not when i am using a central persistence tech
> like hibernate/jpa. so i think of my entitymodel as a repository for a
> single object :) you can give it the object or the class and id and it
> knows how to load it. the need to create a loadable detachable model
> for a persistent object is way too common to neglect such a
> convenience. entitymodel encapsulates the lookup so nothing leaks, but
> thats just me :)
>
> -igor
>
>
> On Thu, May 8, 2008 at 5:14 PM, James Carman <ja...@carmanconsulting.com> wrote:
>> I typically don't like to have my UI code directly interact with the
>> ORM implementation (hibernate in this case). For my applications, I
>> pass a Repository (think DAO) object and the object's id into the
>> LoadableDetachableModel.
>>
>>
>>
>> On Thu, May 8, 2008 at 6:33 PM, Igor Vaynberg <ig...@gmail.com> wrote:
>> > you can pass in the id if you want, i prefer to work with objects...so
>> >
>> > class entitymodel extends ldm {
>> > private final String cn;
>> > private final Serialializable id;
>> >
>> > public entitymodel(class cl, serializable id) { cn=cl.getName(); this.id=id; }
>> > public entitymodel(identifiable entity) {
>> > cn=hibernateutils.unproxy(entity).getclass().getname();
>> > id=entity.getid(); }
>> > public object load() { session.get(class.forname(cn), id); }
>> > }
>> >
>> > this is just more convinient, obviously identifiable is an interface
>> > we use here internally.
>> >
>> > -igor
>> >
>> >
>> >
>> >
>> > On Thu, May 8, 2008 at 3:10 PM, Mathias P.W Nilsson
>> > <ma...@snyltarna.se> wrote:
>> > >
>> > > Thanks Igor!
>> > >
>> > > So if I use the sample code you gave me and, when getting the Object from
>> > > the constructor
>> > > on the response page I will have a detached object.
>> > >
>> > > Do I have to load the object from database here? When looking at the Phone
>> > > book example I see you pass id instead of object. Is this more lightweight
>> > > or is it better practice to just pass the id and not the whole object?
>> > >
>> > >
>> > > --
>> > > View this message in context: http://www.nabble.com/Detached-models-tp17136199p17137533.html
>> > >
>> > >
>> > > Sent from the Wicket - User 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
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> 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
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Detached models
Posted by Igor Vaynberg <ig...@gmail.com>.
im not that religious about this. i dont like to pass daos around or
have to look them up, not when i am using a central persistence tech
like hibernate/jpa. so i think of my entitymodel as a repository for a
single object :) you can give it the object or the class and id and it
knows how to load it. the need to create a loadable detachable model
for a persistent object is way too common to neglect such a
convenience. entitymodel encapsulates the lookup so nothing leaks, but
thats just me :)
-igor
On Thu, May 8, 2008 at 5:14 PM, James Carman <ja...@carmanconsulting.com> wrote:
> I typically don't like to have my UI code directly interact with the
> ORM implementation (hibernate in this case). For my applications, I
> pass a Repository (think DAO) object and the object's id into the
> LoadableDetachableModel.
>
>
>
> On Thu, May 8, 2008 at 6:33 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> > you can pass in the id if you want, i prefer to work with objects...so
> >
> > class entitymodel extends ldm {
> > private final String cn;
> > private final Serialializable id;
> >
> > public entitymodel(class cl, serializable id) { cn=cl.getName(); this.id=id; }
> > public entitymodel(identifiable entity) {
> > cn=hibernateutils.unproxy(entity).getclass().getname();
> > id=entity.getid(); }
> > public object load() { session.get(class.forname(cn), id); }
> > }
> >
> > this is just more convinient, obviously identifiable is an interface
> > we use here internally.
> >
> > -igor
> >
> >
> >
> >
> > On Thu, May 8, 2008 at 3:10 PM, Mathias P.W Nilsson
> > <ma...@snyltarna.se> wrote:
> > >
> > > Thanks Igor!
> > >
> > > So if I use the sample code you gave me and, when getting the Object from
> > > the constructor
> > > on the response page I will have a detached object.
> > >
> > > Do I have to load the object from database here? When looking at the Phone
> > > book example I see you pass id instead of object. Is this more lightweight
> > > or is it better practice to just pass the id and not the whole object?
> > >
> > >
> > > --
> > > View this message in context: http://www.nabble.com/Detached-models-tp17136199p17137533.html
> > >
> > >
> > > Sent from the Wicket - User 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
> >
> >
>
> ---------------------------------------------------------------------
> 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: Detached models
Posted by James Carman <ja...@carmanconsulting.com>.
I typically don't like to have my UI code directly interact with the
ORM implementation (hibernate in this case). For my applications, I
pass a Repository (think DAO) object and the object's id into the
LoadableDetachableModel.
On Thu, May 8, 2008 at 6:33 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> you can pass in the id if you want, i prefer to work with objects...so
>
> class entitymodel extends ldm {
> private final String cn;
> private final Serialializable id;
>
> public entitymodel(class cl, serializable id) { cn=cl.getName(); this.id=id; }
> public entitymodel(identifiable entity) {
> cn=hibernateutils.unproxy(entity).getclass().getname();
> id=entity.getid(); }
> public object load() { session.get(class.forname(cn), id); }
> }
>
> this is just more convinient, obviously identifiable is an interface
> we use here internally.
>
> -igor
>
>
>
>
> On Thu, May 8, 2008 at 3:10 PM, Mathias P.W Nilsson
> <ma...@snyltarna.se> wrote:
> >
> > Thanks Igor!
> >
> > So if I use the sample code you gave me and, when getting the Object from
> > the constructor
> > on the response page I will have a detached object.
> >
> > Do I have to load the object from database here? When looking at the Phone
> > book example I see you pass id instead of object. Is this more lightweight
> > or is it better practice to just pass the id and not the whole object?
> >
> >
> > --
> > View this message in context: http://www.nabble.com/Detached-models-tp17136199p17137533.html
> >
> >
> > Sent from the Wicket - User 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
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Detached models
Posted by Igor Vaynberg <ig...@gmail.com>.
you can pass in the id if you want, i prefer to work with objects...so
class entitymodel extends ldm {
private final String cn;
private final Serialializable id;
public entitymodel(class cl, serializable id) { cn=cl.getName(); this.id=id; }
public entitymodel(identifiable entity) {
cn=hibernateutils.unproxy(entity).getclass().getname();
id=entity.getid(); }
public object load() { session.get(class.forname(cn), id); }
}
this is just more convinient, obviously identifiable is an interface
we use here internally.
-igor
On Thu, May 8, 2008 at 3:10 PM, Mathias P.W Nilsson
<ma...@snyltarna.se> wrote:
>
> Thanks Igor!
>
> So if I use the sample code you gave me and, when getting the Object from
> the constructor
> on the response page I will have a detached object.
>
> Do I have to load the object from database here? When looking at the Phone
> book example I see you pass id instead of object. Is this more lightweight
> or is it better practice to just pass the id and not the whole object?
>
>
> --
> View this message in context: http://www.nabble.com/Detached-models-tp17136199p17137533.html
>
>
> Sent from the Wicket - User 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: Detached models
Posted by "Mathias P.W Nilsson" <ma...@snyltarna.se>.
Thanks Igor!
So if I use the sample code you gave me and, when getting the Object from
the constructor
on the response page I will have a detached object.
Do I have to load the object from database here? When looking at the Phone
book example I see you pass id instead of object. Is this more lightweight
or is it better practice to just pass the id and not the whole object?
--
View this message in context: http://www.nabble.com/Detached-models-tp17136199p17137533.html
Sent from the Wicket - User 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: Detached models
Posted by Igor Vaynberg <ig...@gmail.com>.
if you want nicer code then use a dataview with a dataprovider, that
really is geared much better towards database data then listview, and
dataprovider.model() is a great place to wrap the entity with model,
so your code can look like this:
public void populateItem( tem item) {
item.add( new Link( "link" , item.getModel()) {
setResponsePage(new CategoryPage(getModel());}
-igor
On Thu, May 8, 2008 at 2:02 PM, Igor Vaynberg <ig...@gmail.com> wrote:
> On Thu, May 8, 2008 at 1:56 PM, Mathias P.W Nilsson
> <ma...@snyltarna.se> wrote:
> >
> >
>
> > add( new ListView( "myId" , detachedModel ){
> > public void populateItem( ListItem item) {
> > final Category category = (Category) item.getModelObject();
> >
> > item.add( new Link( "link" ){
> > public void onClick(){
> > setResponsePage( new CategoryPage( category ) );
>
> ^^^^^---------- this is bad. now your anonymous link component is
> holding onto a detached category object, so if in categorypage's
> constructor you try to access a lazy collection you will get an
> exception. the proper thing to do here is to:
>
>
> final Category category = (Category) item.getModelObject();
> final IModel cat=new HibernateEntityModel(category);
> item.add(new Link("link", cat){ setResponsePage(new CategoryPage(getModel()); }
>
> the idea is to pass a lookup(a model) rather then the entity itself.
> this way you are never holding onto the entity outside of a hibernate
> session.
>
> -igor
>
>
>
>
> > }
> > } );
> > }
> > });
> >
> > I hope this is right. This is obviously from the top of my head. My question
> > is.
> > When In my CategoryPage after a user has clicked the link in the list item
> > is the Category that I got
> > from a detached model detached? Or is it a hibernate proxy? If so, how can I
> > get it detached all the way?
> >
> >
> > Igor wrote something about a detached DataProvider. Do I need to use
> > DataProvider to get detached hibernate pojos? I would really want to know
> > how to do this. Right now, I'm in spagetti code with DAOS that throws
> > LazyLoading exceptions and Wicket pages that does the same.
> >
> >
> > --
> > View this message in context: http://www.nabble.com/Detached-models-tp17136199p17136199.html
> > Sent from the Wicket - User 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: Detached models
Posted by Igor Vaynberg <ig...@gmail.com>.
On Thu, May 8, 2008 at 1:56 PM, Mathias P.W Nilsson
<ma...@snyltarna.se> wrote:
>
>
> add( new ListView( "myId" , detachedModel ){
> public void populateItem( ListItem item) {
> final Category category = (Category) item.getModelObject();
>
> item.add( new Link( "link" ){
> public void onClick(){
> setResponsePage( new CategoryPage( category ) );
^^^^^---------- this is bad. now your anonymous link component is
holding onto a detached category object, so if in categorypage's
constructor you try to access a lazy collection you will get an
exception. the proper thing to do here is to:
final Category category = (Category) item.getModelObject();
final IModel cat=new HibernateEntityModel(category);
item.add(new Link("link", cat){ setResponsePage(new CategoryPage(getModel()); }
the idea is to pass a lookup(a model) rather then the entity itself.
this way you are never holding onto the entity outside of a hibernate
session.
-igor
> }
> } );
> }
> });
>
> I hope this is right. This is obviously from the top of my head. My question
> is.
> When In my CategoryPage after a user has clicked the link in the list item
> is the Category that I got
> from a detached model detached? Or is it a hibernate proxy? If so, how can I
> get it detached all the way?
>
>
> Igor wrote something about a detached DataProvider. Do I need to use
> DataProvider to get detached hibernate pojos? I would really want to know
> how to do this. Right now, I'm in spagetti code with DAOS that throws
> LazyLoading exceptions and Wicket pages that does the same.
>
>
> --
> View this message in context: http://www.nabble.com/Detached-models-tp17136199p17136199.html
> Sent from the Wicket - User 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