org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo Java Examples

The following examples show how to use org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JPAPersistenceUnitPostProcessor.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Show PUI infos
 * 
 * @param pui
 *            PersistenceUnitInfo
 */
private void dumpPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
{
    _Log.debug( "Dumping content for PersistenceUnitInfo of " + pui.getPersistenceUnitName( ) );

    _Log.debug( "** getTransactionType : " + pui.getTransactionType( ) );
    _Log.debug( "** getPersistenceProviderClassName : " + pui.getPersistenceProviderClassName( ) );
    _Log.debug( "** getPersistenceProviderPackageName : " + pui.getPersistenceProviderPackageName( ) );
    _Log.debug( "** getPersistenceUnitName : " + pui.getPersistenceUnitName( ) );
    _Log.debug( "** getPersistenceXMLSchemaVersion : " + pui.getPersistenceXMLSchemaVersion( ) );
    _Log.debug( "** getJtaDataSource : " + pui.getJtaDataSource( ) );
    _Log.debug( "** getManagedClassNames : " + pui.getManagedClassNames( ) );
    _Log.debug( "** getMappingFileNames : " + pui.getMappingFileNames( ) );
    _Log.debug( "** getNonJtaDataSource : " + pui.getNonJtaDataSource( ) );
    _Log.debug( "** getPersistenceUnitRootUrl :" + pui.getPersistenceUnitRootUrl( ) );
    _Log.debug( "** getProperties : " + pui.getProperties( ) );
}
 
Example #2
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
	Object testEntity = new Object();

	// This one's for the tx (shared)
	EntityManager sharedEm = mock(EntityManager.class);
	given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());

	// This is the application-specific one
	EntityManager mockEm = mock(EntityManager.class);

	given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);

	LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
	MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
	pui.setTransactionType(PersistenceUnitTransactionType.JTA);

	JpaTransactionManager jpatm = new JpaTransactionManager();
	jpatm.setEntityManagerFactory(cefb.getObject());

	TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());

	EntityManagerFactory emf = cefb.getObject();
	assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());

	assertNotSame("EMF must be proxied", mockEmf, emf);
	EntityManager em = emf.createEntityManager();
	em.joinTransaction();
	assertFalse(em.contains(testEntity));

	jpatm.commit(txStatus);

	cefb.destroy();

	verify(mockEm).joinTransaction();
	verify(mockEm).contains(testEntity);
	verify(mockEmf).close();
}
 
Example #3
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
	Object testEntity = new Object();

	// This one's for the tx (shared)
	EntityManager sharedEm = mock(EntityManager.class);
	given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());

	// This is the application-specific one
	EntityManager mockEm = mock(EntityManager.class);

	given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);

	LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
	MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
	pui.setTransactionType(PersistenceUnitTransactionType.JTA);

	JpaTransactionManager jpatm = new JpaTransactionManager();
	jpatm.setEntityManagerFactory(cefb.getObject());

	TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());

	EntityManagerFactory emf = cefb.getObject();
	assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());

	assertNotSame("EMF must be proxied", mockEmf, emf);
	EntityManager em = emf.createEntityManager();
	em.joinTransaction();
	assertFalse(em.contains(testEntity));

	jpatm.commit(txStatus);

	cefb.destroy();

	verify(mockEm).joinTransaction();
	verify(mockEm).contains(testEntity);
	verify(mockEmf).close();
}
 
Example #4
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
	Object testEntity = new Object();

	// This one's for the tx (shared)
	EntityManager sharedEm = mock(EntityManager.class);
	given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());

	// This is the application-specific one
	EntityManager mockEm = mock(EntityManager.class);

	given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);

	LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
	MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
	pui.setTransactionType(PersistenceUnitTransactionType.JTA);

	JpaTransactionManager jpatm = new JpaTransactionManager();
	jpatm.setEntityManagerFactory(cefb.getObject());

	TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());

	EntityManagerFactory emf = cefb.getObject();
	assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());

	assertNotSame("EMF must be proxied", mockEmf, emf);
	EntityManager em = emf.createEntityManager();
	em.joinTransaction();
	assertFalse(em.contains(testEntity));

	jpatm.commit(txStatus);

	cefb.destroy();

	verify(mockEm).joinTransaction();
	verify(mockEm).contains(testEntity);
	verify(mockEmf).close();
}
 
