0
votes

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;
}

}

2
Can we see the code for your service? - Alex Barnes
I was about to come on to the JDK vs CGLIB proxy issue. Looks like you've got your answer. - Alex Barnes
@Alex ya i did, finally :) thanx anyways :) - Khizar

2 Answers

2
votes

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. 
1
votes

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