Basically, while it is great that you can work directly with hibernate
objects in Wicket components, there are a couple of dangers to it as
well. Especially when you use with a filter that opens and closes a
transaction on every (Wicket) request and/or you have implicit
transactions turned on, you can be in for nasty surprises (like this
one). I'm not in favor of such an approach personally, though I can
understand people who use it because it saves time and make for a very
easy programming model. Alternatively, you can make sure that you put
any hibernate access behind dao/ service interfaces and limit
transactions to those interfaces. Basically you can use a layer of
services which uses a layer of daos and use the services to group
access to daos and define the transaction on (e.g. using Spring's
@Transaction annotation). Going one step further, you might even
consider not using hibernate objects outside those service methods at
all, but instead use value objects. I don't believe that's necessary,
but if you do that, you'll never ever will be in trouble.

My 2c,

Eelco


On 2/18/07, Ryan Holmes <[EMAIL PROTECTED]> wrote:
> I should clarify that last paragraph: I don't know Wicket well enough
> to be sure you haven't found a bug and of course I don't know your
> code. I've just run into this often enough in Hibernate to make an
> educated guess. Let us know how it works out.
>
> -Ryan
>
> On Feb 18, 2007, at 2:08 AM, Ryan Holmes wrote:
>
> > org.hibernate.NonUniqueObjectException is a really common problem. My
> > guess is that the DataStoreUser is being loaded once to pass it into
> > the page constructor and again by the LoadableDetachableModel within
> > the same Hibernate session. Same persistent object loaded into two
> > separate instance variables in the same (Hibernate) session -- boom,
> > NonUniqueObjectException.
> >
> > There are a couple of ways to fix this. The "correct" solution is to
> > avoid loading the object more than once in the first place. For
> > instance, maybe you could take a user id instead of a user object
> > into your page constructor to remove one of the load operations. The
> > standard solution is to use Session.merge() instead of, say,
> > Session.saveOrUpdate() (I assume you're getting this exception when
> > the Hibernate session flushes).
> >
> > In any event, this is happening because LoadableDetachableModel is
> > working as intended and thereby revealing one of Hibernate's many
> > "quirks" -- not because of a problem in Wicket.
> >
> >
> > -Ryan
> > On Feb 10, 2007, at 2:31 AM, Matthew Kwong wrote:
> >
> >>
> >> Hi fellows,
> >>
> >> Last time I asked how to use chain in CompoundPropertyModel with
> >> LoadableDetachableModel, it worked since my page has no form (no
> >> submit).
> >> This time, I make another page and try to use the chain again, and
> >> hibernate
> >> throws org.hibernate.NonUniqueObjectException when I submit the form.
> >>
> >> Caused by: org.hibernate.NonUniqueObjectException: a different
> >> object with
> >> the same identifier value was already associated with the session:
> >> [datastore2.model.DataStoreRole#1]
> >>
> >> My Panel:
> >>
> >> public UserDetail(String id, DataStoreUser user, final Panel
> >> prevPanel) {
> >>         super(id);
> >>         add(new FeedbackPanel("feedback"));
> >>
> >>         add(new Link("back") {
> >>             public void onClick() {
> >>                 getParent().replaceWith(prevPanel);
> >>             }
> >>         });
> >>
> >>         Form form = new Form("form") {
> >>             protected void onSubmit() {
> >>                 DataStoreUser user = (DataStoreUser)
> >> this.getModelObject();
> >>                 try {
> >>                     getDelegate().saveUser(user);
> >>                     info("You have saved the user profile: " +
> >> user.getFullname() + ".");
> >>                 } catch (Exception e) {
> >>                     e.printStackTrace();
> >>                     error("Your user profile state is stale
> >> (someone has
> >> updated the same user profile while you are on this page). Please
> >> press BACK
> >> and come back.");
> >>                 }
> >>             }
> >>         };
> >>
> >>         form.setModel(new CompoundPropertyModel(new
> >> LoadableDataStoreUserModel(getDelegate().getDataStoreUser(user.getId
> >> ()))));
> >>         form.add(new Label("id"));
> >>         form.add(new Label("username"));
> >>         form.add(new RequiredTextField("firstname"));
> >>         form.add(new
> >> TextField("lastname").setConvertEmptyInputStringToNull(false));
> >>         form.add(new Label("email"));
> >>         form.add(new Palette("roles", new
> >> Model((Serializable)getDelegate().getDataStoreRoles()), new
> >> RoleChoiceRenderer(), 10, true));
> >>         add(form);
> >>     }
> >>
> >> Is LoadableDetachableModel working for form processing? Because it is
> >> working if I only have
> >> form.setModel(new
> >> CompoundPropertyModel(getDelegate().getDataStoreUser(user.getId())));
> >>
> >> The reason why i want this loadable since I want to update the page
> >> with
> >> "F5" refresh button again if someone has changed the same user in
> >> another
> >> computer.
> >>
> >> Thank you :)
> >> Matthew Kwong
> >> --
> >> View this message in context: http://www.nabble.com/
> >> LoadableDetachableModel-in-form-processing-tf3204839.html#a8899449
> >> Sent from the Wicket - User mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> -
> >> ---
> >> Using Tomcat but need to do more? Need to support web services,
> >> security?
> >> Get stuff done quickly with pre-integrated technology to make your
> >> job easier.
> >> Download IBM WebSphere Application Server v.1.0.1 based on Apache
> >> Geronimo
> >> http://sel.as-us.falkag.net/sel?
> >> cmd=lnk&kid=120709&bid=263057&dat=121642
> >> _______________________________________________
> >> 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
>
>
> -------------------------------------------------------------------------
> 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