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

The following examples show how to use org.springframework.orm.jpa.persistenceunit.SmartPersistenceUnitInfo. 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: LocalContainerEntityManagerFactoryBean.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws PersistenceException {
	PersistenceUnitManager managerToUse = this.persistenceUnitManager;
	if (this.persistenceUnitManager == null) {
		this.internalPersistenceUnitManager.afterPropertiesSet();
		managerToUse = this.internalPersistenceUnitManager;
	}

	this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse);
	JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
	if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo) {
		String rootPackage = jpaVendorAdapter.getPersistenceProviderRootPackage();
		if (rootPackage != null) {
			((SmartPersistenceUnitInfo) this.persistenceUnitInfo).setPersistenceProviderPackageName(rootPackage);
		}
	}

	super.afterPropertiesSet();
}
 
Example #2
Source File: LocalContainerEntityManagerFactoryBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws PersistenceException {
	PersistenceUnitManager managerToUse = this.persistenceUnitManager;
	if (this.persistenceUnitManager == null) {
		this.internalPersistenceUnitManager.afterPropertiesSet();
		managerToUse = this.internalPersistenceUnitManager;
	}

	this.persistenceUnitInfo = determinePersistenceUnitInfo(managerToUse);
	JpaVendorAdapter jpaVendorAdapter = getJpaVendorAdapter();
	if (jpaVendorAdapter != null && this.persistenceUnitInfo instanceof SmartPersistenceUnitInfo) {
		String rootPackage = jpaVendorAdapter.getPersistenceProviderRootPackage();
		if (rootPackage != null) {
			((SmartPersistenceUnitInfo) this.persistenceUnitInfo).setPersistenceProviderPackageName(rootPackage);
		}
	}

	super.afterPropertiesSet();
}
 
Example #3
Source File: EclipselinkDdlGenerationMojo.java    From eclipselink-maven-plugin with Apache License 2.0 6 votes vote down vote up
public void generateSchema() throws MojoFailureException
{
    final Map<String, Object> cfg = buildCfg();
    String[] allBasePackages = this.getBasePackages();
    getLog().info("Using base packages " + StringUtils.arrayToDelimitedString(allBasePackages, ", "));
    final PersistenceProvider provider = new PersistenceProvider();
    final DefaultPersistenceUnitManager manager = new DefaultPersistenceUnitManager();
    manager.setDefaultPersistenceUnitRootLocation(null);
    manager.setDefaultPersistenceUnitName("default");
    manager.setPackagesToScan(allBasePackages);
    final String[] zeroPULocations = new String[]{};
    manager.setPersistenceXmlLocations(zeroPULocations);
    manager.afterPropertiesSet();

    final SmartPersistenceUnitInfo puInfo = (SmartPersistenceUnitInfo) manager.obtainDefaultPersistenceUnitInfo();
    puInfo.setPersistenceProviderPackageName(provider.getClass().getName());
    getLog().info("Entities found : " + puInfo.getManagedClassNames().size());
    getLog().debug("Managed class names:\n    * " + StringUtils.collectionToDelimitedString(puInfo.getManagedClassNames(), "\n    * "));
    puInfo.getProperties().putAll(cfg);
    provider.generateSchema(new DelegatingPuInfo(puInfo), cfg);
}
 
Example #4
Source File: SpringHibernateJpaPersistenceProvider.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	final List<String> mergedClassesAndPackages = new ArrayList<>(info.getManagedClassNames());
	if (info instanceof SmartPersistenceUnitInfo) {
		mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
	}
	return new EntityManagerFactoryBuilderImpl(
			new PersistenceUnitInfoDescriptor(info) {
				@Override
				public List<String> getManagedClassNames() {
					return mergedClassesAndPackages;
				}
			}, properties).build();
}
 
Example #5
Source File: SpringHibernateJpaPersistenceProvider.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	final List<String> mergedClassesAndPackages = new ArrayList<>(info.getManagedClassNames());
	if (info instanceof SmartPersistenceUnitInfo) {
		mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
	}
	return new EntityManagerFactoryBuilderImpl(
			new PersistenceUnitInfoDescriptor(info) {
				@Override
				public List<String> getManagedClassNames() {
					return mergedClassesAndPackages;
				}
			}, properties).build();
}
 
Example #6
Source File: SpringHibernateJpaPersistenceProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	final List<String> mergedClassesAndPackages = new ArrayList<String>(info.getManagedClassNames());
	if (info instanceof SmartPersistenceUnitInfo) {
		mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
	}
	return new EntityManagerFactoryBuilderImpl(
			new PersistenceUnitInfoDescriptor(info) {
				@Override
				public List<String> getManagedClassNames() {
					return mergedClassesAndPackages;
				}
			}, properties).build();
}
 
