DATAJPA-138 - Make MergingPesistenceUnitManager work again.

Override isPersistenceUnitOverrideAllowed() newly introduced in Spring 3.1.1 to indicate we can deal with persistence units of the same name. For Spring 3.0.x versions the implementation was not broken.
This commit is contained in:
Oliver Gierke
2011-12-14 18:26:21 +01:00
parent 87bc8820b2
commit 9224a4b14f
4 changed files with 48 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,15 @@ public class MergingPersistenceUnitManager extends DefaultPersistenceUnitManager
}
}
/*
* (non-Javadoc)
* @see org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager#isPersistenceUnitOverrideAllowed()
*/
@Override
protected boolean isPersistenceUnitOverrideAllowed() {
return true;
}
void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui, PersistenceUnitInfo oldPui) {
for (URL url : oldPui.getJarFileUrls()) {
@@ -68,6 +77,14 @@ public class MergingPersistenceUnitManager extends DefaultPersistenceUnitManager
}
}
for (String className : oldPui.getManagedClassNames()) {
if (!pui.getManagedClassNames().contains(className)) {
log.debug("Adding class {} to PersistenceUnit {}", className, pui.getPersistenceUnitName());
pui.addManagedClassName(className);
}
}
pui.addJarFileUrl(oldPui.getPersistenceUnitRootUrl());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.net.MalformedURLException;
@@ -27,6 +29,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.jpa.domain.sample.Role;
import org.springframework.data.jpa.domain.sample.User;
import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
/**
@@ -53,4 +57,17 @@ public class MergingPersistenceUnitManagerUnitTests {
manager.postProcessPersistenceUnitInfo(newInfo, oldInfo);
verify(newInfo).addJarFileUrl(jarFileUrl);
}
@Test
public void mergesManagedClassesCorrectly() {
MergingPersistenceUnitManager manager = new MergingPersistenceUnitManager();
manager.setPersistenceXmlLocations(new String[] { "classpath:org/springframework/data/jpa/support/persistence.xml",
"classpath:org/springframework/data/jpa/support/persistence2.xml" });
manager.preparePersistenceUnitInfos();
PersistenceUnitInfo info = manager.obtainPersistenceUnitInfo("pu");
assertThat(info.getManagedClassNames().size(), is(2));
assertThat(info.getManagedClassNames(), hasItems(User.class.getName(), Role.class.getName()));
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="pu">
<class>org.springframework.data.jpa.domain.sample.User</class>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="pu">
<class>org.springframework.data.jpa.domain.sample.Role</class>
</persistence-unit>
</persistence>