List of usage examples for org.hibernate.type BasicTypeRegistry BasicTypeRegistry
public BasicTypeRegistry()
From source file:org.anyframe.hibernate.impl.DynamicHibernateServiceImpl.java
License:Apache License
private void addScalar(SQLQuery query, Map<String, String> returnScalarMap) { BasicTypeRegistry typeRegistry = new BasicTypeRegistry(); if (!returnScalarMap.isEmpty()) { Set<String> keySet = returnScalarMap.keySet(); Iterator<String> keyItr = keySet.iterator(); while (keyItr.hasNext()) { String column = keyItr.next(); String typeName = returnScalarMap.get(column); if (typeName != null) query.addScalar(column, typeRegistry.getRegisteredType(typeName)); else//from ww w . ja v a 2 s. c om query.addScalar(column); } } }
From source file:org.jspresso.framework.tools.entitygenerator.EntityGenerator.java
License:Open Source License
/** * Generates the component java source files. *//*from www. j a v a2 s . c o m*/ @SuppressWarnings({ "rawtypes", "ConstantConditions" }) public void generateComponents() { LOG.debug("Loading Spring context {}.", applicationContextKey); BeanFactoryReference bfr = getBeanFactoryReference(); try { ApplicationContext appContext = (ApplicationContext) bfr.getFactory(); LOG.debug("Spring context {} loaded.", applicationContextKey); Collection<IComponentDescriptor<?>> componentDescriptors = new LinkedHashSet<>(); if (componentIds == null) { LOG.debug("Retrieving components from Spring context."); Map<String, IComponentDescriptor> allComponents = appContext .getBeansOfType(IComponentDescriptor.class); LOG.debug("{} components retrieved.", allComponents.size()); LOG.debug("Filtering components to generate."); for (Map.Entry<String, IComponentDescriptor> componentEntry : allComponents.entrySet()) { String className = componentEntry.getValue().getName(); if (className != null) { boolean include = false; if (includePackages != null) { for (String pkg : includePackages) { if (className.startsWith(pkg)) { include = true; } } } else { include = true; } if (include) { if (excludePatterns != null) { for (String excludePattern : excludePatterns) { if (include && Pattern.matches(excludePattern, componentEntry.getValue().getName())) { include = false; } } } if (include) { componentDescriptors.add(componentEntry.getValue()); } } } } } else { for (String componentId : componentIds) { componentDescriptors.add((IComponentDescriptor<?>) appContext.getBean(componentId)); } } LOG.debug("{} components filtered.", componentDescriptors.size()); LOG.debug("Initializing Freemarker template"); Version version = new Version(2, 3, 23); Configuration cfg = new Configuration(version); cfg.setClassForTemplateLoading(getClass(), templateResourcePath); BeansWrapper wrapper = new DefaultObjectWrapper(version); cfg.setObjectWrapper(new DefaultObjectWrapper(version)); Template template; try { template = cfg.getTemplate(templateName); } catch (IOException ex) { LOG.error("Error while loading the template", ex); return; } Map<String, Object> rootContext = new HashMap<>(); rootContext.put("generateSQLName", new GenerateSqlName()); rootContext.put("dedupSQLName", new DedupSqlName(false)); rootContext.put("reduceSQLName", new ReduceSqlName(maxSqlNameSize, new DedupSqlName(true))); rootContext.put("instanceof", new InstanceOf(wrapper)); rootContext.put("compareStrings", new CompareStrings(wrapper)); rootContext.put("compactString", new CompactString()); rootContext.put("componentTranslationsDescriptor", appContext.getBean("componentTranslationsDescriptor")); rootContext.put("generateAnnotations", generateAnnotations); rootContext.put("hibernateTypeRegistry", new BasicTypeRegistry()); if (classnamePrefix == null) { classnamePrefix = ""; } if (classnameSuffix == null) { classnameSuffix = ""; } LOG.debug("Freemarker template initialized"); for (IComponentDescriptor<?> componentDescriptor : componentDescriptors) { OutputStream out = null; if (outputDir != null) { String cDescName = componentDescriptor.getName(); int lastDotIndex = cDescName.lastIndexOf('.'); if (lastDotIndex >= 0) { cDescName = cDescName.substring(0, lastDotIndex + 1) + classnamePrefix + cDescName.substring(lastDotIndex + 1); } else { cDescName = classnamePrefix + cDescName; } cDescName = cDescName + classnameSuffix; try { File outFile = new File( outputDir + "/" + cDescName.replace('.', '/') + "." + fileExtension); if (!outFile.exists()) { LOG.debug("Creating " + outFile.getName()); if (!outFile.getParentFile().exists()) { //noinspection ResultOfMethodCallIgnored outFile.getParentFile().mkdirs(); } //noinspection ResultOfMethodCallIgnored outFile.createNewFile(); out = new FileOutputStream(outFile); } else if (componentDescriptor.getLastUpdated() > outFile.lastModified()) { out = new FileOutputStream(outFile); } else { LOG.debug("No change detected for {} : {} <= {}", componentDescriptor.getName(), new Date(componentDescriptor.getLastUpdated()), new Date(outFile.lastModified())); } } catch (IOException ex) { LOG.error("Error while writing output", ex); return; } } else { out = System.out; } if (out != null) { LOG.info("Generating source code for {}", componentDescriptor.getName()); rootContext.put("componentDescriptor", componentDescriptor); try { template.process(rootContext, new OutputStreamWriter(out)); out.flush(); if (out != System.out) { out.close(); } } catch (TemplateException ex) { LOG.error("Error while processing the template", ex); return; } catch (IOException ex) { LOG.error("Error while writing output", ex); return; } } else { LOG.debug("Source code for {} is up to date. Skipping generation.", componentDescriptor.getName()); } LOG.debug("Finished generating Source code for {}.", componentDescriptor.getName()); } } finally { bfr.release(); } }
From source file:org.teiid.spring.autoconfigure.SchemaBuilderUtility.java
License:Apache License
public static Metadata generateHbmModel(ConnectionProvider provider, Dialect dialect) throws SQLException { MetadataSources metadataSources = new MetadataSources(); ServiceRegistry registry = metadataSources.getServiceRegistry(); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) registry).applySetting(AvailableSettings.DIALECT, TeiidDialect.class) .addService(ConnectionProvider.class, provider).addService(JdbcEnvironment.class, new JdbcEnvironmentImpl(provider.getConnection().getMetaData(), dialect)) .build();//from ww w. jav a 2 s . c om ReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy(); MetadataBuildingOptions options = new MetadataBuildingOptionsImpl(serviceRegistry); BasicTypeRegistry basicTypeRegistry = new BasicTypeRegistry(); TypeResolver typeResolver = new TypeResolver(basicTypeRegistry, new TypeFactory()); InFlightMetadataCollectorImpl metadataCollector = new InFlightMetadataCollectorImpl(options, typeResolver); MetadataBuildingContext buildingContext = new MetadataBuildingContextRootImpl(options, null, metadataCollector); JDBCBinder binder = new JDBCBinder(serviceRegistry, new Properties(), buildingContext, strategy, false); Metadata metadata = metadataCollector.buildMetadataInstance(buildingContext); binder.readFromDatabase(null, null, buildMapping(metadata)); HibernateMappingExporter exporter = new HibernateMappingExporter() { @Override protected Metadata getMetadata() { return metadata; } }; exporter.start(); return metadata; }
From source file:org.teiid.spring.autoconfigure.SchemaBuilderUtility.java
License:Apache License
public static ArtifactCollector generateHibernateModel(MetadataFactory source, StandardServiceRegistry serviceRegistry) { ReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy(); MetadataBuildingOptions options = new MetadataBuildingOptionsImpl(serviceRegistry); BasicTypeRegistry basicTypeRegistry = new BasicTypeRegistry(); TypeResolver typeResolver = new TypeResolver(basicTypeRegistry, new TypeFactory()); InFlightMetadataCollectorImpl metadataCollector = new InFlightMetadataCollectorImpl(options, typeResolver); MetadataBuildingContext buildingContext = new MetadataBuildingContextRootImpl(options, null, metadataCollector);//w w w .j a va 2 s . c om TeiidJDBCBinder binder = new TeiidJDBCBinder(serviceRegistry, new Properties(), buildingContext, strategy, false, metadataCollector, source); Metadata metadata = metadataCollector.buildMetadataInstance(buildingContext); binder.readFromDatabase(null, null, buildMapping(metadata)); HibernateMappingExporter exporter = new HibernateMappingExporter() { @Override protected Metadata getMetadata() { return metadata; } }; exporter.setOutputDirectory(TMP_DIR); exporter.start(); return exporter.getArtifactCollector(); }