Example usage for org.hibernate.cfg Configuration addPackage

List of usage examples for org.hibernate.cfg Configuration addPackage

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration addPackage.

Prototype

public Configuration addPackage(String packageName) throws MappingException 

Source Link

Document

Read package-level metadata.

Usage

From source file:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes.
 * @see #setPackagesToScan/* www . j ava2  s . c  o m*/
 */
protected void scanPackages(Configuration config) {
    if (this.packagesToScan != null) {
        Set<String> classNames = new TreeSet<String>();
        Set<String> packageNames = new TreeSet<String>();
        try {
            for (String pkg : this.packagesToScan) {
                String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
                Resource[] resources = this.resourcePatternResolver.getResources(pattern);
                MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                        this.resourcePatternResolver);
                for (Resource resource : resources) {
                    if (resource.isReadable()) {
                        MetadataReader reader = readerFactory.getMetadataReader(resource);
                        String className = reader.getClassMetadata().getClassName();
                        if (matchesEntityTypeFilter(reader, readerFactory)) {
                            classNames.add(className);
                        } else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
                            packageNames.add(
                                    className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new MappingException("Failed to scan classpath for unlisted classes", ex);
        }
        try {
            for (String className : classNames) {
                config.addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
            }
            for (String packageName : packageNames) {
                config.addPackage(packageName);
            }
        } catch (ClassNotFoundException ex) {
            throw new MappingException("Failed to load annotated classes from classpath", ex);
        }
    }
}

From source file:org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.java

License:Apache License

@Override
@SuppressWarnings("rawtypes")
public EntityManagerFactory createContainerEntityManagerFactory(final PersistenceUnitInfo info,
        Map properties) {/*from   ww w . j a  v a 2  s .c o m*/
    return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(info), properties) {
        @Override
        public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) {
            Configuration configuration = super.buildHibernateConfiguration(serviceRegistry);
            if (info instanceof SmartPersistenceUnitInfo) {
                for (String managedPackage : ((SmartPersistenceUnitInfo) info).getManagedPackages()) {
                    configuration.addPackage(managedPackage);
                }
            }
            return configuration;
        }
    }.build();
}

From source file:ru.appliedtech.storage.hibernate.DDLGeneratorUtil.java

License:Open Source License

public static void execute(String dialectClassName, String packageName, String outputFilePath) {
    Configuration configuration = new Configuration();
    configuration.addPackage(packageName);
    configuration.setProperty(Environment.DIALECT, dialectClassName);
    Collection<Class<? extends Object>> classes = getPackageClasses(packageName);
    for (Class<?> entityClass : classes) {
        configuration.addAnnotatedClass(entityClass);
    }/*  w  w w. j av  a2s .  c  om*/
    SchemaExport schemaExport = new SchemaExport(configuration);
    schemaExport.setDelimiter(";");
    schemaExport.setOutputFile(outputFilePath);
    schemaExport.create(true, false);
}