Announcement Announcement Module
No announcement yet.
Problem with @Transactional + implement a generic interface? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with @Transactional + implement a generic interface?


    My first message in this community

    I'm having issues when configuring Spring + Hibernate.

    I have an interface HibernateDao<T> :

    public interface HibernateDao<T> extends Dao<T> {
        public void setSessionFactory(SessionFactory sessionFactory);
        public List<T> getAll();
        public T get(Integer id);
        public T insert(T add);
        public T update(T update);
        public void delete(Integer id);
        public Integer getCount();
        public T getNext(Integer previousId);
        public List<T> getRange(Integer startRow, Integer endRow, Ordenacion[] sortBy,
    	    Map<String, Object> filterCriteria);
    Then, the concrete DAO classes implements that interface and the @Transactional annotations in the methods.

    public class ActividadDAOHibernate implements HibernateDao<Actividad> {
        private SessionFactory sessionFactory;
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        public List<Actividad> getAll() {
            return (List<Actividad>) sessionFactory.getCurrentSession()
        public Actividad get(Integer id) {
            return (Actividad) sessionFactory.getCurrentSession().get(
                Actividad.class, id);
        public Actividad insert(Actividad add) {
            Integer id = (Integer) sessionFactory.getCurrentSession().save(add);
            return add;
    Here is my configuration XML:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
        xmlns:xsi="" xmlns:context=""
        xmlns:tx="" xmlns:util=""
        <context:property-placeholder location="" />
        <!--========== TransactionManager ========== -->
        <bean id="txManager"
            <property name="sessionFactory" ref="sessionFactory" />
        <!-- ========= Para usar @Transactional =========== -->
        <tx:annotation-driven transaction-manager="txManager" />
        <!--========== /TransactionManager ========== -->
        <!--========== Hibernate SessionFactory ========== -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!-- <property name="dataSource"> <ref bean="dataSource" /> </property> -->
            <property name="hibernateProperties">
                    <prop key="hibernate.connection.driver_class">${hibernate.connection.driver_class}</prop>
                    <prop key="hibernate.connection.password">${hibernate.connection.password}</prop>
                    <prop key="hibernate.connection.url">${hibernate.connection.url}</prop>
                    <prop key="hibernate.connection.username">${hibernate.connection.username}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    <prop key="current_session_context_class">${hibernate.current_session_context_class}</prop>
            <property name="annotatedClasses">
    	<bean id="actividadDao"
    		<property name="sessionFactory" ref="sessionFactory" />
    Finally I have this this test:
    public class ActividadDAOHibernateTest {
        private static ApplicationContext ctx;
        private static ActividadDAOHibernate dao;
        public static void setUpBeforeClass() throws Exception {
            ctx = new ClassPathXmlApplicationContext("beans.xml");
            Object o = ctx.getBean("actividadDao");
            dao = (ActividadDAOHibernate) o;
        public void testGetAll() {
            List<Actividad> list = dao.getAll();
            assertEquals("Se espera una lista con dos elementos", 2, list.size());
        public void testGet() {
            Actividad act = dao.get(1);
            assertEquals("Se espera que el id sea 1", new Integer(1), act.getId());
            assertEquals("Se espera que el código sea 1", new Integer(1), act.getCodigo());
            assertEquals("Se espera que el nombre sea act1", "act1", act.getNombre());

    When I execute the test I get this exception in the @BeforeClass method:
    java.lang.ClassCastException: $Proxy12 cannot be cast to es.aevum.gwt.aevumerp.server.persistence.dao.hibernate.ActividadDAOHibernate
        at es.aevum.gwt.aevumerp.server.persistence.dao.hibernate.ActividadDAOHibernateTest.setUpBeforeClass(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(
        at org.junit.internal.runners.statements.RunBefores.evaluate(
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
    Well, I know that if I configure the annotation-driven this way:
    <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
    then the problem is solved.

    The problem is that without the proxy-target-class="true" I only get the exception IF THE HibernateDao<T> INTERFACE HAVE THE GENERIC TYPE (T).
    If my DAO doesn't implement any interface or if the interface doesn't have the generic type (i.e. HibernateDao) I don't get the Exception without proxy-target-class="true".

    So, IMHO is probably an issue in the ProxyFactory (¿?) when the Proxied class implements an interface with a Generic Type (:S)

    In addition I would like to implement the @Transactional annotation in the HibernateDao<T> interface, which is not possible with proxy-target-class="true"

    What can I do??

    Thank you!
    - Luis

  • #2
    You shouldn't be programming to concrete classes but interfaces. So instead of using ActividadDAOHibernate you should be using 'HibernateDao<Actividad>' in your test case.

    Generics are a beast especially when you start proxying especially classproxying which relies on cglib which is quite dated and probably doesn't handle generic proxies that well.


    • #3
      Thanks a lot! I'll try it next Monday


      • #4
        Annotation use to direct to jsp page

        Please help me out in telling me the annotation used to direct from a handler to a jsp page and also the annotation used for application context.

        Also,tell me the way by which I can annotate the property with list of values defined in bean injection xml file.

        Please its urgent.

        I am stuck badly.


        • #5
          @marten it works! Thank you

 I don't understand your question but it seems to not be related with this thread. Open a new one