Example usage for org.hibernate MappingException MappingException

List of usage examples for org.hibernate MappingException MappingException

Introduction

In this page you can find the example usage for org.hibernate MappingException MappingException.

Prototype

public MappingException(String message, Throwable cause) 

Source Link

Document

Constructs a MappingException using the given information.

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType.java

License:Apache License

@SuppressWarnings("unchecked")
public void setParameterValues(Properties properties) {
    try {// w w  w .j  a v  a2 s. c  o m
        enumClass = (Class<? extends Enum<?>>) Thread.currentThread().getContextClassLoader()
                .loadClass((String) properties.get(PARAM_ENUM_CLASS));
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Building ID-mapping for Enum Class %s", enumClass.getName()));
        }
        bidiMap = getBidiEnumMap(enumClass);
        type = (AbstractStandardBasicType<?>) typeResolver.basic(bidiMap.keyType.getName());
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Mapped Basic Type is %s", type));
        }
        sqlTypes = type.sqlTypes(null);
    } catch (Exception e) {
        throw new MappingException("Error mapping Enum Class using IdentifierEnumType", e);
    }
}

From source file:org.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes, registering them
 * as annotated classes with this {@code Configuration}.
 * @param packagesToScan one or more Java package names
 * @throws HibernateException if scanning fails for any reason
 *///from   www  .  j  a va2  s . c  o m
public void scanPackages(String... packagesToScan) throws HibernateException {
    try {
        for (String pkg : packagesToScan) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
            Resource[] resources = resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    if (matchesFilter(reader, readerFactory)) {
                        addAnnotatedClasses(resourcePatternResolver.getClassLoader().loadClass(className));
                    }
                }
            }
        }
    } catch (IOException ex) {
        throw new MappingException("Failed to scan classpath for unlisted classes", ex);
    } catch (ClassNotFoundException ex) {
        throw new MappingException("Failed to load annotated classes from classpath", ex);
    }
}

