Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:13:18+00:00 2026-05-30T03:13:18+00:00

i am in the process of developing a seperate java component which will be

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:13:19+00:00Added an answer on May 30, 2026 at 3:13 am

    It is possible as long as you:

    • share the same Spring context between your parent application and your component
    • or your application context is the parent of your component Spring context.

    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 your applicationContext.xml). If you are using the traditional XML configuration, <context:annotation-config /> and then declaring your persistence components in the applicationContext.xml should be enough.

    What would definitely not work is using completely independent contexts for your parent application and your component in the JAR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're currently in the process of developing a web-based application which will require the
I'm in the process of developing a multi-tiered financial processing application in Java using
I am in the process of developing an IOS rpg. This game is a
I'm in the process of developing a site that will allow players of a
I am in the process of developing an SSIS job on my workstation which
I am in the process of developing an application which uses a legacy USB
I am in the process of developing a simple JFrame based GUI to which
We are in the process of developing a .NET based IIS hosted web application
I'm in the process of developing a social network site. And been thinking of
I am in the process of developing a web application that consists visually of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.