Example #5
Source File: JpaUnitPostProcessor.java    From zstack with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo unit) {
    /*
    unit.addProperty("hibernate.connection.username", getDbUser());
    unit.addProperty("hibernate.connection.password", getDbPassword());
    String jdbcUrl = String.format("jdbc:mysql://%s/%s", getDbHost(), getDbName());
    unit.addProperty("hibernate.connection.url", jdbcUrl);
    unit.addProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    unit.addProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
    unit.addProperty("org.jboss.logging.provider", "log4j2");
    */
}
 
Example #6
Source File: AddablePersistenceUnit.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    List<AdditionalHibernateMappings> units = new ArrayList<>();
    String[] unitNames = applicationContext.getBeanNamesForType(AdditionalHibernateMappings.class, false, false);

    for (String name : unitNames) {
        units.add((AdditionalHibernateMappings) applicationContext.getBean(name));
    }

    Collections.sort(units);

    units.forEach(u -> u.processAdditionalUnit(pui));
}
 
Example #7
Source File: SakaiPersistenceUnitManager.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public void preparePersistenceUnitInfos() {
    MutablePersistenceUnitInfo pui = new MutablePersistenceUnitInfo();
    pui.setPersistenceUnitName(defaultPersistenceUnitName);
    pui.setExcludeUnlistedClasses(true);

    if (pui.getJtaDataSource() == null) {
        pui.setJtaDataSource(jtaDataSource);
    }
    if (pui.getNonJtaDataSource() == null) {
        pui.setNonJtaDataSource(dataSource);
    }

    // override default UUIDGenerator with AssignableUUIDGenerator
    pui.addProperty(org.hibernate.jpa.AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER, SakaiIdentifierGeneratorProvider.class.getName());
    AssignableUUIDGenerator.setServerConfigurationService(serverConfigurationService);

    postProcessPersistenceUnitInfo(pui);

    Boolean autoddl = serverConfigurationService.getBoolean("auto.ddl", true);
    String hbm2ddl = serverConfigurationService.getString(AvailableSettings.HBM2DDL_AUTO);
    if (autoddl) {
        // if sakai auto.ddl is on then this needs to be set to update
        hbm2ddl = "update";
    }
    pui.getProperties().setProperty(AvailableSettings.HBM2DDL_AUTO, StringUtils.defaultString(hbm2ddl));

    defaultPersistenceUnitInfo = pui;
}
 
Example #8
Source File: MultiJarAwarePersistenceUnitPostProcessor.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessPersistenceUnitInfo(final MutablePersistenceUnitInfo pui) {
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));

    scanner.findCandidateComponents(AbstractEntity.class.getPackage().getName()).forEach(bd -> {
        LOG.debug("Adding JPA entity {}", bd.getBeanClassName());
        pui.addManagedClassName(Objects.requireNonNull(bd.getBeanClassName()));
    });
}
 
Example #9
Source File: JPAPersistenceUnitPostProcessor.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Scans for *.orm.xml and adds Entites from classpath.
 *
 * @param pui
 *            the pui
 */
@Override
public void postProcessPersistenceUnitInfo( MutablePersistenceUnitInfo pui )
{
    _Log.info( "Scanning for JPA orm.xml files" );

    for ( File ormFile : getListORMFiles( ) )
    {
        String ormAbsolutePath = ormFile.getAbsolutePath( );
        _Log.info( "Found ORM file : " + ormAbsolutePath );
        pui.addMappingFileName( ormAbsolutePath.substring( ormAbsolutePath.indexOf( CLASSPATH_PATH_IDENTIFIER ) ) );
    }

    _Log.info( "Scanning for JPA entities..." );

    Set<String> entityClasses = AnnotationUtil.find( Entity.class.getName( ) );
    entityClasses.addAll( AnnotationUtil.find( Embeddable.class.getName( ) ) );
    entityClasses.addAll( AnnotationUtil.find( MappedSuperclass.class.getName( ) ) );

    for ( String strClass : entityClasses )
    {
        _Log.info( "Found entity class : " + strClass );

        if ( !pui.getManagedClassNames( ).contains( strClass ) )
        {
            pui.addManagedClassName( strClass );
        }
    }

    if ( _Log.isDebugEnabled( ) )
    {
        dumpPersistenceUnitInfo( pui );
    }
}
 
Example #10
Source File: AddablePersistenceUnit.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    List<AdditionalHibernateMappings> units = new ArrayList<>();
    String[] unitNames = applicationContext.getBeanNamesForType(AdditionalHibernateMappings.class, false, false);

    for (String name : unitNames) {
        units.add((AdditionalHibernateMappings) applicationContext.getBean(name));
    }

    Collections.sort(units);

    units.forEach(u -> u.processAdditionalUnit(pui));
}
 
