List of usage examples for org.hibernate.mapping Table getName
public String getName()
From source file:edu.wustl.dao.util.HibernateMetaData.java
License:BSD License
/** * This method will be called to return table name for the given class. * @param classObj : Class of the object * @return tableName : It returns the table name associated to the class. */// www .ja v a 2 s. c o m public static String getTableName(Class classObj) { String tableName = ""; Table tbl = cfg.getClassMapping(classObj.getName()).getTable(); if (tbl != null) { tableName = tbl.getName(); } return tableName; }
From source file:edu.wustl.dao.util.HibernateMetaData.java
License:BSD License
/** * This method will be called to return root table name for the given class. * @param classObj :Class of the object// www. j a v a 2 s.co m * @return :It returns the root table name associated to the class. */ public static String getRootTableName(Class classObj) { String rootTableName = ""; Table tbl = cfg.getClassMapping(classObj.getName()).getRootTable(); if (tbl != null) { rootTableName = tbl.getName(); } return rootTableName; }
From source file:eu.hydrologis.jgrass.database.DatabasePlugin.java
License:Open Source License
@SuppressWarnings("unchecked") private void checkTableExistence() throws Exception { Iterator tableMappings = activeDatabaseConnection.getAnnotationConfiguration().getTableMappings(); List<String> tableList = new ArrayList<String>(); while (tableMappings.hasNext()) { Object next = tableMappings.next(); if (next instanceof Table) { Table mappedTable = (Table) next; String name = mappedTable.getName(); tableList.add(name);/*from ww w .j a va 2 s. c o m*/ } } boolean checkTables = activeDatabaseConnection .checkTables((String[]) tableList.toArray(new String[tableList.size()])); if (!checkTables) { activeDatabaseConnection.createSchemas(true); } }
From source file:fr.keyconsulting.oliphant.postgresql.PostgreSQLListenerAuxiliary.java
License:Open Source License
public String sqlCreateString(Dialect dialect, Mapping mapping, String defaultCatalog, String defaultSchema) { StringBuilder sb = new StringBuilder(); for (Iterator i = config.getClassMappings(); i.hasNext();) { PersistentClass c = (PersistentClass) i.next(); Table table = c.getTable(); if (table.getPrimaryKey().getColumnSpan() > 1) { throw new HibernateException( "Oliphant does not support primary keys that span multiple columns. Objects of class " + c.getClassName() + " will not be monitored for changes."); }/*from ww w.java 2s. co m*/ String tableName = table.getName().toLowerCase(); String idColName = table.getPrimaryKey().getColumn(0).getName(); Iterator verCols = c.getVersion().getColumnIterator(); if (!verCols.hasNext()) { throw new HibernateException( "Oliphant does not support version properties that span multiple columns. Objects of class " + c.getClassName() + " will not be monitored for changes."); } Column verCol = (Column) verCols.next(); String verColName = verCol.getName(); if (verCols.hasNext()) { throw new HibernateException("Oliphant does not support non versioned entities. Objects of class " + c.getClassName() + " will not be monitored for changes."); } sb.append("CREATE OR REPLACE FUNCTION oliphant_" + tableName + "() RETURNS TRIGGER AS $$\n"); sb.append(" DECLARE\n"); sb.append(" VERSION TEXT;\n"); sb.append(" BEGIN\n"); sb.append(" IF TG_OP = 'UPDATE' THEN\n"); sb.append(" VERSION := NEW." + verColName + ";\n"); sb.append(" ELSIF TG_OP = 'DELETE' THEN\n"); sb.append(" VERSION := -1;\n"); sb.append(" END IF;\n"); sb.append(" PERFORM send_notify('oliphant', '" + tableName + "#' || encode(text(OLD." + idColName + ")::bytea,'base64') || '###' || encode(text(VERSION)::bytea,'base64')); RETURN NULL;\n"); sb.append(" END;\n"); sb.append("$$ LANGUAGE 'plpgsql';\n"); sb.append("\n"); sb.append("CREATE TRIGGER oliphant_" + tableName + "_trg\n"); sb.append(" AFTER DELETE OR UPDATE ON " + tableName + "\n"); sb.append(" FOR EACH ROW EXECUTE PROCEDURE oliphant_" + tableName + "();\n"); sb.append("\n"); } return sb.toString(); }
From source file:fr.keyconsulting.oliphant.postgresql.PostgreSQLListenerAuxiliary.java
License:Open Source License
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) { StringBuilder sb = new StringBuilder(); for (Iterator i = config.getClassMappings(); i.hasNext();) { PersistentClass c = (PersistentClass) i.next(); Table table = c.getTable(); if ((table.getPrimaryKey().getColumnSpan() == 1) && (c.getVersion().getColumnSpan() == 1)) { String tableName = table.getName().toLowerCase(); sb.append("DROP FUNCTION oliphant_" + tableName + "()\n"); sb.append("\n"); }/*from w w w. j av a 2 s.c o m*/ } return sb.toString(); }
From source file:it.eng.qbe.datasource.hibernate.HibernateDataSource.java
License:Mozilla Public License
protected void addDbLink(String modelName, Configuration srcCfg, Configuration dstCfg) { String dbLink = null;// ww w . j a va 2s.com PersistentClass srcPersistentClass = null; PersistentClass dstPersistentClass = null; String targetEntityName = null; Table targetTable = null; dbLink = (String) getDbLinkMap().get(modelName); if (dbLink != null) { Iterator it = srcCfg.getClassMappings(); while (it.hasNext()) { srcPersistentClass = (PersistentClass) it.next(); targetEntityName = srcPersistentClass.getEntityName(); dstPersistentClass = dstCfg.getClassMapping(targetEntityName); targetTable = dstPersistentClass.getTable(); targetTable.setName(targetTable.getName() + "@" + dbLink); } } }
From source file:org.beangle.commons.orm.hibernate.internal.SchemaValidator.java
License:Open Source License
public String validateSchema(Configuration config, Dialect dialect, DatabaseMetadata databaseMetadata) { String defaultCatalog = sessionFactoryBean.getHibernateProperties() .getProperty(Environment.DEFAULT_CATALOG); String defaultSchema = sessionFactoryBean.getHibernateProperties().getProperty(Environment.DEFAULT_SCHEMA); Mapping mapping = config.buildMapping(); Iterator<?> iter = config.getTableMappings(); while (iter.hasNext()) { Table table = (Table) iter.next(); if (table.isPhysicalTable()) { TableMetadata tableInfo = databaseMetadata.getTableMetadata(table.getName(), (table.getSchema() == null) ? defaultSchema : table.getSchema(), (table.getCatalog() == null) ? defaultCatalog : table.getCatalog(), table.isQuoted()); if (tableInfo == null) { reporter.append("Missing table: " + table.getName() + "\n"); } else { validateColumns(table, dialect, mapping, tableInfo); }/*w ww .jav a 2 s . c o m*/ } } iter = iterateGenerators(config, dialect); while (iter.hasNext()) { PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next(); Object key = generator.generatorKey(); if (!databaseMetadata.isSequence(key) && !databaseMetadata.isTable(key)) { throw new HibernateException("Missing sequence or table: " + key); } } return null; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
/** * Binds a Column instance to the Hibernate meta model * * @param property The Grails domain class property * @param parentProperty//from w ww. j a va 2 s. c o m * @param column The column to bind * @param path * @param table The table name * @param sessionFactoryBeanName the session factory bean name */ protected void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty, Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) { if (cc != null) { column.setComment(cc.getComment()); column.setDefaultValue(cc.getDefaultValue()); column.setCustomRead(cc.getRead()); column.setCustomWrite(cc.getWrite()); } Class<?> userType = getUserType(property); String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName); if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) { // Only use conventional naming when the column has not been explicitly mapped. if (column.getName() == null) { column.setName(columnName); } if (property.isManyToMany()) { column.setNullable(false); } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) { if (property.getOtherSide().isHasOne()) { column.setNullable(false); } else { column.setNullable(true); } } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) { column.setNullable(true); } else { column.setNullable(property.isOptional()); } } else { column.setName(columnName); column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional())); // Use the constraints for this property to more accurately define // the column's length, precision, and scale ConstrainedProperty constrainedProperty = getConstrainedProperty(property); if (constrainedProperty != null) { if (String.class.isAssignableFrom(property.getType()) || byte[].class.isAssignableFrom(property.getType())) { bindStringColumnConstraints(column, constrainedProperty); } if (Number.class.isAssignableFrom(property.getType())) { bindNumericColumnConstraints(column, constrainedProperty, cc); } } } handleUniqueConstraint(property, column, path, table, columnName, sessionFactoryBeanName); bindIndex(columnName, column, cc, table); if (!property.getDomainClass().isRoot()) { Mapping mapping = getMapping(property.getDomainClass()); if (mapping == null || mapping.getTablePerHierarchy()) { if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName() + "] for column name [" + column.getName() + "] set to nullable"); column.setNullable(true); } else { column.setNullable(property.isOptional()); } } if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name [" + column.getName() + "] in table [" + table.getName() + "]"); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void createUniqueKeyForColumns(Table table, String columnName, List<Column> keyList) { Collections.reverse(keyList); UniqueKey key = table.getOrCreateUniqueKey("unique_" + columnName); List<?> columns = key.getColumns(); if (columns.isEmpty()) { LOG.debug("create unique key for " + table.getName() + " columns = " + keyList); key.addColumns(keyList.iterator()); }/*from w w w .jav a2 s . co m*/ }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a Column instance to the Hibernate meta model * * @param property The Grails domain class property * @param parentProperty/*from ww w .j av a2 s . co m*/ * @param column The column to bind * @param path * @param table The table name * @param sessionFactoryBeanName the session factory bean name */ private static void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty, Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) { Class<?> userType = getUserType(property); String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName); if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) { // Only use conventional naming when the column has not been explicitly mapped. if (column.getName() == null) { column.setName(columnName); } if (property.isManyToMany()) { column.setNullable(false); } else if (property.isOneToOne() && property.isBidirectional() && !property.isOwningSide()) { if (property.getOtherSide().isHasOne()) { column.setNullable(false); } else { column.setNullable(true); } } else if ((property.isManyToOne() || property.isOneToOne()) && property.isCircular()) { column.setNullable(true); } else { column.setNullable(property.isOptional()); } } else { column.setName(columnName); column.setNullable(property.isOptional() || (parentProperty != null && parentProperty.isOptional())); // Use the constraints for this property to more accurately define // the column's length, precision, and scale ConstrainedProperty constrainedProperty = getConstrainedProperty(property); if (constrainedProperty != null) { if (String.class.isAssignableFrom(property.getType()) || byte[].class.isAssignableFrom(property.getType())) { bindStringColumnConstraints(column, constrainedProperty); } if (Number.class.isAssignableFrom(property.getType())) { bindNumericColumnConstraints(column, constrainedProperty); } } } ConstrainedProperty cp = getConstrainedProperty(property); if (cp != null && cp.hasAppliedConstraint(UniqueConstraint.UNIQUE_CONSTRAINT)) { UniqueConstraint uc = (UniqueConstraint) cp.getAppliedConstraint(UniqueConstraint.UNIQUE_CONSTRAINT); if (uc != null && uc.isUnique()) { if (!uc.isUniqueWithinGroup()) { column.setUnique(true); } else if (uc.getUniquenessGroup().size() > 0) { createKeyForProps(property, path, table, columnName, uc.getUniquenessGroup(), sessionFactoryBeanName); } } } else { Object val = cp != null ? cp.getMetaConstraintValue(UniqueConstraint.UNIQUE_CONSTRAINT) : null; if (val instanceof Boolean) { column.setUnique(((Boolean) val).booleanValue()); } else if (val instanceof String) { createKeyForProps(property, path, table, columnName, Arrays.asList(new String[] { (String) val }), sessionFactoryBeanName); } else if (val instanceof List<?> && ((List<?>) val).size() > 0) { createKeyForProps(property, path, table, columnName, (List<?>) val, sessionFactoryBeanName); } } bindIndex(columnName, column, cc, table); if (!property.getDomainClass().isRoot()) { Mapping mapping = getMapping(property.getDomainClass()); if (mapping == null || mapping.getTablePerHierarchy()) { if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] Sub class property [" + property.getName() + "] for column name [" + column.getName() + "] set to nullable"); column.setNullable(true); } else { column.setNullable(property.isOptional()); } } if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] bound property [" + property.getName() + "] to column name [" + column.getName() + "] in table [" + table.getName() + "]"); }