Example usage for org.hibernate.cfg NamingStrategy NamingStrategy

List of usage examples for org.hibernate.cfg NamingStrategy NamingStrategy

Introduction

In this page you can find the example usage for org.hibernate.cfg NamingStrategy NamingStrategy.

Prototype

NamingStrategy

Source Link

Usage

From source file:chat.Models.java

License:LGPL

/** @param test whether use the testing database */
public static AnnotationConfiguration build(boolean test) throws Exception {
    AnnotationConfiguration conf = new AnnotationConfiguration() {
        private static final long serialVersionUID = 1L;

        @Override//from w  w w .ja v  a 2s .c om
        public SessionFactory buildSessionFactory() throws HibernateException {
            if (!"org.hsqldb.jdbcDriver".equals(getProperty(Environment.DRIVER)))
                return super.buildSessionFactory();
            // fix the issue of hsqldb write delay stupid default value
            SessionFactory fac = super.buildSessionFactory();
            try {
                SessionImpl hib = (SessionImpl) fac.openSession();
                hib.beginTransaction();
                Statement stat = hib.getJDBCContext().borrowConnection().createStatement();
                stat.executeUpdate("SET WRITE_DELAY FALSE");
                hib.getTransaction().commit();
                stat.close();
                hib.close();
                LOG.info("SET WRITE_DELAY FALSE");
            } catch (Exception e) {
                throw new Error(e);
            }
            return fac;
        }
    };
    InputStreamReader connect = new InputStreamReader(
            Models.class.getResourceAsStream("/hibernate.connect.properties"), "UTF-8");
    conf.getProperties().load(connect);
    connect.close();

    conf.setNamingStrategy(new NamingStrategy() {
        @Override
        public String classToTableName(String entity) {
            return StringHelper.unqualify(entity);
        }

        @Override
        public String propertyToColumnName(String property) {
            return StringHelper.unqualify(property);
        }

        @Override
        public String tableName(String table) {
            return table;
        }

        @Override
        public String columnName(String column) {
            return column;
        }

        @Override
        public String collectionTableName(String ownerEntity, String ownerTable, String associatedEntity,
                String associatedTable, String property) {
            return ownerTable + "_" + StringHelper.unqualify(property);
        }

        @Override
        public String joinKeyColumnName(String joinedColumn, String joinedTable) {
            return joinedColumn;
        }

        @Override
        public String foreignKeyColumnName(String property, String propertyEntity, String propertyTable,
                String referencedColumn) {
            return property != null ? StringHelper.unqualify(property) : propertyTable;
        }

        @Override
        public String logicalColumnName(String column, String property) {
            return StringHelper.isEmpty(column) ? StringHelper.unqualify(property) : column;
        }

        @Override
        public String logicalCollectionTableName(String table, String ownerTable, String associatedTable,
                String property) {
            if (table != null)
                return table;
            return ownerTable + "_" + StringHelper.unqualify(property);
        }

        @Override
        public String logicalCollectionColumnName(String column, String property, String referencedColumn) {
            return StringHelper.isEmpty(column) ? property + "_" + referencedColumn : column;
        }
    });

    for (Class<?> c : Class2.packageClasses(Id.class))
        conf.addAnnotatedClass(c);

    if (!"false".equals(conf.getProperty(Environment.AUTOCOMMIT)))
        throw new RuntimeException(Environment.AUTOCOMMIT + " must be false");
    if (test)
        conf.setProperty(Environment.URL, conf.getProperty(Environment.URL + ".test"));
    return conf;
}