use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class ImportSqlScriptTest method persistence.
@Module
public Persistence persistence() {
final PersistenceUnit unit = new PersistenceUnit("ImportSqlScriptTest");
unit.addClass(Something.class);
unit.setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
unit.setProperty("openjpa.Log", "DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE");
unit.setExcludeUnlistedClasses(true);
final Persistence persistence = new Persistence(unit);
persistence.setVersion("2.0");
return persistence;
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class PersistenceUnitLinkResolverTest method resolve.
@Test
public void resolve() {
final AppModule appModule = new AppModule(Thread.currentThread().getContextClassLoader(), "target/classes/foo", new Application(), false);
Files.mkdir(new File("target/classes/foo/bar"));
final PersistenceUnitLinkResolver resolver = new PersistenceUnitLinkResolver(appModule);
resolver.add(URI.create("file:/fake/1"), "foo", new PersistenceUnit());
resolver.add(URI.create("file:/fake/2"), "foo", new PersistenceUnit());
// can't resolve but doesn't fail
assertNull(resolver.resolveLink("foo", URI.create("bar")));
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class AutoConfigPersistenceUnitsTest method addPersistenceUnit.
// --------------------------------------------------------------------------------------------
// Convenience methods
// --------------------------------------------------------------------------------------------
private PersistenceUnitInfo addPersistenceUnit(final String unitName, final String jtaDataSource, final String nonJtaDataSource) throws OpenEJBException, IOException, NamingException {
final PersistenceUnit unit = new PersistenceUnit(unitName);
unit.setJtaDataSource(jtaDataSource);
unit.setNonJtaDataSource(nonJtaDataSource);
final AppModule app = new AppModule(this.getClass().getClassLoader(), unitName + "-app");
app.addPersistenceModule(new PersistenceModule("root", new Persistence(unit)));
// Create app
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
return appInfo.persistenceUnits.get(0);
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class PersistenceUnitLinkResolver method isIn.
private boolean isIn(final PersistenceUnit value, final WebModule war) {
final Collection<URL> urls = (Collection<URL>) war.getAltDDs().get(DeploymentLoader.EAR_WEBAPP_PERSISTENCE_XML_JARS);
if (urls == null || urls.isEmpty()) {
return false;
}
final Collection<String> strUrls = new ArrayList<>();
for (final URL url : urls) {
strUrls.add(URLs.toFilePath(url));
}
for (final PersistenceModule persistenceModule : module.getPersistenceModules()) {
final Persistence persistence = persistenceModule.getPersistence();
final String rootUrl;
try {
rootUrl = URLs.toFilePath(new URL(persistenceModule.getRootUrl()));
} catch (final MalformedURLException e) {
continue;
}
for (final PersistenceUnit unit : persistence.getPersistenceUnit()) {
if (unit == value) {
if (strUrls.contains(rootUrl)) {
return true;
}
}
}
}
return false;
}
use of org.apache.openejb.jee.jpa.unit.PersistenceUnit in project tomee by apache.
the class ConfigurationDeployer method configureJpa.
private void configureJpa(final AppModule appModule, final PersistenceUnitDefinition annotation, final IAnnotationFinder finder) {
if (annotation == null) {
return;
}
final String unitName = PropertyPlaceHolderHelper.simpleValue(annotation.unitName());
for (final PersistenceModule module : appModule.getPersistenceModules()) {
for (final PersistenceUnit unit : module.getPersistence().getPersistenceUnit()) {
if (unitName.equals(unit.getName())) {
Logger.getInstance(LogCategory.OPENEJB_STARTUP, ConfigurationDeployer.class).info("Unit[" + unitName + "] overriden by a persistence.xml with root url: " + module.getRootUrl());
return;
}
}
}
final PersistenceUnit unit = new PersistenceUnit();
unit.setName(unitName);
if (!"auto".equals(annotation.jtaDataSource())) {
unit.setJtaDataSource(PropertyPlaceHolderHelper.simpleValue(annotation.jtaDataSource()));
}
if (!"auto".equals(annotation.nonJtaDataSource())) {
unit.setNonJtaDataSource(PropertyPlaceHolderHelper.simpleValue(annotation.nonJtaDataSource()));
}
for (final String prop : annotation.properties()) {
final int equalIndex = prop.indexOf('=');
unit.setProperty(PropertyPlaceHolderHelper.simpleValue(prop.substring(0, equalIndex)), PropertyPlaceHolderHelper.simpleValue(prop.substring(equalIndex + 1, prop.length())));
}
unit.setProperty("openejb.jpa.auto-scan", "true");
if (!"auto".equals(annotation.entitiesPackage())) {
unit.setProperty("openejb.jpa.auto-scan.package", PropertyPlaceHolderHelper.simpleValue(annotation.entitiesPackage()));
}
if (annotation.ddlAuto()) {
unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
unit.setProperty("javax.persistence.schema-generation.database.action", "create");
}
if (annotation.jta()) {
unit.setTransactionType(TransactionType.JTA);
} else {
unit.setTransactionType(TransactionType.RESOURCE_LOCAL);
// otherwise type is forced to JTA
unit.setNonJtaDataSource("autoNonJtaDb");
}
if (!"auto".equals(annotation.provider())) {
unit.setProvider(annotation.provider());
}
unit.setValidationMode(annotation.validationMode());
unit.setSharedCacheMode(annotation.cacheMode());
// we pass after annotation deployer so need to fill it ourself
AnnotationDeployer.doAutoJpa(finder, unit);
final Persistence persistence = new Persistence();
persistence.addPersistenceUnit(unit);
appModule.addPersistenceModule(new PersistenceModule(appModule, "@Configuration#" + unitName, persistence));
}
Aggregations