From source file:org.jboss.tools.hibernate3_6.ConfigurationFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
private Configuration loadConfigurationXML(Configuration localCfg, boolean includeMappings,
        EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;/*from   w  w  w  .  j  a va 2s. c om*/
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        String resourceName = "<unknown>"; //$NON-NLS-1$
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(
                        ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            resourceName = "/hibernate.cfg.xml"; //$NON-NLS-1$
            if (checkHibernateResoureExistence(resourceName)) {
                stream = ConfigHelper.getResourceAsStream(resourceName); // simulate hibernate's
                // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<Throwable> errors = new ArrayList<Throwable>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration,
                        errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements("mapping"); //$NON-NLS-1$ //$NON-NLS-2$
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);

        } catch (DocumentException e) {
            throw new HibernateException(
                    ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
                // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            Configuration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) { //$NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}

From source file:org.opentides.persistence.hibernate.PersistenceScanner.java

License:Apache License

/**
 * /*from  ww w  .  ja va2s  .co m*/
 * Perform Spring-based scanning for entity classes.
 * 
 * @see #setPackagesToScan
 */

protected String[] scanPackages() {
    Set<String> entities = new HashSet<String>();
    if (this.packagesToScan != null) {
        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 (matchesFilter(reader, readerFactory)) {
                            entities.add(className);
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new MappingException("Failed to scan classpath for unlisted classes", ex);
        }
    }
    return entities.toArray(new String[entities.size()]);
}

From source file:org.sakaiproject.springframework.orm.hibernate.impl.AdditionalHibernateMappingsImpl.java

License:Educational Community License

public void processConfig(Configuration config) throws IOException, MappingException {
    for (int i = 0; i < this.mappingLocations.length; i++) {
        try {/*  w ww.j  a v a 2s  .  c  o m*/
            logger.info("Loading hbm: " + mappingLocations[i]);
            if (config == null) {
                logger.warn("config is null!");
                return;
            }
            config.addInputStream(this.mappingLocations[i].getInputStream());
        } catch (MappingException me) {
            throw new MappingException("Failed to load " + this.mappingLocations[i], me);
        }
    }
}

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

License:Apache License

/**
 * Perform Spring-based scanning for entity classes.
 * @see #setPackagesToScan//from ww  w  .  ja va2  s . co 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.hibernate3.annotation.AnnotationSessionFactoryBuilder.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes.
 * @see #setPackagesToScan/*from  w  w w.  jav  a2  s  . c o m*/
 */
protected void scanPackages() {
    if (this.packagesToScan != null) {
        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 (matchesFilter(reader, readerFactory)) {
                            Class<?> annotatedClass = this.resourcePatternResolver.getClassLoader()
                                    .loadClass(className);
                            invokeConfigurationMethod("addAnnotatedClass", Class.class, annotatedClass);
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new MappingException("Failed to scan classpath for unlisted classes", ex);
        } catch (ClassNotFoundException ex) {
            throw new MappingException("Failed to load annotated classes from classpath", ex);
        }
    }
}

From source file:org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes, registering them
 * as annotated classes with this {@code Configuration}.
 * @param packagesToScan one or more Java package names
 * @throws HibernateException if scanning fails for any reason
 *//*from  w  w w  .j  ava  2s  .c  om*/
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException {
    Set<String> entityClassNames = new TreeSet<String>();
    Set<String> converterClassNames = new TreeSet<String>();
    Set<String> packageNames = new TreeSet<String>();
    try {
        for (String pkg : 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)) {
                        entityClassNames.add(className);
                    } else if (converterTypeFilter != null
                            && converterTypeFilter.match(reader, readerFactory)) {
                        converterClassNames.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 {
        ClassLoader cl = this.resourcePatternResolver.getClassLoader();
        for (String className : entityClassNames) {
            addAnnotatedClass(cl.loadClass(className));
        }
        for (String className : converterClassNames) {
            ConverterRegistrationDelegate.registerConverter(this, cl.loadClass(className));
        }
        for (String packageName : packageNames) {
            addPackage(packageName);
        }
    } catch (ClassNotFoundException ex) {
        throw new MappingException("Failed to load annotated classes from classpath", ex);
    }
    return this;
}

From source file:org.springframework.orm.hibernate43.LocalSessionFactoryBuilder.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes, registering them
 * as annotated classes with this {@code Configuration}.
 * @param packagesToScan one or more Java package names
 * @throws HibernateException if scanning fails for any reason
 *//*from  w  w w.  j a v  a  2s . co  m*/
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException {
    try {
        for (String pkg : 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)) {
                        addAnnotatedClass(this.resourcePatternResolver.getClassLoader().loadClass(className));
                    } else if (className.endsWith(PACKAGE_INFO_SUFFIX)) {
                        addPackage(className.substring(0, className.length() - PACKAGE_INFO_SUFFIX.length()));
                    }
                }
            }
        }
        return this;
    } catch (IOException ex) {
        throw new MappingException("Failed to scan classpath for unlisted classes", ex);
    } catch (ClassNotFoundException ex) {
        throw new MappingException("Failed to load annotated classes from classpath", ex);
    }
}

From source file:org.springframework.orm.hibernate5.LocalSessionFactoryBuilder.java

License:Apache License

/**
 * Perform Spring-based scanning for entity classes, registering them
 * as annotated classes with this {@code Configuration}.
 * @param packagesToScan one or more Java package names
 * @throws HibernateException if scanning fails for any reason
 *///from   ww  w.ja  v  a  2 s. c  o m
@SuppressWarnings("unchecked")
public LocalSessionFactoryBuilder scanPackages(String... packagesToScan) throws HibernateException {
    Set<String> entityClassNames = new TreeSet<String>();
    Set<String> converterClassNames = new TreeSet<String>();
    Set<String> packageNames = new TreeSet<String>();
    try {
        for (String pkg : 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)) {
                        entityClassNames.add(className);
                    } else if (CONVERTER_TYPE_FILTER.match(reader, readerFactory)) {
                        converterClassNames.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 {
        ClassLoader cl = this.resourcePatternResolver.getClassLoader();
        for (String className : entityClassNames) {
            addAnnotatedClass(cl.loadClass(className));
        }
        for (String className : converterClassNames) {
            addAttributeConverter((Class<? extends AttributeConverter<?, ?>>) cl.loadClass(className));
        }
        for (String packageName : packageNames) {
            addPackage(packageName);
        }
    } catch (ClassNotFoundException ex) {
        throw new MappingException("Failed to load annotated classes from classpath", ex);
    }
    return this;
}