In the Manual and JavaDoc, Spring explicit states that it plays really well with Hibernate's Contextual Sessions feature. Namely, that when you configure DataSource, Transactions, and Session lifecycle management with Spring, it ties into this Contextual Sessions feature.
But I can't get it to work that way...
/org/example/blogroll/applicationContext.xml
hibernate.cfg.xmlCode:<?xml version="1.0" encoding="UTF-8"?> <beans ...> <bean id="blogroll" class="org.example.blogroll.BlogRollImpl"> <property name="dao" ref="blogEntryDAO" /> <property name="txMgr" ref="blogRollTxMgr" /> </bean> <bean id="blogEntryDAO" class="org.example.blogroll.BlogEntryDAO"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="dataSource" ref="blogRollDS" /> </bean> <bean id="blogRollDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/> <property name="url" value="jdbc:derby://localhost:1527/BlogDB" /> </bean> <bean id="blogRollTxMgr" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
Main.javaCode:<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "..."> <hibernate-configuration> <session-factory> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.DerbyDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="hibernate.current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <mapping resource="org/example/blogroll/BlogEntry.hbm.xml"/> </session-factory> </hibernate-configuration>
BlogRollImpl.java (my service class)Code:package org.example.blogroll; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { /** * @param args */ public static void main(String[] args) { ApplicationContext appCtx = new ClassPathXmlApplicationContext("/org/example/blogroll/applicationContext.xml"); BlogRollFacade blogroll = (BlogRollFacade) appCtx.getBean("blogroll"); BlogEntry entry = blogroll.getBlogEntry(1L); System.out.println("\"" + entry.getTitle() + "\" by " + entry.getAuthor() + " on " + entry.getDate()); System.out.println(entry.getBody()); } }
BlogEntryDAO.javaCode:package org.example.blogroll; import org.hibernate.Session; import org.springframework.orm.hibernate3.HibernateTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; public class BlogRollImpl implements BlogRollFacade { private BlogEntryDAO dao; private HibernateTransactionManager txMgr; public BlogRollImpl() { } public BlogEntry getBlogEntry(long id) { BlogEntry entry; TransactionStatus txStatus = txMgr.getTransaction(new DefaultTransactionDefinition()); try { entry = dao.getEntry(id); } catch (Exception ex) { txMgr.rollback(txStatus); throw new RuntimeException("Failed to get BlogEntry with id = " + id + "; rolling back transaction.", ex); } txMgr.commit(txStatus); return entry; } public long saveBlogEntry(BlogEntry entry) { throw new RuntimeException("saveBlogEntry() is not yet implemented."); } public BlogEntryDAO getDao() { return dao; } public void setDao(BlogEntryDAO dao) { this.dao = dao; } public HibernateTransactionManager getTxMgr() { return txMgr; } public void setTxMgr(HibernateTransactionManager txMgr) { this.txMgr = txMgr; } }
and I get the following exception:Code:package org.example.blogroll; import org.hibernate.Session; import org.hibernate.SessionFactory; public class BlogEntryDAO { private SessionFactory sessionFactory; public BlogEntry getEntry(Long id) { Session session = sessionFactory.getCurrentSession(); BlogEntry entry = (BlogEntry) session.get(BlogEntry.class, id); return entry; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
Code:Exception in thread "main" java.lang.RuntimeException: Failed to get BlogEntry with id = 1; rolling back transaction. at org.example.blogroll.BlogRollImpl.getBlogEntry(BlogRollImpl.java:26) at org.example.blogroll.Main.main(Main.java:15) Caused by: org.hibernate.HibernateException: get is not valid without active transaction at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338) at $Proxy1.get(Unknown Source) at org.example.blogroll.BlogEntryDAO.getEntry(BlogEntryDAO.java:13) at org.example.blogroll.BlogRollImpl.getBlogEntry(BlogRollImpl.java:23) ... 1 more
However, if I use the SessionFactoryUtils class.......it works.
BlogEntryDAO.java (now using SessionFactoryUtils)
I get successful output:Code:package org.example.blogroll; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.SessionFactoryUtils; public class BlogEntryDAO { private SessionFactory sessionFactory; public BlogEntry getEntry(Long id) { Session session = SessionFactoryUtils.getSession(sessionFactory, false); BlogEntry entry = (BlogEntry) session.get(BlogEntry.class, id); return entry; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
Any ideas as to why? Am I missing something fundamental here?Code:Hibernate: select blogentry0_.ID as ID0_0_, blogentry0_.title as title0_0_, blogentry0_.author as author0_0_, blogentry0_.body as body0_0_, blogentry0_.CREATED_ON as CREATED5_0_0_ from BLOG_ENTRIES blogentry0_ where blogentry0_.ID=? "This is our first Persisted Blog Entry" by SYSTEM on 2008-11-02 21:12:30.0 This is the first blog entry that we actually pulled from the database...cool!
p.s. I had to remove URL-style URIs in the XML config... you can't post URLs until ya got 5 posts under your belt...soon!![]()



Reply With Quote