@Test @SuppressWarnings("serial") public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception { LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { @Override protected Configuration newConfiguration() { return new Configuration() { @Override public Configuration setNamingStrategy(NamingStrategy namingStrategy) { throw new IllegalArgumentException(namingStrategy.toString()); } }; } }; sfb.setMappingResources(new String[0]); sfb.setDataSource(new DriverManagerDataSource()); sfb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); try { sfb.afterPropertiesSet(); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException ex) { // expected assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString())); } }
@Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setPackagesToScan("com.github.kmbulebu.nicknack.server.model"); entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); Properties jpaProperties = new Properties(); jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, org.hibernate.dialect.H2Dialect.class.getName()); jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, "true"); jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, ImprovedNamingStrategy.class.getName()); jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, "false"); jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, "update"); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; }
public static void main(String[] args) { Ejb3Configuration ejb3Cfg = new Ejb3Configuration(); // set propeties (these were set based on the persistence.xml file) Properties props = new Properties(); props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); ejb3Cfg.addProperties(props); ejb3Cfg.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); // add annotated classes ejb3Cfg.addAnnotatedClass(EndUser.class); ejb3Cfg.addAnnotatedClass(Individual.class); ejb3Cfg.addAnnotatedClass(Location.class); ejb3Cfg.addAnnotatedClass(SocialGroup.class); ejb3Cfg.addAnnotatedClass(Visit.class); Configuration cfg = ejb3Cfg.getHibernateConfiguration(); SchemaExport se = new SchemaExport(cfg).setOutputFile(RESOURCES_PATH + "specialstudy-schema-ddl.sql"); se.execute(false, false, false, true); }
public static SessionFactory buildSessionFactoryFromAnnotations() { try { sessionFactory = new AnnotationConfiguration() .setNamingStrategy(ImprovedNamingStrategy.INSTANCE) .configure("/hib.cfg.xml") .buildSessionFactory(); return sessionFactory; } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } }
public static void main(String[] args) { Configuration configuration = new Configuration().configure().setNamingStrategy(new ImprovedNamingStrategy()); EnversSchemaGenerator generator = new EnversSchemaGenerator(configuration); SchemaExport export = generator.export(); export.setFormat(false); export.setOutputFile("src/test/resources/data/h2/create-table-new.sql"); export.create(true, false); }
public static void main(String[] args) { Configuration configuration = new Configuration().configure().setNamingStrategy(new ImprovedNamingStrategy()); EnversSchemaGenerator generator = new EnversSchemaGenerator(configuration); SchemaExport export = generator.export(); export.setFormat(false); export.setOutputFile("src/test/resources/h2schma.sql"); export.create(true, false); }
@Test public void improvedNaming_ClassToTableName() { ImprovedNamingStrategy namingStrategy = new ImprovedNamingStrategy(); for (int i = 0; i < classNames.length; i++) { String tableName = namingStrategy.classToTableName(classNames[i]); assertThat(tableName).isEqualTo(tableNames[i]); } }
@Test public void improvedNaming_PropertyToColumnName() { ImprovedNamingStrategy namingStrategy = new ImprovedNamingStrategy(); for (int i = 0; i < propertyNames.length; i++) { final String columnName = namingStrategy.propertyToColumnName(propertyNames[i]); assertThat(columnName).isEqualTo(columnNames[i]); } }
@Test public void improvedNaming_ClassToTableName() { ImprovedNamingStrategy improvedNamingStrategy = new ImprovedNamingStrategy(); for (int i = 0; i < classNames.length; i++) { Assert.assertEquals(tableNames[i], improvedNamingStrategy.classToTableName(classNames[i])); } }
@Test public void improvedNaming_PropertyToColumnName() { ImprovedNamingStrategy improvedNamingStrategy = new ImprovedNamingStrategy(); for (int i = 0; i < propertyNames.length; i++) { Assert.assertEquals(columnNames[i], improvedNamingStrategy.propertyToColumnName(propertyNames[i])); } }
@Test public void testSuccessGetResultSetStatementImprovedNamingStrategyJava() { interpretedClass = InterpretedClassFactory.getInterpretedClass(ExampleModel.class); resultSetStatement = new ResultSetStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.resultSetStringImprovedNamingStrategy, resultSetStatement.getStatement()); }
@Test public void testSuccessGetResultSetStatementImprovedNamingStrategyClass() { interpretedClass = InterpretedClassFactory.getInterpretedClass(ExampleModel.class); resultSetStatement = new ResultSetStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.resultSetStringImprovedNamingStrategy, resultSetStatement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyJava() { interpretedClass = InterpretedClassFactory.getInterpretedClass(ExampleModel.class); statement = new UpdateSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.updateSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyClass() { interpretedClass = InterpretedClassFactory.getInterpretedClass(getJavaClass()); statement = new UpdateSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.updateSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyJava() { interpretedClass = InterpretedClassFactory.getInterpretedClass(ExampleModel.class); statement = new InsertSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.insertSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyClass() { interpretedClass = InterpretedClassFactory.getInterpretedClass(getJavaClass()); statement = new InsertSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.insertSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyJava() { interpretedClass = InterpretedClassFactory.getInterpretedClass(ExampleModel.class); statement = new SelectSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.selectSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Test public void testSuccessGetStatementImprovedNamingStrategyClass() { interpretedClass = InterpretedClassFactory.getInterpretedClass(getJavaClass()); statement = new SelectSQLStatement(interpretedClass, new ImprovedNamingStrategy()); assertEquals(Results.selectSQLStatementStringImprovedNamingStrategy, statement.getStatement()); }
@Override public SessionFactory load(String sessFacKey) { checkNotNull(sessFacKey); String m = "load(String)"; logger.debug( "{}: building new SessionFactory for sessFacKey [{}]", m, sessFacKey); Configuration config = new Configuration(); config.setNamingStrategy(new ImprovedNamingStrategy()); Optional<Properties> propsOpt = Optional.fromNullable(configProps .get(sessFacKey)); if (propsOpt.isPresent()) { config.setProperties(propsOpt.get()); } else { if (sessFacKey.equals(DEFAULT_SESS_FAC_KEY)) { } else { Properties props = new Properties(); InputStream is = null; try { String fileName = sessFacKey + "-hibernate.properties"; is = HibernateUtil.class .getClassLoader() .getResourceAsStream( sessFacKey + "-hibernate.properties"); Preconditions.checkState(is != null, "couldn't load hibernate properties file [" + fileName + "]"); props.load(is); config.setProperties(props); } catch (IOException e) { throw Throwables.propagate(e); } finally { Closeables.closeQuietly(is); } } } if (sessFacKey.equals(DEFAULT_SESS_FAC_KEY)) { config.configure(); } else { config.configure(sessFacKey + "-hibernate.cfg.xml"); } ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()) .build(); SessionFactory sessionFactory = config .buildSessionFactory(serviceRegistry); configs.put(sessFacKey, config); configProps.remove(sessFacKey); return sessionFactory; }
@Bean public NamingStrategy namingStrategy() { return new ImprovedNamingStrategy(); }
/** * Adds the underscored and covert to upperCase. * * @param name * the name * @return the string */ static String addUnderscoredAndCovertToUppercase(final String name) { return ImprovedNamingStrategy.addUnderscores(name).toLowerCase(); }