Search in sources :

Example 31 with Persistence

use of org.apache.openejb.jee.jpa.unit.Persistence 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));
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit)

Example 32 with Persistence

use of org.apache.openejb.jee.jpa.unit.Persistence 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<String>();
    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;
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) MalformedURLException(java.net.MalformedURLException) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) ArrayList(java.util.ArrayList) Collection(java.util.Collection) URL(java.net.URL)

Example 33 with Persistence

use of org.apache.openejb.jee.jpa.unit.Persistence in project tomee by apache.

the class PersistenceXmlTest method testPersistenceVersion2.

/**
 * @throws Exception
 */
public void testPersistenceVersion2() throws Exception {
    final JAXBContext ctx = JAXBContextFactory.newInstance(Persistence.class);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    final URL resource = this.getClass().getClassLoader().getResource("persistence_2.0-example.xml");
    final InputStream in = resource.openStream();
    final java.lang.String expected = readContent(in);
    final Persistence element = (Persistence) unmarshaller.unmarshal(new ByteArrayInputStream(expected.getBytes()));
    unmarshaller.setEventHandler(new TestValidationEventHandler());
    System.out.println("unmarshalled");
    final Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(element, baos);
    final String actual = new String(baos.toByteArray());
    final Diff myDiff = new Diff(expected, actual);
    myDiff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
    assertTrue("Files are similar " + myDiff, myDiff.similar());
}
Also used : Marshaller(javax.xml.bind.Marshaller) Diff(org.custommonkey.xmlunit.Diff) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ElementNameAndAttributeQualifier(org.custommonkey.xmlunit.ElementNameAndAttributeQualifier) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 34 with Persistence

use of org.apache.openejb.jee.jpa.unit.Persistence in project tomee by apache.

the class DataSourceDefinitionGlobalJPATest method persistence.

@Module
public Persistence persistence() {
    final org.apache.openejb.jee.jpa.unit.PersistenceUnit unit = new org.apache.openejb.jee.jpa.unit.PersistenceUnit("jpa-global-dsdef-unit");
    unit.addClass(IdEntity.class);
    unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
    unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
    unit.setJtaDataSource("java:app/foo");
    unit.setExcludeUnlistedClasses(true);
    final Persistence persistence = new Persistence(unit);
    persistence.setVersion("2.0");
    return persistence;
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(javax.persistence.PersistenceUnit) Module(org.apache.openejb.testing.Module)

Example 35 with Persistence

use of org.apache.openejb.jee.jpa.unit.Persistence 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;
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) Module(org.apache.openejb.testing.Module)

Aggregations

Persistence (org.apache.openejb.jee.jpa.unit.Persistence)43 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)34 Module (org.apache.openejb.testing.Module)18 AppInfo (org.apache.openejb.assembler.classic.AppInfo)10 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)9 EjbJar (org.apache.openejb.jee.EjbJar)7 WebApp (org.apache.openejb.jee.WebApp)7 URL (java.net.URL)5 ArrayList (java.util.ArrayList)5 Assembler (org.apache.openejb.assembler.classic.Assembler)5 Properties (java.util.Properties)4 PersistenceUnit (javax.persistence.PersistenceUnit)4 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)4 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)4 File (java.io.File)3 PersistenceUnitInfo (org.apache.openejb.assembler.classic.PersistenceUnitInfo)3 StatelessBean (org.apache.openejb.jee.StatelessBean)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2