Search in sources :

Example 6 with PersistenceUnitInfo

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

the class PersistenceXmlParsingTests method testExample6.

@Test
public void testExample6() throws Exception {
    PersistenceUnitReader reader = new PersistenceUnitReader(new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
    String resource = "/org/springframework/orm/jpa/persistence-example6.xml";
    PersistenceUnitInfo[] info = reader.readPersistenceUnitInfos(resource);
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("pu");
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 7 with PersistenceUnitInfo

use of jakarta.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);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement4");
    assertThat(info[0].getMappingFileNames().size()).isEqualTo(1);
    assertThat(info[0].getMappingFileNames().get(0)).isEqualTo("order-mappings.xml");
    assertThat(info[0].getManagedClassNames().size()).isEqualTo(3);
    assertThat(info[0].getManagedClassNames().get(0)).isEqualTo("com.acme.Order");
    assertThat(info[0].getManagedClassNames().get(1)).isEqualTo("com.acme.Customer");
    assertThat(info[0].getManagedClassNames().get(2)).isEqualTo("com.acme.Item");
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should be true when no value.").isTrue();
    assertThat(info[0].getTransactionType()).isSameAs(PersistenceUnitTransactionType.RESOURCE_LOCAL);
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    builder.clear();
}
Also used : SimpleNamingContextBuilder(org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) Test(org.junit.jupiter.api.Test)

Example 8 with PersistenceUnitInfo

use of jakarta.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 && this.defaultPersistenceUnitName != null) {
        return obtainPersistenceUnitInfo(this.defaultPersistenceUnitName);
    }
    PersistenceUnitInfo pui = this.persistenceUnitInfos.values().iterator().next();
    this.persistenceUnitInfos.clear();
    return pui;
}
Also used : PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo)

Example 9 with PersistenceUnitInfo

use of jakarta.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);
    assertThat(info).isNotNull();
    assertThat(info.length).as("The number of persistence units is incorrect.").isEqualTo(4);
    PersistenceUnitInfo noExclude = info[0];
    assertThat(noExclude).as("noExclude should not be null.").isNotNull();
    assertThat(noExclude.getPersistenceUnitName()).as("noExclude name is not correct.").isEqualTo("NoExcludeElement");
    assertThat(noExclude.excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
    PersistenceUnitInfo emptyExclude = info[1];
    assertThat(emptyExclude).as("emptyExclude should not be null.").isNotNull();
    assertThat(emptyExclude.getPersistenceUnitName()).as("emptyExclude name is not correct.").isEqualTo("EmptyExcludeElement");
    assertThat(emptyExclude.excludeUnlistedClasses()).as("emptyExclude should be true.").isTrue();
    PersistenceUnitInfo trueExclude = info[2];
    assertThat(trueExclude).as("trueExclude should not be null.").isNotNull();
    assertThat(trueExclude.getPersistenceUnitName()).as("trueExclude name is not correct.").isEqualTo("TrueExcludeElement");
    assertThat(trueExclude.excludeUnlistedClasses()).as("trueExclude should be true.").isTrue();
    PersistenceUnitInfo falseExclude = info[3];
    assertThat(falseExclude).as("falseExclude should not be null.").isNotNull();
    assertThat(falseExclude.getPersistenceUnitName()).as("falseExclude name is not correct.").isEqualTo("FalseExcludeElement");
    assertThat(falseExclude.excludeUnlistedClasses()).as("falseExclude should be false.").isFalse();
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Example 10 with PersistenceUnitInfo

use of jakarta.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);
    assertThat(info).isNotNull();
    assertThat(info.length).isEqualTo(1);
    assertThat(info[0].getPersistenceUnitName()).isEqualTo("OrderManagement2");
    assertThat(info[0].getMappingFileNames().size()).isEqualTo(1);
    assertThat(info[0].getMappingFileNames().get(0)).isEqualTo("mappings.xml");
    assertThat(info[0].getProperties().keySet().size()).isEqualTo(0);
    assertThat(info[0].excludeUnlistedClasses()).as("Exclude unlisted should default false in 1.0.").isFalse();
}
Also used : JndiDataSourceLookup(org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PersistenceUnitInfo(jakarta.persistence.spi.PersistenceUnitInfo) Test(org.junit.jupiter.api.Test)

Aggregations

PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)13 Test (org.junit.jupiter.api.Test)11 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)10 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)9 ClassPathResource (org.springframework.core.io.ClassPathResource)4 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 LinkedHashMap (java.util.LinkedHashMap)1 Properties (java.util.Properties)1 EntityManagerFactoryBuilderImpl (org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl)1 PersistenceUnitInfoDescriptor (org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor)1 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 MapDataSourceLookup (org.springframework.jdbc.datasource.lookup.MapDataSourceLookup)1 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)1