List of usage examples for org.hibernate.cfg ImprovedNamingStrategy ImprovedNamingStrategy
ImprovedNamingStrategy
From source file:com.bentechapps.angularcrud.config.SpringApplication.java
@Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan(new String[] { "com.bentechapps.angularcrud.entity" }); sessionFactory.setHibernateProperties(hibernateProperties()); sessionFactory.setNamingStrategy(new ImprovedNamingStrategy()); return sessionFactory; }
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 w ww. j a v a 2 s . 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.maydesk.base.util.CledaConnector.java
License:Mozilla Public License
private void createSessionFactory() { Properties props = new Properties(); try {//w w w . j av a2 s.co m Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/maydesk_db"); props.put("hibernate.connection.datasource", ds); } catch (NamingException e) { e.printStackTrace(); } props.put("hibernate.cglib.use_reflection_optimizer", true); props.put("hibernate.show_sql", false); props.put("hibernate.hbm2ddl.auto", "update"); props.put("transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory"); props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); // Create config Configuration config = new Configuration(); config.setProperties(props); config.setInterceptor(new AuditInterceptor()); config.setNamingStrategy(new ImprovedNamingStrategy()); config.setProperties(props); // registering model registerModels(config, "com.maydesk.base.model"); registerModels(config, "com.maydesk.social.model"); ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder(); ServiceRegistry serviceRegistry = serviceRegistryBuilder.applySettings(props).buildServiceRegistry(); sessionFactory = config.buildSessionFactory(serviceRegistry); }
From source file:com.mycompany.asyncreq.Main.java
public static void main(String[] args) throws JsonProcessingException, JSONException, IOException { Configuration config = new Configuration(); // Name tables with lowercase_underscore_separated RestTemplate restTemplate = new RestTemplate(); config.setNamingStrategy(new ImprovedNamingStrategy()); try {//from w w w. ja v a 2 s . co m ArrayList<String> ArrReq = GenData(); String addr; for (Iterator<String> i = ArrReq.iterator(); i.hasNext();) { try { addr = i.next(); Thread.sleep(1000); Root tRoot = restTemplate.getForObject(addr, Root.class); tRoot.addr = addr; if (!tRoot.dataset.isEmpty()) if (tRoot.dataset.size() == tRoot.validation.count.value) ObjToCsv(tRoot, "all"); else { for (int reg = 1; reg < 5; reg++) { Thread.sleep(1000); addr = "http://comtrade.un.org/api/get?max=50000&type=C&freq=M&px=HS&ps=2014&r=804&p=" + tRoot.dataset.get(0).getptCode() + "&rg=" + reg + "&cc=All&fmt=json"; Root tRootImp = restTemplate.getForObject(addr, Root.class); tRootImp.addr = addr; if (!tRootImp.dataset.isEmpty()) if (tRootImp.dataset.size() == tRootImp.validation.count.value) ObjToCsv(tRootImp, Integer.toString(reg)); else System.out.println("addr: " + tRootImp.addr + "\n"); } } else System.out.println("addr: " + tRoot.addr + ":null" + "\n"); } catch (Exception e) { System.err.println(e.getMessage()); } } } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); } }
From source file:edu.upenn.cis.ppod.persistence.SessionFactoryProvider.java
License:Apache License
public SessionFactory get() { logger.debug("building session factory..."); final Configuration cfg = new Configuration(); cfg.setNamingStrategy(new ImprovedNamingStrategy()); // Read hibernate.cfg.xml (has to be present) cfg.configure();/*from ww w.java2 s. c o m*/ final SessionFactory sf = cfg.buildSessionFactory(); logger.debug("...done"); return sf; }
From source file:onl.netfishers.netshot.Database.java
License:Open Source License
/** * Initializes the database access, with Hibernate. *//*from w w w . j a v a2 s. c om*/ public static void init() { try { configuration = new Configuration(); configuration .setProperty("hibernate.connection.driver_class", Netshot.getConfig("netshot.db.driver_class", "com.mysql.jdbc.Driver")) .setProperty("hibernate.connection.url", Netshot.getConfig("netshot.db.url", "jdbc:mysql://localhost/netshot01")) .setProperty("hibernate.connection.username", Netshot.getConfig("netshot.db.username", "netshot")) .setProperty("hibernate.connection.password", Netshot.getConfig("netshot.db.password", "netshot")) .setProperty("hibernate.c3p0.min_size", "5").setProperty("hibernate.c3p0.max_size", "30") .setProperty("hibernate.c3p0.timeout", "1800") .setProperty("hibernate.c3p0.max_statements", "50") .setProperty("hibernate.c3p0.unreturnedConnectionTimeout", "1800") .setProperty("hibernate.c3p0.debugUnreturnedConnectionStackTraces", "true"); configuration.setProperty("factory_class", "org.hibernate.transaction.JDBCTransactionFactory") .setProperty("current_session_context_class", "thread") .setProperty("hibernate.hbm2ddl.auto", "update") //.setProperty("hibernate.show_sql", "true") .addAnnotatedClass(Device.class).addAnnotatedClass(DeviceGroup.class) .addAnnotatedClass(Config.class).addAnnotatedClass(DeviceAttribute.class) .addAnnotatedClass(DeviceNumericAttribute.class).addAnnotatedClass(DeviceTextAttribute.class) .addAnnotatedClass(DeviceLongTextAttribute.class).addAnnotatedClass(DeviceBinaryAttribute.class) .addAnnotatedClass(ConfigAttribute.class).addAnnotatedClass(ConfigNumericAttribute.class) .addAnnotatedClass(ConfigTextAttribute.class).addAnnotatedClass(ConfigLongTextAttribute.class) .addAnnotatedClass(ConfigBinaryAttribute.class).addAnnotatedClass(LongTextConfiguration.class) .addAnnotatedClass(StaticDeviceGroup.class).addAnnotatedClass(DynamicDeviceGroup.class) .addAnnotatedClass(Module.class).addAnnotatedClass(Domain.class) .addAnnotatedClass(PhysicalAddress.class).addAnnotatedClass(NetworkAddress.class) .addAnnotatedClass(Network4Address.class).addAnnotatedClass(Network6Address.class) .addAnnotatedClass(NetworkInterface.class).addAnnotatedClass(DeviceSnmpv1Community.class) .addAnnotatedClass(DeviceSnmpv2cCommunity.class).addAnnotatedClass(DeviceSshAccount.class) .addAnnotatedClass(DeviceSshKeyAccount.class).addAnnotatedClass(DeviceTelnetAccount.class) .addAnnotatedClass(Policy.class).addAnnotatedClass(Rule.class).addAnnotatedClass(Task.class) .addAnnotatedClass(Exemption.class).addAnnotatedClass(Exemption.Key.class) .addAnnotatedClass(CheckResult.class).addAnnotatedClass(CheckResult.Key.class) .addAnnotatedClass(SoftwareRule.class).addAnnotatedClass(HardwareRule.class) .addAnnotatedClass(DeviceJsScript.class).addAnnotatedClass(User.class); for (Class<?> clazz : Task.getTaskClasses()) { logger.info("Registering task class " + clazz.getName()); configuration.addAnnotatedClass(clazz); } for (Class<?> clazz : Rule.getRuleClasses()) { configuration.addAnnotatedClass(clazz); for (Class<?> subClass : clazz.getClasses()) { if (subClass.getAnnotation(Entity.class) != null) { configuration.addAnnotatedClass(subClass); } } } configuration.setNamingStrategy(new ImprovedNamingStrategy()); configuration.setInterceptor(new DatabaseInterceptor()); serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()) .build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (HibernateException e) { logger.error(MarkerFactory.getMarker("FATAL"), "Unable to instantiate Hibernate", e); throw new RuntimeException("Unable to instantiate Hibernate, see logs for more details"); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected NamingStrategy getNamingStrategy(String sessionFactoryBeanName) { String key = "sessionFactory".equals(sessionFactoryBeanName) ? GrailsDomainClassProperty.DEFAULT_DATA_SOURCE : sessionFactoryBeanName.substring("sessionFactory_".length()); NamingStrategy namingStrategy = NAMING_STRATEGIES.get(key); return namingStrategy != null ? namingStrategy : new ImprovedNamingStrategy(); }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected NamingStrategy getNamingStrategy(String sessionFactoryBeanName) { String key = "sessionFactory".equals(sessionFactoryBeanName) ? Mapping.DEFAULT_DATA_SOURCE : sessionFactoryBeanName.substring("sessionFactory_".length()); NamingStrategy namingStrategy = NAMING_STRATEGIES.get(key); return namingStrategy != null ? namingStrategy : new ImprovedNamingStrategy(); }