Spring DaoSupport and @PersistanceContext EntityManager?

One of the most difficult things in understanding Spring is that Spring supports multiple approaches to the same problem.

So, in my application, I use an injectable EntityManager using the @PersistanceContext annotation, for example:

@Repository public class JpaDao extends JpaDaoSupport implements Dao { @PersistenceContext(unitName = "PersistanceUnit", type = PersistenceContextType.EXTENDED) private EntityManager em; 

Is this approach compatible with the JpaDaoSupport extension (which requires an injection in the EntityManager)? For me, this looks like two incompatible approaches to solving the same problem, but I would like to get some advice from someone who has more experience with Spring.

If I will not extend JpaDaoSupport, how can I create my DAO using the @PersistenceContext approach?

+4
source share
3 answers

You are right that these are two different approaches to the same problem. I think which one is β€œbetter” is a matter of taste. Using annotations avoids Spring import dependencies in your code, and even Spring JavaDoc for JpaDaoSupport suggests using them for new JPA projects. JpaDaoSupport supports Spring for JPA, equivalent to its support for other ORM strategies ( HibernateDaoSupport , JdbcDaoSupport , TopLinkDaoSupport , etc.). In these cases, annotation-based injection is not an option.

+4
source

To enter EntityManager you just need to add the following definition

 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

You can get more information about this topic in this post from the official blog.

+1
source

I would rather recommend you not extend JpaDaoSupport, spring will do everything for you. Follow the link provided by diega for more information on the same blog that I followed to upgrade my application to support spring - jpa.

0
source

All Articles