Difference between
version 299
and
version 298:
| At line 20 added 1 line. |
| ;:''The DAO layer has the Hibernate sessionFactory injected (made available) to it through Spring and this entry in applicationContext-hibernate.xml: |
| At line 22 added 3 lines. |
| {{{ |
| <property name="sessionFactory"><ref local="sessionFactory"/></property> |
| }}} |
| At line 26 added 1 line. |
| So to access the Hibernate session from within your DAO, place a method like this in your DAO: |
| At line 28 added 38 lines. |
| {{{ |
|
| Session session = SessionFactoryUtils.getSession(getSessionFactory(), false); |
|
| }}} |
|
| * How do I get hold of the Hibernate session in my View code? |
| ;:''To get hold of the Session in the view code, you need to lookup the SessionFactory defined in the applicationContext-hibernate.xml: |
|
| {{{ |
| private void createApplicationContext() { |
| if (ctx == null) { |
| ctx = |
| WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext()); |
| } |
| } |
|
| public net.sf.hibernate.Session getCurrentHibernateSession() { |
| createApplicationContext(); |
| |
| SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory"); |
| SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.getResource(sessionFactory); |
| net.sf.hibernate.Session currentSession = null; |
| Transaction currentTransaction = null; |
| |
| if (sessionHolder!=null) { |
| currentSession = sessionHolder.getSession(); |
| currentTransaction = sessionHolder.getTransaction(); |
| } |
| return currentSession; |
| } |
|
| }}} |
|
|
|
|
|
Back to SandBox,
or to the Page History.
|