List of usage examples for org.hibernate.cfg Configuration addURL
public Configuration addURL(URL url) throws MappingException
From source file:com.vecna.maven.hibernate.HibernateSchemaMojo.java
License:Apache License
/** * Create mapping metadata from provided Hibernate configuration * @return mapping metadata/*from ww w . j a va 2s . c o m*/ * @throws MojoExecutionException if a mapping class cannot be resolved or if the naming strategy cannot be instantiated */ protected Configuration createMappings() throws MojoExecutionException { Configuration configuration = new AnnotationConfiguration(); if (configFiles != null) { for (String configFile : configFiles) { if (configFile != null && !configFile.equals("")) { configuration.configure(getURL(configFile)); } } } if (additionalClasses != null) { for (String additionalClass : additionalClasses) { try { configuration.addClass(Class.forName(additionalClass)); } catch (ClassNotFoundException e) { throw new MojoExecutionException("coudn't add additional classes", e); } } } if (additionalMappings != null) { for (String mapping : additionalMappings) { configuration.addURL(getURL(mapping)); } } if (propertyFiles != null) { for (String propertyFile : propertyFiles) { URL url = getURL(propertyFile); Properties properties = PropertyUtils.loadProperties(url); configuration.addProperties(properties); } } if (properties != null) { configuration.addProperties(properties); } if (namingStrategy != null) { try { @SuppressWarnings("unchecked") Class nsClass = Thread.currentThread().getContextClassLoader().loadClass(namingStrategy); configuration.setNamingStrategy((NamingStrategy) nsClass.newInstance()); } catch (Exception e) { throw new MojoExecutionException(namingStrategy + " is not a valid naming strategy", e); } } configuration.buildMappings(); if (!disableEnvers) { if (tryEnableEnvers(configuration)) { getLog().info("Detected Envers"); } } return configuration; }
From source file:de.cosmocode.palava.jpa.hibernate.DefaultHibernateService.java
License:Apache License
@Override public void initialize() { final Configuration configuration = new AnnotationConfiguration(); LOG.debug("Adding hibernate schema: {}", schema); configuration.addURL(schema); LOG.debug("Adding hibernate config file: {}", config); configuration.configure(config);/*from ww w . ja v a2 s.co m*/ if (interceptor == null) { LOG.info("No interceptor configured"); } else { LOG.info("Using {} as interceptor", interceptor); configuration.setInterceptor(interceptor); } if (propagateEvents) { LOG.info("Registering event listeners"); for (Entry<String, Class<?>> entry : LISTENERS.entrySet()) { final String event = entry.getKey(); final Class<?> type = entry.getValue(); final Key<?> key = Key.get(type, event); final Object listener = registry.proxy(key); LOG.info("Registering {} for {}", listener, event); configuration.setListener(event, listener); } } else { LOG.info("Events are not propagated through the registry"); } LOG.debug("Building session factory"); this.factory = configuration.buildSessionFactory(); statistics.setSessionFactory(factory); statistics.setStatisticsEnabled(true); mBeanService.register(statistics, "name", name); }
From source file:de.tudarmstadt.ukp.lmf.hibernate.HibernateConnect.java
License:Apache License
/** * Creates Hibernate {@link Configuration} and adds all files from Hibernate mapping folder to * the model./*from w w w. j ava2s. c om*/ * * @param dbConfig * database configuration holder * * @return the created Hibernate Configuration */ public static Configuration getConfiguration(DBConfig dbConfig) { Configuration cfg = new Configuration(); cfg.addProperties(getBasicProperties(dbConfig.getJdbc_url(), dbConfig.getJdbc_driver_class(), dbConfig.getDb_vendor(), dbConfig.getUser(), dbConfig.getPassword(), dbConfig.isShowSQL())); cfg.addProperties(getExtraProperties()); // load hibernate mappings ClassLoader cl = HibernateConnect.class.getClassLoader(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); Resource[] mappings = null; try { mappings = resolver.getResources("hibernatemap/access/**/*.hbm.xml"); for (Resource mapping : mappings) { cfg.addURL(mapping.getURL()); } } catch (IOException e) { logger.error("Hibernate mappings not found!"); e.printStackTrace(); } return cfg; }
From source file:de.tudarmstadt.ukp.lmf.hibernate.HibernateConnect.java
License:Apache License
/** * Creates Hibernate {@link Configuration} and adds all files from Hibernate mapping folder to * the model./*from w ww. ja v a 2 s . com*/ * * @param dbConfig * database configuration holder * * @return the created Hibernate Configuration */ public static Configuration getConfiguration(DBConfig dbConfig, Properties extraProperties) { Configuration cfg = new Configuration(); cfg.addProperties(getBasicProperties(dbConfig.getJdbc_url(), dbConfig.getJdbc_driver_class(), dbConfig.getDb_vendor(), dbConfig.getUser(), dbConfig.getPassword(), dbConfig.isShowSQL())); cfg.addProperties(getExtraProperties()); cfg.addProperties(extraProperties); // load hibernate mappings ClassLoader cl = HibernateConnect.class.getClassLoader(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl); Resource[] mappings = null; try { mappings = resolver.getResources("hibernatemap/access/**/*.hbm.xml"); for (Resource mapping : mappings) { cfg.addURL(mapping.getURL()); } } catch (IOException e) { logger.error("Hibernate mappings not found!"); e.printStackTrace(); } return cfg; }
From source file:myrmidia.Database.Connector.java
License:Open Source License
/** * This mehtod is used to override a hard-coded parameter in the * hibernate.cfg.xml file to give a the application a limited altered * configuration. Used to permit hibernate to create non-existing tables. * @param file URL the url path to the hibernate.cfg.xml file * @throws InitializingException/*from w w w . ja va2s.c o m*/ */ public void initOverwriteFromXMLFile(URL file) throws InitializingException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file.openStream()); String hcf = document.getElementsByTagName("HibernateConfigFile").item(0).getTextContent(); String descriptionMapFile = document.getElementsByTagName("DescriptionMappingFile").item(0) .getTextContent(); descriptionClassName = document.getElementsByTagName("DescriptionClassName").item(0).getTextContent(); Configuration hbconfig = new Configuration(); hbconfig.configure(FileIO.findFile(hcf)); hbconfig.addURL(FileIO.findFile(descriptionMapFile)); try { String solutionMapFile = document.getElementsByTagName("SolutionMappingFile").item(0) .getTextContent(); solutionClassName = document.getElementsByTagName("SolutionClassName").item(0).getTextContent(); hbconfig.addResource(solutionMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have solution"); } try { String justOfSolutionMapFile = document.getElementsByTagName("JustificationOfSolutionMappingFile") .item(0).getTextContent(); justOfSolutionClassName = document.getElementsByTagName("JustificationOfSolutionClassName").item(0) .getTextContent(); hbconfig.addResource(justOfSolutionMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have justification of the solution"); } try { String resultMapFile = document.getElementsByTagName("ResultMappingFile").item(0).getTextContent(); resultClassName = document.getElementsByTagName("ResultClassName").item(0).getTextContent(); hbconfig.addResource(resultMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have result"); } hbconfig.setProperty("hibernate.hbm2ddl.auto", "update"); String currentProperty = hbconfig.getProperty("hibernate.connection.url"); currentProperty += ";create=true"; hbconfig.setProperty("hibernate.connection.url", currentProperty); sessionFactory = hbconfig.buildSessionFactory(); } catch (Throwable ex) { throw new InitializingException(ex); } }
From source file:myrmidia.Database.Connector.java
License:Open Source License
@Override public void initFromXMLfile(URL file) throws InitializingException { try {//from w w w . ja v a 2 s. c om DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file.openStream()); String hcf = document.getElementsByTagName("HibernateConfigFile").item(0).getTextContent(); String descriptionMapFile = document.getElementsByTagName("DescriptionMappingFile").item(0) .getTextContent(); descriptionClassName = document.getElementsByTagName("DescriptionClassName").item(0).getTextContent(); Configuration hbconfig = new Configuration(); hbconfig.configure(FileIO.findFile(hcf)); hbconfig.addURL(FileIO.findFile(descriptionMapFile)); try { String solutionMapFile = document.getElementsByTagName("SolutionMappingFile").item(0) .getTextContent(); solutionClassName = document.getElementsByTagName("SolutionClassName").item(0).getTextContent(); hbconfig.addResource(solutionMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have solution"); } try { String justOfSolutionMapFile = document.getElementsByTagName("JustificationOfSolutionMappingFile") .item(0).getTextContent(); justOfSolutionClassName = document.getElementsByTagName("JustificationOfSolutionClassName").item(0) .getTextContent(); hbconfig.addResource(justOfSolutionMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have justification of the solution"); } try { String resultMapFile = document.getElementsByTagName("ResultMappingFile").item(0).getTextContent(); resultClassName = document.getElementsByTagName("ResultClassName").item(0).getTextContent(); hbconfig.addResource(resultMapFile); } catch (Exception e) { LogFactory.getLog(this.getClass()).info("Case does not have result"); } sessionFactory = hbconfig.buildSessionFactory(); } catch (Throwable ex) { throw new InitializingException(ex); } }
From source file:org.clothocad.hibernate.HibernateConnection.java
License:Open Source License
public boolean connect(URL hibernateXML, List<URL> mappings) { // Create a new configuration and configures it. Configuration config = new Configuration(); for (URL map : mappings) { config.addURL(map); }/*from www. j a v a2s.co m*/ try { // Attempt to create a session factory from the configuration. // As long as this session factory is active, the connection is // defined as active. config.configure(hibernateXML); fac = config.buildSessionFactory(); HibernateConnection.connection = this; return true; } catch (Throwable e) { System.err.println(e); e.printStackTrace(); fac = null; return false; } }
From source file:org.exoplatform.services.organization.idm.DisabledUserMigrationScript.java
License:Open Source License
private void setupHibernate(Properties config) throws Exception { final Configuration conf_ = new Configuration(); conf_.setProperty("hibernate.connection.driver_class", config.getProperty("hibernate.connection.driver_class")); conf_.setProperty("hibernate.connection.url", config.getProperty("hibernate.connection.url")); conf_.setProperty("hibernate.connection.username", config.getProperty("hibernate.connection.username")); conf_.setProperty("hibernate.connection.password", config.getProperty("hibernate.connection.password")); conf_.setProperty("hibernate.dialect", config.getProperty("hibernate.dialect")); String config_path = config.getProperty("hibernate.config_path"); URL url = Thread.currentThread().getContextClassLoader().getResource(config_path); if (url == null) { log.error("hibernate config file not found: {}", config_path); } else {//from www .j a v a 2s . c o m log.info("adding hibernate config file {}", config_path); conf_.addURL(url); } sessionFactory_ = SecurityHelper.doPrivilegedAction(new PrivilegedAction<SessionFactory>() { public SessionFactory run() { SessionFactory factory = conf_.configure().buildSessionFactory(); return factory; } }); }
From source file:org.siberia.binding.impl.db.hibernate.HibernateBindingManager.java
License:Open Source License
/** method that returns the configuration to use * @return a Configuration//w w w.j av a 2 s .c o m */ private Configuration createConfiguration() { Configuration cfg = new Configuration(); List<Class> classes = TypeInformationProvider.getInstance().getSubClassFor(SibType.class, true, true, true, (Class[]) null); if (classes != null) { String suffix = ".hbm.xml"; for (int i = 0; i < classes.size(); i++) { Class current = classes.get(i); if (current != null) { String pluginId = TypeInformationProvider.getInstance().getPluginDeclaring(current); if (pluginId == null) { logger.warn("could not find the plugin declaring class " + current); } else { StringBuffer buffer = new StringBuffer(current.getName().length() + suffix.length()); buffer.append(current.getName()); /** replace . by / */ for (int j = 0; j < buffer.length(); j++) { char currentChar = buffer.charAt(j); if (currentChar == '.') { buffer.deleteCharAt(j); buffer.insert(j, '/'); } } buffer.append(suffix); URL url = ResourceLoader.getInstance().getPluginClassLoader(pluginId) .getResource(buffer.toString()); if (url == null) { logger.warn("resource " + buffer.toString() + " could not be found --> ignored"); } else { logger.info("adding hibernate mapping declaration from " + url); cfg = cfg.addURL(url); } } } } } /** configure Configuration properties */ Properties convertedProperties = this.convertProperties(this.getProperties()); this.reportIgnoredProperties(); Enumeration en = convertedProperties.keys(); while (en.hasMoreElements()) { Object current = en.nextElement(); logger.debug( "consider database property '" + current + "' --> '" + convertedProperties.get(current) + "'"); } cfg = cfg.setProperties(convertedProperties); return cfg; }