Example #11
Source File: SakaiPersistenceUnitManager.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public void preparePersistenceUnitInfos() {
    MutablePersistenceUnitInfo pui = new MutablePersistenceUnitInfo();
    pui.setPersistenceUnitName(defaultPersistenceUnitName);
    pui.setExcludeUnlistedClasses(true);

    if (pui.getJtaDataSource() == null) {
        pui.setJtaDataSource(jtaDataSource);
    }
    if (pui.getNonJtaDataSource() == null) {
        pui.setNonJtaDataSource(dataSource);
    }

    // override default UUIDGenerator with AssignableUUIDGenerator
    pui.addProperty(org.hibernate.jpa.AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER, SakaiIdentifierGeneratorProvider.class.getName());
    AssignableUUIDGenerator.setServerConfigurationService(serverConfigurationService);

    postProcessPersistenceUnitInfo(pui);

    Boolean autoddl = serverConfigurationService.getBoolean("auto.ddl", true);
    String hbm2ddl = serverConfigurationService.getString(AvailableSettings.HBM2DDL_AUTO);
    if (autoddl) {
        // if sakai auto.ddl is on then this needs to be set to update
        hbm2ddl = "update";
    }
    pui.getProperties().setProperty(AvailableSettings.HBM2DDL_AUTO, StringUtils.defaultString(hbm2ddl));

    defaultPersistenceUnitInfo = pui;
}
 
Example #12
Source File: KradEntityManagerFactoryBean.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
      * {@inheritDoc}
      */
     @Override
     public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
         pui.setExcludeUnlistedClasses(DEFAULT_EXCLUDE_UNLISTED_CLASSES);
processConverterPackages(pui);
         for (String managedClassName : getManagedClassNames()) {
             pui.addManagedClassName(managedClassName);
         }
     }
 
Example #13
Source File: KradEntityManagerFactoryBean.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
       * Determines whether the managed classes contain {@link Converter} annotations and adds them if necessary.
       *
       * @param pui the list of current list of managed classes.
       */
private void processConverterPackages(MutablePersistenceUnitInfo pui) {
	if (converterPackageNames != null) {
		for (String converterPackage : converterPackageNames) {
			// Code below lifted and modified from Spring's DefaultPersistenceUnitManager
			try {
				String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
						+ ClassUtils.convertClassNameToResourcePath(converterPackage)
						+ ENTITY_CLASS_RESOURCE_PATTERN;
				if (LOG.isInfoEnabled()) {
					LOG.info(getPersistenceUnitName() + ": Scanning for JPA @Converter annotations in: "
							+ pattern);
				}
				Resource[] resources = this.resourcePatternResolver.getResources(pattern);
				MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
						this.resourcePatternResolver);
				for (Resource resource : resources) {
					if (!resource.isReadable()) {
						continue;
					}
					if (LOG.isDebugEnabled()) {
						LOG.debug(getPersistenceUnitName() + ": Found Matching Resource: " + resource);
					}
					MetadataReader reader = readerFactory.getMetadataReader(resource);
					String className = reader.getClassMetadata().getClassName();
					if (!pui.getManagedClassNames().contains(className)
							&& converterAnnotationTypeFilter.match(reader, readerFactory)) {
						pui.addManagedClassName(className);
						if (LOG.isDebugEnabled()) {
							LOG.debug(getPersistenceUnitName()
									+ ": Registering Converter in JPA Persistence Unit: " + className);
						}
					}
				}
			} catch (IOException ex) {
				throw new PersistenceException("Failed to scan classpath converters in package: "
						+ converterPackage, ex);
			}
		}
	}
}
 
Example #14
Source File: KradEclipseLinkEntityManagerFactoryBeanTest.java    From rice with Educational Community License v2.0 4 votes vote down vote up
@Override
public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
    pui.getManagedClassNames().add(TestEntity3.class.getName());
}
 
Example #15
Source File: AdditionalHibernateMappings.java    From sakai with Educational Community License v2.0 votes vote down vote up
void processAdditionalUnit(MutablePersistenceUnitInfo pui); 
Example #16
Source File: AdditionalHibernateMappings.java    From sakai with Educational Community License v2.0 votes vote down vote up
void processAdditionalUnit(MutablePersistenceUnitInfo pui);