Example #7
Source File: SpringHibernateEjbPersistenceProvider.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	Ejb3Configuration cfg = new Ejb3Configuration();
	if (info instanceof SmartPersistenceUnitInfo) {
		for (String managedPackage : ((SmartPersistenceUnitInfo) info).getManagedPackages()) {
			cfg.addPackage(managedPackage);
		}
	}
	Ejb3Configuration configured = cfg.configure(info, properties);
	return (configured != null ? configured.buildEntityManagerFactory() : null);
}
 
Example #8
Source File: SpringHibernateEjbPersistenceProvider.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	Ejb3Configuration cfg = new Ejb3Configuration();
	if (info instanceof SmartPersistenceUnitInfo) {
		for (String managedPackage : ((SmartPersistenceUnitInfo) info).getManagedPackages()) {
			cfg.addPackage(managedPackage);
		}
	}
	Ejb3Configuration configured = cfg.configure(info, properties);
	return (configured != null ? configured.buildEntityManagerFactory() : null);
}
 
Example #9
Source File: SpringHibernateJpaPersistenceProvider.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
	final List<String> mergedClassesAndPackages = new ArrayList<String>(info.getManagedClassNames());
	if (info instanceof SmartPersistenceUnitInfo) {
		mergedClassesAndPackages.addAll(((SmartPersistenceUnitInfo) info).getManagedPackages());
	}
	return new EntityManagerFactoryBuilderImpl(
			new PersistenceUnitInfoDescriptor(info) {
				@Override
				public List<String> getManagedClassNames() {
					return mergedClassesAndPackages;
				}
			}, properties).build();
}
 
Example #10
Source File: JpaSchemaGeneratorMojo.java    From jpa-schema-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void generate() throws Exception {
    Map<String, Object> map = JpaSchemaGeneratorUtils.buildProperties(this);
    if (getVendor() == null) {
        // with persistence.xml
        Persistence.generateSchema(this.persistenceUnitName, map);
    } else {
        PersistenceProvider provider = getProviderClass().newInstance();
        List<String> packages = getPackageToScan();
        if (packages.isEmpty()) {
            throw new IllegalArgumentException("packageToScan is required on xml-less mode.");
        }

        DefaultPersistenceUnitManager manager = new DefaultPersistenceUnitManager();
        manager.setDefaultPersistenceUnitName(getPersistenceUnitName());
        manager.setPackagesToScan(packages.toArray(new String[packages.size()]));
        // issue #22
        Field persistenceXmlLocations = manager.getClass().getDeclaredField("persistenceXmlLocations");
        persistenceXmlLocations.setAccessible(true);
        persistenceXmlLocations.set(manager, new String[0]);
        manager.afterPropertiesSet();

        SmartPersistenceUnitInfo info = (SmartPersistenceUnitInfo) manager.obtainDefaultPersistenceUnitInfo();
        info.setPersistenceProviderPackageName(provider.getClass().getName());
        info.getProperties().putAll(map);

        // Path persistenceXml = null;
        /* @formatter:off */
        // if (Vendor.datanucleus.equals(getVendor())) {
        // // datanucleus must need persistence.xml
        // Path path = Paths.get(project.getBuild().getOutputDirectory(), "META-INF");
        // persistenceXml = Files.createTempFile(path, "persistence-", ".xml");
        // try (BufferedWriter writer = Files.newBufferedWriter(persistenceXml, StandardCharsets.UTF_8)) {
        // PrintWriter out = new PrintWriter(writer);
        // out.println("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        // out.println("<persistence version=\"2.1\"");
        // out.println(" xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\"
        // xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
        // out.println(" xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/persistence
        // http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd\">");
        // out.printf(" <persistence-unit name=\"%s\" transaction-type=\"RESOURCE_LOCAL\">\n",
        // info.getPersistenceUnitName());
        // out.println(" <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>");
        // out.println(" <exclude-unlisted-classes>false</exclude-unlisted-classes>");
        // out.println(" </persistence-unit>");
        // out.println("</persistence>");
        // }
        // map.put(PropertyNames.PROPERTY_PERSISTENCE_XML_FILENAME, persistenceXml.toAbsolutePath().toString());
        // // datanucleus does not support execution order...
        // map.remove(PersistenceUnitProperties.SCHEMA_GENERATION_CREATE_SOURCE);
        // map.remove(PersistenceUnitProperties.SCHEMA_GENERATION_DROP_SOURCE);
        // }
        /* @formatter:on */

        try {
            provider.generateSchema(info, map);
        } finally {
            // if (persistenceXml != null) {
            // Files.delete(persistenceXml);
            // }
        }
    }
}