According to me you can use Hibernate's thread local session if you
want (you still need the custom request cycle though!). This removes
the need for all the casting and getting.

Session.get().createCriteria(SysUser.class).add(.....).uniqueResult();
^^^^^^^^
Hibernate session, not Wicket's

Martijn

On 10/4/07, Stanczak Group <[EMAIL PROTECTED]> wrote:
> It's really very simple. I'm doing the same thing here. You simple use
> the HibernateUtil example that is in the Hibernate documentation. Then
> you simple create a custom RequestCycle by overriding this method in
> your application. With the request cycle you can open and close
> Hibernate seesion. Then with a custom RequestCycle you can get it from
> anywhere, it uses thread local, and us it to access your database.
> Here's some example code:
> ##############################################################################################
>     @Override
>     public RequestCycle newRequestCycle(Request request, Response
> response) {
>         return new RequestCycleImpl(this, request, response);
>     }
> ##############################################################################################
> public class RequestCycleImpl extends WebRequestCycle {
>
>     private Session hibernateSession;
>
>     public RequestCycleImpl(Application application, Request request,
> Response response) {
>         super(application, (WebRequest) request, response);
>     }
>
>     @Override
>     protected void onBeginRequest() {
>         this.hibernateSession =
> HibernateUtil.getSessionFactory().openSession();
>     }
>
>     @Override
>     protected void onEndRequest() {
>         if (this.hibernateSession != null) {
>             this.hibernateSession.close();
>         }
>     }
>
>     public Session getHibernateSession() {
>         return hibernateSession;
>     }
> }
> ##############################################################################################
> public class AllDivisionModel extends LoadableDetachableModel {
>
>     protected Object load() {
>         Session session = ((RequestCycleImpl)
> RequestCycle.get()).getHibernateSession();
>         Transaction tx = session.beginTransaction();
>         try {
>             @SuppressWarnings(value = "unchecked")
>             List<Division> results = (List<Division>)
> session.createCriteria(Division.class)
>                     .addOrder(Order.asc("name"))
>                     .list();
>             tx.commit();
>             return results;
>         } catch (Exception e) {
>             Logger.getLogger(getClass()).error(e);
>             tx.rollback();
>         }
>         return new ArrayList();
>     }
> }
> ##############################################################################################
>         private boolean isLoggedIn(String username, String password) {
>             Session session = ((RequestCycleImpl)
> getRequestCycle()).getHibernateSession();
>             Transaction tx = session.beginTransaction();
>             try {
>                 SysUser user = (SysUser)
> session.createCriteria(SysUser.class)
>                     .add(Restrictions.eq("username", username))
>                     .add(Restrictions.eq("password", password))
>                     .uniqueResult();
>                 if (!user.isLockout()) {
>                     return true;
>                 }
>                 tx.commit();
>             } catch (Exception e) {
>                 Logger.getLogger(getClass()).error(e);
>                 tx.rollback();
>             }
>             return false;
>         }
> ##############################################################################################
>
> All that said, I'm pretty new my self, so I'm sure it can be improved.
> This seems to work so far for me.
>
> Neil B. Cohen wrote:
> > I suspect I'm biting off more than I can chew conveniently but maybe
> > someone can push me in the right direction...
> >
> > I'm attempting to build a fairly simple web application with Wicket,
> > and I'd like to use Hibernate to manage the database access (although
> > other frameworks like Cayenne have been suggested and I'll look at
> > them too...)
> >
> > I think I've figured out the basic application structure, and how to
> > map my data to an html page. But I don't think I understand the
> > relationships between web sessions, hibernate sessions, DAO objects
> > etc. I need to open a mysql db, read a set of objects from a table,
> > and display them in a (paged) table on the screen. I've looked at
> > several examples but they are using in-memory databases, or Spring
> > along with Hibernate and I can't get a handle on what needs to be done
> > to whom and by whom....
> >
> > Anyone have a really simple MySQL example like that? Or an online
> > tutorial that I could follow?
> >
> > Much obliged,
> >
> > nbc
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
>
> --
> Justin Stanczak
> Stanczak Group
> 812-735-3600
>
> "All that is necessary for the triumph of evil is that good men do nothing."
> Edmund Burke
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Buy Wicket in Action: http://manning.com/dashorst
Apache Wicket 1.3.0-beta3 is released
Get it now: http://www.apache.org/dyn/closer.cgi/wicket/1.3.0-beta3/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to