|
Hi,
I think I solved this issue on my local PC, using Hibernate 4.3.11.Final as base code. The idea was to do something similar with what EclipseLink implemented with https://www.eclipse.org/eclipselink/api/2.4/org/eclipse/persistence/config/PersistenceUnitProperties.html#ECLIPSELINK_PERSISTENCE_XML
I added two extra properties in AvailableSettings:
String HIBERNATE_PERSISTENCE_XML = "org.hibernate.persistencexml"; String HIBERNATE_PERSISTENCE_XML_DEFAULT = "META-INF/persistence.xml";
With this and some minimal modifications in PersistenceXmlParser, I was able to load multiple persistence.xml, or override the default location. If the custom property is not specified, all will work as before, with the entity manager trying to find persistence.xml in META-INF/persistence.xml
The problem is that I don't have the time to follow all the standard procedures required for implementing something new with GIT , Atlassian, so I would really appreciate it if I could send the 3 modified files to someone who already has an account. Also, I was not able to run the build tests for Hibernate, but I did use the modified version in my projects and it works as expected.
PS: Here is the modified code in PersistenceXmlParser:
.......
public List<ParsedPersistenceXmlDescriptor> doResolve(Map integration) { final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();
String resource = getResourceXmlLocation(integration); final List<URL> xmlUrls = classLoaderService.locateResources( resource );
if ( xmlUrls.isEmpty() ) { LOG.unableToFindPersistenceXmlInClasspath(resource); }
else { for ( URL xmlUrl : xmlUrls ) { persistenceUnits.addAll( parsePersistenceXml( xmlUrl, integration ) ); }
}
return persistenceUnits; }
private String getResourceXmlLocation(Map integration){ String resource = null; String key = AvailableSettings.HIBERNATE_PERSISTENCE_XML;
if((integration != null) && integration.containsKey(key)) { resource = (String)integration.get(key); LOG.infof( "Using custom persistence.xml file : %s", resource ); }
if(resource == null) { resource = AvailableSettings.HIBERNATE_PERSISTENCE_XML_DEFAULT; LOG.infof( "Using default persistence.xml file : %s", resource ); }
return resource; }
private List<ParsedPersistenceXmlDescriptor> parsePersistenceXml(URL xmlUrl, Map integration) { LOG.tracef( "Attempting to parse persistence.xml file : %s", xmlUrl.toExternalForm() );
final Document doc = loadUrl( xmlUrl ); final Element top = doc.getDocumentElement();
final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();
final NodeList children = top.getChildNodes(); for ( int i = 0; i < children.getLength() ; i++ ) { if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) { final Element element = (Element) children.item( i ); final String tag = element.getTagName(); if ( tag.equals( "persistence-unit" ) ) {
String resource = getResourceXmlLocation(integration);
final URL puRootUrl = ArchiveHelper.getJarURLFromURLEntry( xmlUrl, resource ); .....
|