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

}

3
  • Can we see the code for your service? Commented Jan 17, 2012 at 9:32
  • I was about to come on to the JDK vs CGLIB proxy issue. Looks like you've got your answer. Commented Jan 17, 2012 at 10:48
  • @Alex ya i did, finally :) thanx anyways :) Commented Jan 17, 2012 at 10:53

2 Answers 2

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. 
Sign up to request clarification or add additional context in comments.

7 Comments

@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 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 .
well i am using the CalendarManagerImpl , because the @ Service annotation is on the class not the interface. is it wrong ?
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 .
@Khizar add CGLIB jar to your class path as you are doing class based proxies instead of JDK interface-based proxies
|
1

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

2 Comments

thanx but i have already done some simple examples and basically i was just taking it to more complex problems...
@Khizar Can please verify your config settings?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.