Search in sources :

Example 6 with PersistenceUnitInfo

use of javax.persistence.spi.PersistenceUnitInfo in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExample4.

@Test
public void testExample4() throws Exception {
    SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    DataSource ds = new DriverManagerDataSource();
    builder.bind("java:comp/env/jdbc/MyDB", ds);
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example4.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertNotNull(info);
    assertEquals(1, info.length);
    assertEquals("OrderManagement4", info[0].getPersistenceUnitName());
    assertEquals(1, info[0].getMappingFileNames().size());
    assertEquals("order-mappings.xml", info[0].getMappingFileNames().get(0));
    assertEquals(3, info[0].getManagedClassNames().size());
    assertEquals("com.acme.Order", info[0].getManagedClassNames().get(0));
    assertEquals("com.acme.Customer", info[0].getManagedClassNames().get(1));
    assertEquals("com.acme.Item", info[0].getManagedClassNames().get(2));
    assertTrue("Exclude unlisted should be true when no value.", info[0].excludeUnlistedClasses());
    assertSame(PersistenceUnitTransactionType.RESOURCE_LOCAL, info[0].getTransactionType());
    assertEquals(0, info[0].getProperties().keySet().size());
    builder.clear();
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.Test)

Example 7 with PersistenceUnitInfo

use of javax.persistence.spi.PersistenceUnitInfo in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testJpa2ExcludeUnlisted.

@Test
public void testJpa2ExcludeUnlisted() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-exclude-2.0.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertNotNull(info);
    assertEquals("The number of persistence units is incorrect.", 4, info.length);
    PersistenceUnitInfo noExclude = info[0];
    assertNotNull("noExclude should not be null.", noExclude);
    assertEquals("noExclude name is not correct.", "NoExcludeElement", noExclude.getPersistenceUnitName());
    assertFalse("Exclude unlisted still defaults to false in 2.0.", noExclude.excludeUnlistedClasses());
    PersistenceUnitInfo emptyExclude = info[1];
    assertNotNull("emptyExclude should not be null.", emptyExclude);
    assertEquals("emptyExclude name is not correct.", "EmptyExcludeElement", emptyExclude.getPersistenceUnitName());
    assertTrue("emptyExclude should be true.", emptyExclude.excludeUnlistedClasses());
    PersistenceUnitInfo trueExclude = info[2];
    assertNotNull("trueExclude should not be null.", trueExclude);
    assertEquals("trueExclude name is not correct.", "TrueExcludeElement", trueExclude.getPersistenceUnitName());
    assertTrue("trueExclude should be true.", trueExclude.excludeUnlistedClasses());
    PersistenceUnitInfo falseExclude = info[3];
    assertNotNull("falseExclude should not be null.", falseExclude);
    assertEquals("falseExclude name is not correct.", "FalseExcludeElement", falseExclude.getPersistenceUnitName());
    assertFalse("falseExclude should be false.", falseExclude.excludeUnlistedClasses());
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Test(org.junit.Test)

Example 8 with PersistenceUnitInfo

use of javax.persistence.spi.PersistenceUnitInfo in project spring-framework by spring-projects.

the class PersistenceXmlParsingTests method testExample1.

@Test
public void testExample1() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example1.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertNotNull(info);
    assertEquals(1, info.length);
    assertEquals("OrderManagement", info[0].getPersistenceUnitName());
    assertFalse("Exclude unlisted should default false in 1.0.", info[0].excludeUnlistedClasses());
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Test(org.junit.Test)

Example 9 with PersistenceUnitInfo

use of javax.persistence.spi.PersistenceUnitInfo in project robo4j by Robo4J.

the class PersistenceContextBuilder method build.

public PersistenceContextBuilder build() {
    PersistenceDescriptorFactory persistenceDescriptorFactory = new PersistenceDescriptorFactory();
    PersistenceUnitInfo persistenceUnitInfo = persistenceDescriptorFactory.get(RoboClassLoader.getInstance().getClassLoader(), sourceType, packages);
    PersistenceUnitDescriptor persistenceUnitDescriptor = new PersistenceUnitInfoDescriptor(persistenceUnitInfo);
    EntityManagerFactoryBuilder builder = new EntityManagerFactoryBuilderImpl(persistenceUnitDescriptor, new HashMap<>());
    registeredClasses = persistenceDescriptorFactory.registeredClasses();
    dataSourceContext = new DataSourceProxy(builder.build());
    return this;
}
Also used : PersistenceUnitInfoDescriptor(org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor) EntityManagerFactoryBuilderImpl(org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) EntityManagerFactoryBuilder(org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder) PersistenceDescriptorFactory(com.robo4j.db.sql.jpa.PersistenceDescriptorFactory) PersistenceUnitDescriptor(org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor)

Example 10 with PersistenceUnitInfo

use of javax.persistence.spi.PersistenceUnitInfo in project OpenAttestation by OpenAttestation.

the class PersistenceManager method createEntityManagerFactory.

/**
     * The new form of this method.  The old form simply wraps this one now.
     * Loads persistence.xml from the classpath and creates an entity manager
     * with those settings, which can now include Apache Commons Pool and JDBC.
     * 
     * @param persistenceUnitName
     * @param properties
     * @return 
     */
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Properties jpaProperties) {
    log.debug("Loading database driver {} for persistence unit {}", new String[] { jpaProperties.getProperty("javax.persistence.jdbc.driver"), persistenceUnitName });
    try {
        Class.forName(jpaProperties.getProperty("javax.persistence.jdbc.driver"));
    } catch (ClassNotFoundException ex) {
        log.error("Cannot load JDBC Driver for persistence unit", ex);
    }
    PersistenceUnitInfo persistenceUnitInfo = null;
    try {
        persistenceUnitInfo = getPersistenceUnitInfo(persistenceUnitName, jpaProperties);
    } catch (IOException e) {
        throw new PersistenceException("Cannot load PersistenceUnit named " + persistenceUnitName, e);
    }
    if (persistenceUnitInfo == null) {
        throw new PersistenceException("Cannot find PersistenceUnit named " + persistenceUnitName);
    }
    EntityManagerFactory emf = null;
    PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
    List<PersistenceProvider> providers = resolver.getPersistenceProviders();
    // check if we have the requested provider
    if (persistenceUnitInfo.getPersistenceProviderClassName() != null) {
        log.info("Looking for specific JPA provider: {}", persistenceUnitInfo.getPersistenceProviderClassName());
        for (PersistenceProvider provider : providers) {
            log.info("Looking at provider: {}", provider.getClass().getName());
            if (provider.getClass().getName().equals(persistenceUnitInfo.getPersistenceProviderClassName())) {
                // important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically:  remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
                emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
                if (emf != null) {
                    log.info("Found requested persistence provider");
                    return emf;
                }
            }
        }
    }
    // check if any other provider can accomodate the persistence unit
    log.info("Looking for any compatible JPA provider");
    for (PersistenceProvider provider : providers) {
        log.info("Looking at provider: {}", provider.getClass().getName());
        // important: must use the properties as returned by the persistenceUnitInfo because it may have altered them... specifically:  remove user and password entries after creating datasource to force eclipselink to call getConnection() instead of getConnection(user,password)
        emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, persistenceUnitInfo.getProperties());
        if (emf != null) {
            log.info("Found compatible persistence provider");
            return emf;
        }
    }
    throw new PersistenceException("No Persistence provider for EntityManager named " + persistenceUnitName);
}
Also used : PersistenceProviderResolver(javax.persistence.spi.PersistenceProviderResolver) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceProvider(javax.persistence.spi.PersistenceProvider) PersistenceException(javax.persistence.PersistenceException) IOException(java.io.IOException) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo)

Aggregations

PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)14 Test (org.junit.Test)10 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)10 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)9 ClassPathResource (org.springframework.core.io.ClassPathResource)4 DataSource (javax.sql.DataSource)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 PersistenceDescriptorFactory (com.robo4j.db.sql.jpa.PersistenceDescriptorFactory)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 PersistenceException (javax.persistence.PersistenceException)1 PersistenceProvider (javax.persistence.spi.PersistenceProvider)1 PersistenceProviderResolver (javax.persistence.spi.PersistenceProviderResolver)1 EntityManagerFactoryBuilderImpl (org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl)1 PersistenceUnitInfoDescriptor (org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor)1 EntityManagerFactoryBuilder (org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder)1 PersistenceUnitDescriptor (org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor)1 MapDataSourceLookup (org.springframework.jdbc.datasource.lookup.MapDataSourceLookup)1