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

  • Home
  • SEARCH
  • 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 3323364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:17:38+00:00 2026-05-17T23:17:38+00:00

I have a couple questions regarding using the entity manager in a JavaSE environment.

  • 0

I have a couple questions regarding using the entity manager in a JavaSE environment.

I’m using the repository pattern to perform my CRUD operations on the database. There will be a base repository class like so:

public class Repository<T> implements IRepository<T> {

    private EntityManager em;
    private String persistenceUnitName;

    public Repository(String persistenceUnitName) {
        this.persistenceUnitName = persistenceUnitName;
    }

    @Override
    public T find(Class<T> type, Object id) {
        return em.find(type, id);
    }

    private EntityManager getEntityManager() {
        if (this.em == null) {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName);

            em = emf.createEntityManager();
        }

        return em;
    }
    ...
    ...
}

I will then have classes like EmployeeRepository that will inherit Repository. These repository classes will be created in my service layer.

Is this a good way of initializing the entity manager? I’m starting to think that it’s not – it seems like you should only have one entity manager per persistence unit? Where as in this case you would have an entity manager for every repository you create… How would you go about ensuring you only have one entity manager per persistence unit? Also, I noticed that the entity manager and entity manager factory methods have a close method – when should these be called? On a server terminate event?

If you know of any good sources about using JPA in JavaSE I would appreciate the info.

Thanks!

  • 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-17T23:17:39+00:00Added an answer on May 17, 2026 at 11:17 pm

    Almost. You need just one EntityManagerFactory per persistence unit.

    How would you go about ensuring you only have one EntityManagerFactory per persistence unit ?

    Usually developers create an helper class with a singleton EntityManagerFactory such as

    public class EntityManagerFactoryHelper {
    
        private static EntityManagerFactory factory;
    
        static {
            try {
               // Set up factory right here
            } catch(ExceptionInInitializerError e) {
                throw e;
            }
        }
    
        public static EntityManagerFactory getFactory() {
            return this.factory;
        }
    
    }
    

    EntityManager, in other hand, is used to interact with a set of managed entity instances called persistence context.

    If you want to know why i use ErrorInInitializerError, its API is clear

    Signals that an unexpected exception has occurred in a static initializer

    …

    Is this a good way of initializing the entity manager ?

    Well, the service layer is used to delimit Transaction boundary. So for each use case, you can create your EntityManager and pass by reference for each colaborator needed to help you to execute your use case.

    public static br.com.helper.EntityManagerFactoryHelper.getFactory;
    
    public EmployeeService {
    
        public void doSomething() {
            EntityManager eManager = getFactory().createEntityManager();
            eManager.getTransaction().begin();
    
            EmployeeRepository repository = new EmployeeRepository(eManager);
    
            eManager.getTransaction().commit();
        }
    
    }
    

    Now imagine you need the boilerplate code shown above for each use case.

        public void forEachUseCase() {
            // Create an EntityManager
            // Begin a Transaction
    
            EmployeeRepository repository = new EmployeeRepository(eManager);
    
            // And finally, commit
        }
    

    You can rely on Spring to help you to get rid of this boilerplate code.

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

Sidebar

Related Questions

I have a couple of questions regarding VBScript and ASP Classic: What is the
I have a couple of questions regarding security mechanisms in asp.net (2.0). I have
I have a couple of questions regarding core dumps. I have gdb on Windows,
I have a couple of conceptual questions: regarding serving static files (media) in Django
I've couple of questions regarding scrolling ListView without using external ListView controls from other
Recently there have been a couple questions regarding static type constraints and inline: Use
I have a couple of questions regarding a modular GWT based application framework. I
I have a couple of questions about generic wildcards in Java: What is the
I am a newbie to WPF and have a couple of questions about WPF
There have been a couple of questions that sort of dealt with this but

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.