Get all available persistence-unit names defined in persistence.xml

Viewed 397

I have persistence.xml as below (just an example) and I want to retrieve set of all persistence-unit names. In this example a,b and c

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
   <persistence-unit name="a">
   </persistence-unit>
   <persistence-unit name="b">
   </persistence-unit>
   <persistence-unit name="c">
   </persistence-unit>
</persistence>
1 Answers

You can get all persistence unit names using this code.

List<ParsedPersistenceXmlDescriptor> persistenceUnits = PersistenceXmlParser.locatePersistenceUnits(new Properties());
    for (ParsedPersistenceXmlDescriptor descriptor : persistenceUnits) {
        AVAILABLE_MODULES_LIST.add(descriptor.getName());
    }

For me it works fine.

Related