title: Spring @Transactional and inheritance
body: <p>I do have a generics-based DAO class, which is base for all other DAO classes in my project and contains common functionality:</p>
  
  <pre><code>public class Dao&lt;E&gt; {
  
      private SessionFactory factory;
  
      public void setSessionFactory(SessionFactory factory) {
          this.factory = factory;
      }
  
      public E get(int id) {
          // ....
      }
  
      public void save(E entity) {
          // ...
      }
  
      public void delete(E entity) {
          // ...
      }
  
  }
  </code></pre>
  
  <p>In my project I am using multiple datasources, pointing to different databases, therefore, I do have multiple session and transaction managers:</p>
  
  <pre><code>&lt;bean id="factory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="source1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="manager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="factory1" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="factory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="source2" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="manager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="factory2" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Now I want to create couple of DAOs, operating on different databases:</p>
  
  <pre><code>@Repository
  @Transactional("manager1")
  public class Dao1 extends Dao&lt;Entity1&gt; {
  
      @Overrides
      @Autowired
      @Qualifier("factory1")
      public void setSessionFactory(SessionFactory factory) {
          super.setSessionFactory(factory);
      }
  
  }
  
  @Repository
  @Transactional("manager2")
  public class Dao1 extends Dao&lt;Entity2&gt; {
  
      @Overrides
      @Autowired
      @Qualifier("factory2")
      public void setSessionFactory(SessionFactory factory) {
          super.setSessionFactory(factory);
      }
  
  }
  </code></pre>
  
  <p>but the problem is, that all public methods from <code>Dao</code> aren't covered by <code>@Transactional</code> from a child classes, so no transaction management occurs. The only option what I can think of is to override the parent class methods, so they are defined in inherited classes and therefore are taken care by <code>@Transactional</code>:</p>
  
  <pre><code>@Repository
  @Transactional("manager1")
  public class Dao1 extends Dao&lt;Entity1&gt; {
  
      @Overrides
      @Autowired
      @Qualifier("factory1")
      public void setSessionFactory(SessionFactory factory) {
          super.setSessionFactory(factory);
      }
  
      @Overrides
      public Entity1 get(int id) {
          return super.get(id);
      }
  
      @Overrides
      public void save(Entity1 entity) {
          super.save(entity);
      }
  
      @Overrides
      public void delete(Entity1 entity) {
          super.delete(entity);
      }
  
  }
  </code></pre>
  
  <p>But then I would need to do this in <strong>every</strong> DAO class, and code would be everywhere the same... </p>
  
  <p>Is there a better way how to share common functionality across all classes and still have all benefits of declarative transaction management?</p>