views:

243

answers:

5

Right now I'm having a problem injecting a entityFactoryManager into my jpadaosupport extended class.

My configuration is below:

<bean id="productDao" class="springapp.repository.JdbcProductDao">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

The above configuration for this bean works fine however when I try to use annotations to configure the bean my application doesn't work

My JdbcProductDao.java file is below

@Repository("productDao")
@Transactional
public class JdbcProductDao extends JpaDaoSupport implements ProductDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Product> getProductList() {
        // TODO Auto-generated method stub
        return getJpaTemplate().getEntityManagerFactory().createEntityManager()
                .createQuery("from Product").getResultList();
    }

    @Override
    public void persist(Product product) {
        // TODO Auto-generated method stub

    }

    @Override
    public void saveProduct(Product prod) {
        // TODO Auto-generated method stub
        getJpaTemplate().merge(prod);
    }

    @Autowired
    @Required
    public void setJpaEntityManagerFactory(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        super.setEntityManagerFactory(entityManagerFactory);
    }
}

However it seems as though the EntityManagerFactory is not injected properly because none of my database transactions are seen

Could anybody offer any insight?

A: 

Which transaction manager did you declare? Knowing that might give a hint.

How are you testing transactions? When you say "none of my database transactions are seen", do you mean that you're INSERTing objects but your database is unchanged?

Any error messages or exceptions?

If you step through the code in a debugger, what do you see happening?

Make sure you have Log4J JAR and a config XML in your CLASSPATH. I find that Spring pumps out a wealth of information in the console on startup. You need to see that to have a better idea of what's going on. Make sure that the app context is finding your beans. There will be lots of other handy information as well. Without it, you're flying blind.

duffymo
there are no exceptions but there are no database transactions. I'm using org.springframework.orm.jpa.JpaTransactionManager
Albinoswordfish
+2  A: 

Do you have a line like this in your bean config file?

    <context:component-scan base-package="com.noisyair.whatisayis.web"/>

This tells spring to look for annotated classes in some package.

darren
I may be missing something but, is this what the question is about?
Pascal Thivent
+1  A: 

Did you declare the transaction manager ?

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
Kartoch
+2  A: 

To use the @Transactional annotation, did you add the line <tx:annotation-driven/> to your Spring configuration?

See section 9.5.6. Using @Transactional in the Chapter 9, Transaction management for all the details.

Pascal Thivent
yes I added @Transactional, thank you for you response
Albinoswordfish
the point was to add <tx:annotation-driven/>.
Bozho
@Albinoswordfish I can see that you added `@Transactional` in your java code. But **did you add `<tx:annotation-driven/>` to your spring configuration**?
Pascal Thivent
I did use the <tx:annotation-driven/> in my config
Albinoswordfish
+1  A: 

After following Pascal's tips, you will probably resolve your problem.

However I have another advice based on your code:

  • if you are going to use JpaDaoSupport, use the JpaTemplate methods.
  • if you want to use EntityManager directly, then inject it via @PersistenceContext, and don't use JpaDaoSupport

Calling createEntityManager() may disrupt the transaction handling of spring. This is because the transaction manager creates the current EntityManager which is later used. If you create it yourself, you may effectively end up having 2 different EntityManagers - one with an ongoing transaction, and one - not.

Bozho