i am in the process of developing a seperate java component which will be packaged as a jar and added to the parent project. the reason for making this as a component is because tomorrow i might have to remove that code and discard it.
The problem that i am facing is the standalone component,(jar file) which is being developed needs to query the database when someone calls its API.
my parent project is setup using Spring + Hibernate + JPA.
I am not able to inject the EntityManager from the parent project into the external Jar which is instantiated during the startup of the spring container.
Is this conceptually correct? or is it possible to do it in this fashion?
i want the jar file to just piggyback on the persistence.xml which is defined by the parent project and make use of the EntityManager which was loaded during startup.
EDIT
The entityManager is injected through the LocalContainerEntityManagerFactoryBean in the main webapp in this fashion
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceWrapper" />
<property name="persistenceUnitName" value="LineManagement" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="${hibernate.dialect}" />
</bean>
</property>
<property name="beanName" value="entityManager"></property>
</bean>
The abstractDAO in the parent webapp is defined in the following manner, and the PersistenceContext annotation does give back the EntityManager and everything works fine..
public abstract class AbstractDAO<T extends LMEntity> {
@PersistenceContext(unitName = "LineManagement")
protected EntityManager entityManager;
@Autowired
private DataSource dataSource;
@Autowired
private DAOSupport daoSupport;
public void initHibernateStatistics() {
HibernateStatistics.enableHibernateStatistics(entityManager);
}
Below is the DAO in the jar file where i want the EntityManager to be injected so that it can use the one already provided
This is the DAO in the jar file
@Repository(value = "retryDAO")
public class RetryDAOImpl
implements RetryDAO {
@Autowired
private EntityManager entityManager;
@Transactional
public void saveEvent(final IntegrationEvent event) {
entityManager.merge(event);
}
I tried the @Autowired annotation and it mentions that the entityManager bean isnt present.
It is possible as long as you:
Furthermore, you need to make sure that Spring does in fact instantiate your component so that it can inject whatever is necessary. If you are using annotation-based configuration, this might involve component scanning (that is, you would need to specify
<context:component-scan />in yourapplicationContext.xml). If you are using the traditional XML configuration,<context:annotation-config />and then declaring your persistence components in theapplicationContext.xmlshould be enough.What would definitely not work is using completely independent contexts for your parent application and your component in the JAR.