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();
}
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());
}
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());
}
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;
}
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);
}
Aggregations