Here is a hack to allow multiple persistence.xmls. Why do i want different persistence.xmls? cos i have different sets of entities that belong to different dbs and different cvs modules.
Found this spring solution:
http://forum.springframework.org/showthread.php?t=26365
but im not using spring.
I am using the spring.jar for the PathMatchingResourcePatternResolver so i can find the different persistence.xml in the different jars. There may be a better way of doing this
Code:
/**
*
* Source 1: http://forum.springframework.org/showthread.php?t=26365
* Source 2: http://www.hibernate.org/386.html?cmd=comphist&histnode=2844
* @throws Exception
*/
private static void hackInit() throws Exception {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:META-INF/persistence.xml");
for (Resource resource : resources) {
if (resource.getURL().getProtocol().equals("file")) {
VzLogger.info("Found Persistence.xml: " + resource.getFile().getCanonicalPath());
URL classpathRoot = new URL("file:"+determineClasspathRootDir(resource, "META-INF/persistence.xml"));
List<PersistenceMetadata> persistenceMetadatas = PersistenceXmlLoader.deploy(resource.getURL(), new HashMap(), new EJB3DTDEntityResolver());
for(PersistenceMetadata metadata : persistenceMetadatas){
// Configure our EJB3 EntityManagerFactory
Ejb3Configuration ejbconf = new Ejb3Configuration();
for(Entry entry : metadata.getProps().entrySet()){
ejbconf.setProperty(entry.getKey().toString(), entry.getValue().toString());
}
// Find all our persistent classes from persistence.jar
JarVisitor.Filter[] filters = new JarVisitor.Filter[2];
filters[0] = new JarVisitor.PackageFilter( false, null ) {
public boolean accept(String javaElementName) {
return true;
}
};
filters[1] = new JarVisitor.ClassFilter(
false, new Class[]{
Entity.class,
MappedSuperclass.class,
Embeddable.class}
) {
public boolean accept(String javaElementName) {
return true;
}
};
// Add all persistent classes found to EJB3 config
JarVisitor j = JarVisitor.getVisitor(classpathRoot, filters);
Set<JarVisitor.Entry>[] entries = (Set<JarVisitor.Entry>[])j.getMatchingEntries();
for (int i = 0; i < entries.length; i++)
{
for (JarVisitor.Entry c : entries[i])
{
VzLogger.debug("Found entity " + c.getName());
ejbconf.addAnnotatedClass(Class.forName(c.getName()));
}
}
// Add any classes stated in the config file
for(String string : metadata.getClasses()){
VzLogger.debug("Persistence.xml entity " + string);
ejbconf.addAnnotatedClass(Class.forName(string));
}
//Use depricated because of http://forum.hibernate.org/viewtopic.php?t=967615
EntityManagerFactory emf = ejbconf.createEntityManagerFactory();
//Save emf to a map
VzLogger.info("Added EntityManagerFactory: " + metadata.getName());
}
}
}
}
protected static String determineClasspathRootDir(Resource resource, String classpathMarkerFile) throws IOException {
String location = resource.getFile().getAbsolutePath();
return location.substring(0, location.length()-classpathMarkerFile.length());
}
I have not tested this in a production env