There are several types of safety: - making sure they are detached? - making sure they are only used inside one thread? - ...
Detaching outside of the wicket component tree's default model slot is not automatic: you have to do that yourself (override ondetach for example). AFAIK we'll be looking into automatic detachment in 1.5 when an IDetachable is somewhere inside the component tree as a field, and drop the default model slot altogether. Thread safety is another issue where you are responsible yourself: if you pass your imodel to another thread (e.g. to generate a report) then it is better to clone or create a new model instance and pass the copy to the thread. Otherwise you'll run into issues where one thread wants to detach, and another wants to attach. Also, the entity instance can only be attached to one EntityManager. You are responsible for that. Martijn On Fri, Feb 20, 2009 at 12:58 PM, Martin Voigt <martin.vo...@artnology.com> wrote: > Hi Martijn, > > your reply reminds me of a question I always wanted to ask: is it safe > to use IModels outside of components with their defined lifecycle and > getDefaultModel() method? We'd like to use models everywhere we have > to retrieve domain objects, be it configuration or, as in this thread, > retrieving session data. And since we don't use transactions directly > from the weblayer but call stateless session beans to do the work and > keep the tx management in another layer/ container (EJB) and use > mostly detached entites, is it safe then to use LDMs directly from the > session object? > > Thanks in advance, > Martin > > 2009/2/20 Martijn Dashorst <martijn.dasho...@gmail.com>: >> move the IModel<Person> to your custom request cycle, otherwise you'll >> run into the issues I've pointed out earlier where on thread detaches >> while another attaches. Storing entities in your session when your >> interested in maintaining them with your entitymanager is BAD, even if >> you put it in a LDM. >> >> Martijn >> >> On Fri, Feb 20, 2009 at 11:05 AM, nino martinez wael >> <nino.martinez.w...@gmail.com> wrote: >>> Hi Tauren >>> >>> I've done something similar.. Have no trouble with.. >>> >>> Disclaimer, below code are really ugly and I need to clean it up... >>> >>> >>> package zeuzgroup.application; >>> >>> import org.apache.wicket.Request; >>> import org.apache.wicket.authentication.AuthenticatedWebSession; >>> import org.apache.wicket.authorization.strategies.role.Roles; >>> import org.apache.wicket.injection.web.InjectorHolder; >>> import org.apache.wicket.spring.injection.annot.SpringBean; >>> >>> import zeuzgroup.application.models.BaseEntityDetachableModel; >>> import zeuzgroup.core.Person; >>> import zeuzgroup.core.provider.IDBDao; >>> import zeuzgroup.core.user.UserType; >>> >>> public class ZeuzSession extends AuthenticatedWebSession { >>> >>> private boolean authorized = false; >>> >>> private BaseEntityDetachableModel<Person> personModel; >>> >>> @SpringBean(name = "dBDao") >>> protected IDBDao dBDao; >>> >>> protected ZeuzSession(Request request) { >>> super(request); >>> InjectorHolder.getInjector().inject(this); >>> >>> } >>> >>> @Override >>> protected void detach() { >>> super.detach(); >>> if (personModel != null) { >>> personModel.detach(); >>> } >>> } >>> >>> public boolean isAuthorized() { >>> return authorized; >>> } >>> >>> public void setAuthorized(boolean authorized) { >>> >>> this.authorized = authorized; >>> if (authorized) { >>> getPerson().setLoggedIn(true); >>> } >>> // Call below too! >>> signIn(getPerson().getAlias(), getPerson().getPassword()); >>> } >>> >>> public Person getPerson() { >>> if (personModel != null) { >>> >>> Person person = (Person) personModel.getObject(); >>> if (person == null) { >>> person = new Person(); >>> person.setUserType(UserType.Guest); >>> >>> } >>> return person; >>> } else { >>> Person person = new Person(); >>> person.setUserType(UserType.Guest); >>> return person; >>> } >>> } >>> >>> public BaseEntityDetachableModel<Person> getPersonModel() { >>> return personModel; >>> } >>> >>> public void setPerson(Person person) { >>> if (personModel != null) { >>> personModel.setBaseEntityDetachableModel(person); >>> } else { >>> personModel = new BaseEntityDetachableModel<Person>(person); >>> } >>> } >>> >>> public void onBeforeDestroy() { >>> getPerson().setLoggedIn(false); >>> } >>> >>> @Override >>> public boolean authenticate(String username, String password) { >>> >>> Person person = new Person(); >>> person.setAlias(username); >>> person.setPassword(password); >>> >>> return dBDao.authorizePerson(person); >>> } >>> >>> @Override >>> public Roles getRoles() { >>> // If the user is signed in, they have these roles >>> // user always are associated with a person >>> return new Roles(getPerson().getUserType().toString()); >>> } >>> } >>> >>> >>> 2009/2/20 Tauren Mills <tau...@tauren.com> >>> >>>> The WIA book and other example apps I've found online often show a >>>> User object being stored in the session: >>>> >>>> class BlogSession extends WebSession { >>>> private User user; >>>> } >>>> >>>> But does it make sense to do this if your User object is loaded from a >>>> persistence layer (Hibernate) and can contain a large tree of >>>> dependent objects? For instance, what if my User object has a >>>> hierarchy like this: >>>> >>>> User >>>> Name >>>> Password >>>> Set<Blog> >>>> Blog >>>> Set<BlogEntry> >>>> BlogEntry >>>> Set<Tag> >>>> Set<Tag> >>>> >>>> Would this store the entire hierarchy of blogs, tags, blog entries, >>>> etc. into the session? I've been experimenting with storing an LDM >>>> of the user in the session instead of the User directly: >>>> >>>> class BlogSession extends WebSession { >>>> private DetachableUserModel userModel; >>>> } >>>> >>>> But I'm getting Hibernate LazyInit errors. So it leaves me wondering >>>> if LDMs in Session aren't automatically loaded since it isn't a >>>> Component. I haven't really dug into what is going on yet. I first >>>> wanted to find out what is common practice for a situation like this. >>>> >>>> Oh, and this isn't my actual User object -- just and example for >>>> illustrative purposes. I'm not building a blog, and the properties >>>> and sets of objects in my User need to be there. >>>> >>>> Thanks! >>>> Tauren >>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org >>>> For additional commands, e-mail: users-h...@wicket.apache.org >>>> >>>> >>> >> >> >> >> -- >> Become a Wicket expert, learn from the best: http://wicketinaction.com >> Apache Wicket 1.3.5 is released >> Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3. >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org >> For additional commands, e-mail: users-h...@wicket.apache.org >> >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org > For additional commands, e-mail: users-h...@wicket.apache.org > > -- Become a Wicket expert, learn from the best: http://wicketinaction.com Apache Wicket 1.3.5 is released Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3. --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org