On 2/21/07, Peter Thomas <[EMAIL PROTECTED]> wrote:

Hi,

I use Hibernate and have got a reasonably sized app working with Wicket.
I am aware of the concept of detachable models and want to use it
effectively and I have the following questions.  If the information is
already there on the wiki or something - feel free to point me there.
Apologies in advance for the dumb questions, but I'm sure there is a general
consensus that Wicket models are tricky to understand.


what can be tricky about interface IModel<T> { T getObject(); void
setObject(T t); } ? :)

there is a great wiki page on models that explains all this stuff in detail.

a) If you don't use detachable models, it is ok but the downside is that
when your page is serialized, your models get serialized and this could take
up a lot of memory. Is my understanding correct?


yes. generally you want to keep your session as small as possible. generally
there are no reasons to stick things in there that can be loaded from the
db.

b) In some cases I use new Label("foo", new PropertyModel(fooModel,
"foo"))); and in some cases I use new Label("foo", fooModel.getFoo());
which should I stick to or when should I use which?


if you dont mind the object put in session then you can do foomodel.getfoo()
(like if its something simple like a string)

c) I have a list of objects I got from Hibernate.  Can you provide (or link
to) some simple code that will show me how to "install" this model on the
parent component, and I mean in detachable mode.  In my case it is just a
WebPage.


add(new listview("list", new loadabledetachablemodel() {
  public object load() { return
getsession().createcriteria(user.class).list();
}
}

thats all there is to it. consider this:

public class entitymodel extends loadabledetachablemodel {
 private final Class clazz;
 private final Serializable id;

 public entitymodel(Class clazz, Serializable id) {
        this.clazz=clazz; this.id=id;
 }

 public object load() { return getsession().get(clazz, id); }
}

the above model is a generalized hibernate entity model. you can do thigs
like

setresponsepage(new ViewUserPage(new entitymodel(user.class, 15)));

this model will load the user when getobject() is called, and keep it in
memory during the request. once the request is done and before the page is
saved this model will null the variable that keeps the user so it is not put
in session. only the two key pieces of information necessary to retrieve
this user (and which are hopefully much smaller then the actual user object)
are kept in session.


d) Now I am iterating using ListView.  How exactly do I initialize the
ListView so that it uses the "installed" detachable model?


what do you mean? you just pass it into the constructor.

e) While rendering each row of the ListView, how do I initialize Labels so
that they in turn get the right chunk of the detachable model?


just what you did above  - new PropertyModel(item.,getModel(), "name");
or if you use PropertyListView just add(new Label("name")) :)

f) And suppose I have a Label that has to be derived by say some string
concatenation or formula and OGNL notation will not work.  Will new
Label("foo", fooModel.getFoo() + "bar")kind of hardcoding conflict in some
subtle way with the concept of detachable models?


write a simple model

private static class foomodel extends abstractreadonlymodel {
 private final imodel wrapped;
 public foomodel(imodel wrapped) { this.wrapped=wrapped; }
 public object getobject() { return wrapped.getobject()+"bar"; }
 public void detach() { wrapped.detach(); // important }
}

you do this a lot, so it is very helpful to create a superclass of the above
that helps with setting and detaching. i leave that as an excercise to you.

-igor


Thanks,

Peter.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share
your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to