You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "Neil B. Cohen" <nc...@verisign.com> on 2007/10/04 18:44:34 UTC

More on Wicket/Hibernate...

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: More on Wicket/Hibernate...

Posted by Michael Laccetti <mi...@s2g.ca>.
I'm not sure if Spring is something that really requires much learning - More than anything it is a good way of tying together a bunch of disparate frameworks for use together.

Neil B. Cohen wrote:
> Michael Laccetti wrote:
>  > If you are using Spring, you may be interested in a Servlet Filter
>  > that supports opening/closing sessions on a per-request basis:
>  > 
> http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html
>  >
>  >
>  > Mike
> 
> Thanks  - at the moment, I'm not using Spring - I was trying to figure
> out if I need to use it - that would mean yet another framework to learn
> at the same time as Wicket and Hibernate....
> 
> nbc

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: More on Wicket/Hibernate...

Posted by "Neil B. Cohen" <nc...@verisign.com>.
Michael Laccetti wrote:
> If you are using Spring, you may be interested in a Servlet Filter 
> that supports opening/closing sessions on a per-request basis: 
> http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html 
>
>
> Mike

Thanks  - at the moment, I'm not using Spring - I was trying to figure 
out if I need to use it - that would mean yet another framework to learn 
at the same time as Wicket and Hibernate....

nbc
>
> 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: 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: More on Wicket/Hibernate...

Posted by Michael Laccetti <mi...@s2g.ca>.
If you are using Spring, you may be interested in a Servlet Filter that supports opening/closing sessions on a per-request basis: http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/orm/jpa/support/OpenEntityManagerInViewFilter.html

Mike

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: 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: More on Wicket/Hibernate...

Posted by Igor Vaynberg <ig...@gmail.com>.
On 10/4/07, Neil B. Cohen <nc...@verisign.com> wrote:
> Thanks for the sample code - I'll see what I can do with it - you'll
> probably hear from me again :)

thanks for the warning

-igor


>
> nbc
> > On 10/4/07, Stanczak Group <ju...@stanczakgroup.com> 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: users-unsubscribe@wicket.apache.org
> >>> For additional commands, e-mail: users-help@wicket.apache.org
> >>>
> >>>
> >>>
> >> --
> >> 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: 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: More on Wicket/Hibernate...

Posted by Igor Vaynberg <ig...@gmail.com>.
this can all be done independently of wicket using a
servletcontextlistener for sessionfactory start/shutdown and a servlet
filter for closing the session at the end of requests. go for the
simplest things first :)

-igor


On 10/4/07, Stanczak Group <ju...@stanczakgroup.com> wrote:
> Another piece I forgot to add is the config and shutting down of the
> factory. I use this in the WebApplication:
>
>     @Override
>     protected void init() {
>         try {
>             HibernateUtil.setSessionFactory(new
> Configuration().configure().buildSessionFactory());
>         } catch (Throwable e) {
>             Logger.getLogger(getClass()).error(e);
>         }
>     }
>
>     @Override
>     protected void onDestroy() {
>         HibernateUtil.getSessionFactory().close();
>     }
>
> Neil B. Cohen wrote:
> > Martijn Dashorst wrote:
> >> 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
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
> --
> 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: 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: More on Wicket/Hibernate...

Posted by Stanczak Group <ju...@stanczakgroup.com>.
Another piece I forgot to add is the config and shutting down of the 
factory. I use this in the WebApplication:

    @Override
    protected void init() {
        try {
            HibernateUtil.setSessionFactory(new 
Configuration().configure().buildSessionFactory());
        } catch (Throwable e) {
            Logger.getLogger(getClass()).error(e);
        }
    }

    @Override
    protected void onDestroy() {
        HibernateUtil.getSessionFactory().close();
    }

Neil B. Cohen wrote:
> Martijn Dashorst wrote:
>> 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
>>
>>
>>   
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

-- 
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: More on Wicket/Hibernate...

Posted by "Neil B. Cohen" <nc...@verisign.com>.
Martijn Dashorst wrote:
> 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
>   

Thanks for the sample code - I'll see what I can do with it - you'll 
probably hear from me again :)

nbc
> On 10/4/07, Stanczak Group <ju...@stanczakgroup.com> 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: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>>       
>> --
>> 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: 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: More on Wicket/Hibernate...

Posted by Martijn Dashorst <ma...@gmail.com>.
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 <ju...@stanczakgroup.com> 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: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
> --
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: More on Wicket/Hibernate...

Posted by Stanczak Group <ju...@stanczakgroup.com>.
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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

-- 
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: More on Wicket/Hibernate...

Posted by Nathan Hamblen <na...@technically.us>.
Neil B. Cohen wrote:
 > Anyone have a really simple MySQL example like that? Or an online
 > tutorial that I could follow?

We have one of those:
http://databinder.net/site/show/baseball-players

I don't know if you want to use Databinder or not, but you aren't going 
to find a lot of code or tutorials otherwise for what you're asking 
because most people are using Spring managed sessions and transactions.

If you need any help that is particular to Databinder please register on 
the forum and send me an email immediately afterward, or else the 
registration will be lost among 300 spam registrations.

Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org