0

i am new to Spring and hibernate and i am stuck at this problem. i have been searching around for a fix but although there are a lot of questions on this, they do not seem to solve my problem. i am using spring 3.1.0 with hibernate 3.6.9 and making a web application with using spring mvc. After a lot of looking around, i managed to solve it with the following config

web.xml

<listener> <description>Spring context loader</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>*.do</url-pattern>
</filter-mapping>


<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

applicationContext.xml

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven  />
<context:annotation-config/>

<!-- Scans within the base package of the application for @Components to 
    configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.emumba.cricketcalendar" />



<import resource="hibernate-context.xml"/>

hibernate-context.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.emumba.cricketcalendar.domain.Match</value>
            <value>com.emumba.cricketcalendar.domain.Ground</value>
            <value>com.emumba.cricketcalendar.domain.Umpire</value>
            <value>com.emumba.cricketcalendar.domain.Country</value>
            <value>com.emumba.cricketcalendar.domain.CricketStatus</value>
            <value>com.emumba.cricketcalendar.domain.Series</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

And then i put the @Transactional on my service and this exception was gone. But then my autowiring stops working, the autowired annotation doesn't work and beans with autowired properties start throwing exception. i remove the @transactional annotation from my service and it again starts working but the "no hibernate session bound to thread " exception returns

So i am really confused , any help would be greatly appreciated

EDIT Service Code

@Service(value="calendarManager") public class CalendarMangerImpl implements CalendarManager {

@Autowired
@Qualifier("matchDao")
public MatchDaoHibernate matchDao;

@Override
public List<Match> getAllMatches() {
    List<Match> matches=new ArrayList<Match>();
    matches=matchDao.findAll();
    return matches;
}

}

Khizar
  • 2,288
  • 5
  • 32
  • 53

2 Answers2

2

Use the service class with reference to its interface and not the actual class as spring uses interface based proxies by default

Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables 
any interface (or set of interfaces) to be proxied. 
Aravind A
  • 9,507
  • 4
  • 36
  • 45
  • @Arvind A all my daos are extending a generic dao and when i moved the @transactional from the child to the parent calss , it gave this `org.springframework.aop.framework.AopConfigException: Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.` – Khizar Jan 17 '12 at 09:59
  • @khizar one more query - How are you using the service ? I hope you're using CalendarManager myservice and not CalendarMangerImpl myservice from wherever you're calling it . – Aravind A Jan 17 '12 at 10:11
  • well i am using the CalendarManagerImpl , because the @ Service annotation is on the class not the interface. is it wrong ? – Khizar Jan 17 '12 at 10:17
  • Yes . use CalendarManager myservice . The service annotation will be detected even in this case . You're using interface base proxying - Hence you need to have an interface there . – Aravind A Jan 17 '12 at 10:23
  • @Khizar add CGLIB jar to your class path as you are doing class based proxies instead of JDK interface-based proxies – Pokuri Jan 17 '12 at 10:23
  • @khizar - See the discussion at http://stackoverflow.com/questions/2713033/autowire-strange-problem . The problem is same as yours . – Aravind A Jan 17 '12 at 10:24
  • @AravindA thanx alot mate, that was the problem, even after using the interface reference for the service, i was still using class reference for the dao, as soon as i changed it , it worked :) – Khizar Jan 17 '12 at 10:35
1

I will suggest first go through this simple example clear all the concepts. It will help you in future as well.

Rupeshit
  • 1,476
  • 1
  • 14
  • 23