List of usage examples for org.hibernate MappingException MappingException
public MappingException(String message, Throwable cause)
From source file:com.baomidou.hibernateplus.HibernateSpringSessionFactoryBuilder.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 ww.jav a 2s . c o m @SuppressWarnings("unchecked") public HibernateSpringSessionFactoryBuilder 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) { Class<?> cls = cl.loadClass(className); // TODO Caratacus ? EntityInfo EntityInfoUtils.initEntityInfo(cls); addAnnotatedClass(cls); } 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; }
From source file:com.clican.pluto.orm.dynamic.impl.LocalWrapSessionFactoryBean.java
License:LGPL
@Override protected void scanPackages(AnnotationConfiguration config) { if (this.packagesToScan != null) { try {/*from www .j a v a2s. c om*/ ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver( dynamicClassLoader); for (String pkg : this.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)) { config.addAnnotatedClass( 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:com.enonic.cms.store.hibernate.id.IntegerBasedCustomIdentifierGenerator.java
License:Open Source License
private Class parseClass(String className) { try {//from w w w . j a v a 2 s . c o m return ReflectHelper.classForName(className); } catch (ClassNotFoundException e) { throw new MappingException("Failed to parse class: " + className, e); } }
From source file:com.foilen.smalltools.tools.Hibernate4Tools.java
License:Open Source License
/** * Generate the SQL file. This is based on the code in {@link LocalSessionFactoryBuilder#scanPackages(String...)} * * @param dialect/*from www.j a v a 2s. c o m*/ * the dialect (e.g: org.hibernate.dialect.MySQL5InnoDBDialect ) * @param outputSqlFile * where to put the generated SQL file * @param useUnderscore * true: to have tables names like "employe_manager" ; false: to have tables names like "employeManager" * @param packagesToScan * the packages where your entities are */ @SuppressWarnings("deprecation") public static void generateSqlSchema(Class<? extends Dialect> dialect, String outputSqlFile, boolean useUnderscore, String... packagesToScan) { // Configuration Configuration configuration = new Configuration(); if (useUnderscore) { configuration.setNamingStrategy(new ImprovedNamingStrategy()); } Properties properties = new Properties(); properties.setProperty(AvailableSettings.DIALECT, dialect.getName()); // Scan packages Set<String> classNames = 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 = 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 (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) { configuration.addAnnotatedClass(resourcePatternResolver.getClassLoader().loadClass(className)); } for (String packageName : packageNames) { configuration.addPackage(packageName); } } catch (ClassNotFoundException ex) { throw new MappingException("Failed to load annotated classes from classpath", ex); } // Exportation SchemaExport schemaExport = new SchemaExport(configuration, properties); schemaExport.setOutputFile(outputSqlFile); schemaExport.setDelimiter(";"); schemaExport.setFormat(true); schemaExport.execute(true, false, false, true); }
From source file:com.hyron.poscafe.util.ConfigurationWithWildcard.java
License:Open Source License
private void parseMappingElement(Element mappingElement, String name) { final Attribute resourceAttribute = mappingElement.attribute("resource"); final Attribute fileAttribute = mappingElement.attribute("file"); final Attribute jarAttribute = mappingElement.attribute("jar"); final Attribute packageAttribute = mappingElement.attribute("package"); final Attribute classAttribute = mappingElement.attribute("class"); if (resourceAttribute != null) { final String resourceName = resourceAttribute.getValue(); log.debug("session-factory config [{}] named resource [{}] for mapping", name, resourceName); addResource(resourceName);/* w ww.j a v a2s . c om*/ } else if (fileAttribute != null) { final String fileName = fileAttribute.getValue(); log.debug("session-factory config [{}] named file [{}] for mapping", name, fileName); addFile(fileName); } else if (jarAttribute != null) { final String jarFileName = jarAttribute.getValue(); log.debug("session-factory config [{}] named jar file [{}] for mapping", name, jarFileName); addJar(new File(jarFileName)); } else if (packageAttribute != null) { final String packageName = packageAttribute.getValue(); log.debug("session-factory config [{}] named package [{}] for mapping", name, packageName); addPackage(packageName); } else if (classAttribute != null) { final String classAttributeName = classAttribute.getValue(); final List<String> classNames = new ArrayList<String>(); if (classAttributeName.endsWith(".*")) { try { classNames.addAll(getAllAnnotatedClassNames(classAttributeName)); } catch (IOException ioe) { log.error("Could not read class: " + classAttributeName, ioe); } catch (URISyntaxException use) { log.error("Could not read class: " + classAttributeName, use); } } else { classNames.add(classAttributeName); } for (String className : classNames) { try { log.debug("session-factory config [{}] named class [{}] for mapping", name, className); addAnnotatedClass(ReflectHelper.classForName(className)); } catch (Exception e) { throw new MappingException("Unable to load class [ " + className + "] declared in Hibernate configuration <mapping/> entry", e); } } } else { throw new MappingException("<mapping> element in configuration specifies no known attributes"); } }
From source file:org.apache.ode.daohib.NativeHiLoGenerator.java
License:Apache License
public void configure(Type type, Properties params, Dialect dialect) throws MappingException { Class generatorClass = null;/*ww w .ja v a2 s .c o m*/ if (dialect.supportsSequences()) { __log.debug("Using SequenceHiLoGenerator"); generatorClass = SequenceHiLoGenerator.class; } else { generatorClass = TableHiLoGenerator.class; __log.debug("Using native dialect generator " + generatorClass); } IdentifierGenerator g = null; try { g = (IdentifierGenerator) generatorClass.newInstance(); } catch (Exception e) { throw new MappingException("", e); } if (g instanceof Configurable) ((Configurable) g).configure(type, params, dialect); this._proxy = g; }
From source file:org.babyfish.springframework.orm.hibernate.LocalXSessionFactoryBuilder.java
License:Open Source 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 *//* ww w.j a va 2 s . co m*/ public LocalXSessionFactoryBuilder 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.beangle.orm.hibernate.internal.OverrideConfiguration.java
License:Open Source License
/** * Just disable xml file validation./*from w w w.jav a 2s . c o m*/ */ @Override protected Configuration doConfigure(InputStream stream, String resourceName) throws HibernateException { try { ErrorLogger errorLogger = new ErrorLogger(resourceName); SAXReader reader = xmlHelper.createSAXReader(errorLogger, getEntityResolver()); reader.setValidation(false); Document document = reader.read(new InputSource(stream)); if (errorLogger.hasErrors()) { throw new MappingException("invalid configuration", errorLogger.getErrors().get(0)); } doConfigure(document); } catch (DocumentException e) { throw new HibernateException("Could not parse configuration: " + resourceName, e); } finally { try { stream.close(); } catch (IOException ioe) { logger.error("Could not close input stream for {}", resourceName); } } return this; }
From source file:org.bonitasoft.engine.business.data.impl.SchemaManager.java
License:Open Source License
public Class<?> getMappedClass(final String className) throws MappingException { if (className == null) { return null; }/*from w ww.jav a 2s . c o m*/ try { return ReflectHelper.classForName(className); } catch (final ClassNotFoundException cnfe) { throw new MappingException("entity class not found: " + className, cnfe); } }
From source file:org.castafiore.persistence.CastaAnnotationSessionFactoryBean.java
License:Open Source License
/** * Perform Spring-based scanning for entity classes. * @see #setPackagesToScan//from ww w .j a v a 2 s. c om */ protected void scanPackages(AnnotationConfiguration config) { 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)) { config.addAnnotatedClass( this.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); } } }