Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the PersistenceUnit name for a given entity class?
    primarykey
    data
    text
    <p>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 <code>Class&lt;? extends Task&gt;</code> 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).</p> <p>To do the tool's work, i need to get an <code>EntityManager</code> for the given class. To get an <code>EntityManager</code>, I need an <code>EntityManagerFactory</code>. To get an <code>EntityManagerFactory</code>, I need the persistence unit name.</p> <p>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.</p> <p>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 <code>persistence.xml</code> file is something that we really want to avoid.</p> <p>So, what can I do to solve this? Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload