Search in sources :

Example 11 with PersistenceUnitInfo

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

the class PersistenceXmlParsingTests method testExample2.

@Test
public void testExample2() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example2.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertNotNull(info);
    assertEquals(1, info.length);
    assertEquals("OrderManagement2", info[0].getPersistenceUnitName());
    assertEquals(1, info[0].getMappingFileNames().size());
    assertEquals("mappings.xml", info[0].getMappingFileNames().get(0));
    assertEquals(0, info[0].getProperties().keySet().size());
    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 12 with PersistenceUnitInfo

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

the class PersistenceXmlParsingTests method testMetaInfCase.

@Test
public void testMetaInfCase() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/META-INF/persistence.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertNotNull(info);
    assertEquals(1, info.length);
    assertEquals("OrderManagement", info[0].getPersistenceUnitName());
    assertEquals(2, info[0].getJarFileUrls().size());
    assertEquals(new ClassPathResource("order.jar").getURL(), info[0].getJarFileUrls().get(0));
    assertEquals(new ClassPathResource("order-supplemental.jar").getURL(), info[0].getJarFileUrls().get(1));
    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) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 13 with PersistenceUnitInfo

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

the class PersistenceXmlParsingTests method testJpa1ExcludeUnlisted.

@Test
public void testJpa1ExcludeUnlisted() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-exclude-1.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 should default false in 1.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 14 with PersistenceUnitInfo

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

the class DefaultPersistenceUnitManager method obtainDefaultPersistenceUnitInfo.

@Override
public PersistenceUnitInfo obtainDefaultPersistenceUnitInfo() {
    if (this.persistenceUnitInfoNames.isEmpty()) {
        throw new IllegalStateException("No persistence units parsed from " + ObjectUtils.nullSafeToString(this.persistenceXmlLocations));
    }
    if (this.persistenceUnitInfos.isEmpty()) {
        throw new IllegalStateException("All persistence units from " + ObjectUtils.nullSafeToString(this.persistenceXmlLocations) + " already obtained");
    }
    if (this.persistenceUnitInfos.size() > 1) {
        return obtainPersistenceUnitInfo(this.defaultPersistenceUnitName);
    }
    PersistenceUnitInfo pui = this.persistenceUnitInfos.values().iterator().next();
    this.persistenceUnitInfos.clear();
    return pui;
}
Also used : 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