Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 620 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to inject JPA EntityManager using spring

#1
Is it possible to have *Spring* inject the JPA `entityManager` object into my DAO class without extending `JpaDaoSupport`? If yes, does Spring manage the transaction in this case?

I'm trying to keep my Spring configuration as simple as possible:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="em"/>
</bean>
<bean id="em" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPU"/>
</bean>
Reply

#2
> Is it possible to have spring to inject the JPA entityManager object into my DAO class whitout extending JpaDaoSupport? if yes, does spring manage the transaction in this case?

This is documented black on white in [12.6.3. Implementing DAOs based on plain JPA][1]:

> It is possible to write code against
> the plain JPA without using any Spring
> dependencies, using an injected
> `EntityManagerFactory` or `EntityManager`.
> Note that Spring can understand
> `@PersistenceUnit` and
> `@PersistenceContext` annotations both
> at field and method level if a
> `PersistenceAnnotationBeanPostProcessor`
> is enabled. A corresponding DAO
> implementation might look like this (...)

And regarding transaction management, have a look at [12.7. Transaction Management][2]:

> Spring JPA allows a configured `JpaTransactionManager` to expose a JPA transaction to JDBC access code that accesses the same JDBC DataSource, provided that the registered `JpaDialect` supports retrieval of the underlying JDBC Connection. Out of the box, Spring provides dialects for the Toplink, Hibernate and OpenJPA JPA implementations. See the next section for details on the `JpaDialect` mechanism.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
Yes, although it's full of gotchas, since JPA is a bit peculiar. It's very much worth reading the documentation on injecting JPA `EntityManager` and `EntityManagerFactory`, without explicit Spring dependencies in your code:

[To see links please register here]


This allows you to either inject the `EntityManagerFactory`, or else inject a thread-safe, transactional proxy of an `EntityManager` directly. The latter makes for simpler code, but means more Spring plumbing is required.

Reply

#4
The latest Spring + JPA versions solve this problem fundamentally.
You can learn more how to use Spring and JPA togather in [a separate thread](

[To see links please register here]

)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through