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 6093123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:31:27+00:00 2026-05-23T12:31:27+00:00

I am developing a tool that handles some database entities using JPA2, with hibernate

  • 0

I am developing a tool that handles some database entities using JPA2, with hibernate as the persistence provider. The tool is generic and uncoupled from any business logic, it takes a generic Class<? extends Task> parameter to do its business, where Task is an interface that I created to say that the implementing entity has some behaviour interesting to my tool. The work of the tool includes getting instances of the given class from the database and manipulate some of their properties. Its a generic tool because it is being developed to be reusable and we have a large set of entity classes (but not all of the entities classes) that implements that interface (mainly those entities which models some types of running business tasks).

To do the tool’s work, i need to get an EntityManager for the given class. To get an EntityManager, I need an EntityManagerFactory. To get an EntityManagerFactory, I need the persistence unit name.

So, my problem is that given a entity class, get the name of its persistence unit. However I did not found any way to do this without an already existing EntityManager for that persistence unit, which creates a chicken-or-egg-first, key-locked-inside-the-chest problem.

A partial solution is to pass the name of the persistence unit along with the entity class, but this looks wrong because the name of the persistence unit should be inferred (or at least guessed) from the given class. This could be optimized to something like putting the name in an annotation on the entity class, but this still looks like an improper solution. Searching and analyzing the persistence.xml file is something that we really want to avoid.

So, what can I do to solve this? Any ideas?

  • 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-23T12:31:28+00:00Added an answer on May 23, 2026 at 12:31 pm

    I’m pretty sure that it’s impossible to determine the persistence unit an entity class is associated with because JPA’s design is the other way around: normally you inject one EntityManagerFactory for a specific persistence unit.

    If you have at least the names of all available persistence units you could create all available factories using Persistence.createEntityManagerFactory(persistenceUnitName) and then check whether a certain class belongs to a unit using its meta model:

    for (EntityManagerFactory emf : factories) {
      try {
        EntityType<?> entity = emf.getMetamodel().entity(classToCheck);
        // bingo
      } catch (Exception e) {
        // wrong persistence unit
      }
    }
    

    Or you inspect MetaModel.getEntities() and build a mapping of unit name to managed entity classes.

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

Sidebar

Related Questions

I’m using PDK 7.3 as a development tool for developing an application using both
I'm developing a tool that will perform several types of analysis, and each analysis
I'm developing a tool that searches the keyword entered by the user on a
I am developing a tool that will eventually be used to run a sql
I'm developing the application that sending MPEG video over IP network using RTP protocol.
I've been developing an internal website for a portfolio management tool. There is a
I'm trying to use VS's attach to process tool to debug add-ins I'm developing
I am developing a site that needs an access permissioning scheme. I am uncertain
I've been developing a web-site that uses Django and MySQL; what I want to
I'm developing a system using Codeigniter , and it's nearly completion, i didn't went

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.