List of usage examples for org.hibernate.mapping SimpleValue addColumn
public void addColumn(Column column)
From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java
License:Open Source License
protected Column createColumn(Mappings mappings, Table tab, com.manydesigns.portofino.model.database.Column column) { Column col = new Column(); col.setName(escapeName(column.getColumnName())); col.setLength(column.getLength());//from w ww. ja va2 s .c o m col.setPrecision(column.getLength()); col.setScale(column.getScale()); col.setNullable(column.isNullable()); String columnType = column.getColumnType(); int jdbcType = column.getJdbcType(); col.setSqlTypeCode(jdbcType); col.setSqlType(columnType); SimpleValue value = new SimpleValue(mappings, tab); if (!setHibernateType(value, column, column.getActualJavaType(), jdbcType)) { logger.error("Skipping column {}", column.getQualifiedName()); return null; } value.addColumn(col); tab.addColumn(col); mappings.addColumnBinding(column.getColumnName(), col, tab); return col; }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue, String columnName) {/* w ww . jav a 2s.c o m*/ PropertyConfig pc = getPropertyConfig(property); String typeName = getTypeName(property, getPropertyConfig(property), getMapping(property.getDomainClass())); if (typeName == null) { Properties enumProperties = new Properties(); enumProperties.put(ENUM_CLASS_PROP, propertyType.getName()); String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType(); if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) { simpleValue.setTypeName("org.codehaus.groovy.grails.orm.hibernate.cfg.IdentityEnumType"); } else { simpleValue.setTypeName(ENUM_TYPE_CLASS); if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) { enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR)); } else if (!"ordinal".equalsIgnoreCase(enumType)) { LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class [" + property.getDomainClass().getClazz().getName() + "]. Using defaults instead."); } } simpleValue.setTypeParameters(enumProperties); } else { simpleValue.setTypeName(typeName); } Table t = simpleValue.getTable(); Column column = new Column(); if (property.getDomainClass().isRoot()) { column.setNullable(property.isOptional()); } else { 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()); } } column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); PropertyConfig propertyConfig = getPropertyConfig(property); if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) { bindIndex(columnName, column, propertyConfig.getColumns().get(0), t); bindColumnConfigToColumn(column, propertyConfig.getColumns().get(0)); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindSimpleValue(GrailsDomainClassProperty grailsProp, GrailsDomainClassProperty parentProperty, SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) { setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig); if (grailsProp.isDerived()) { Formula formula = new Formula(); formula.setFormula(propertyConfig.getFormula()); simpleValue.addFormula(formula); } else {//from www. j av a 2 s .com Table table = simpleValue.getTable(); // Add the column definitions for this value/property. Note that // not all custom mapped properties will have column definitions, // in which case we still need to create a Hibernate column for // this value. List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns() : Arrays.asList(new Object[] { null }); if (columnDefinitions.isEmpty()) { columnDefinitions = Arrays.asList(new Object[] { null }); } for (int i = 0, n = columnDefinitions.size(); i < n; i++) { ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i); Column column = new Column(); // Check for explicitly mapped column name and SQL type. if (cc != null) { if (cc.getName() != null) { column.setName(cc.getName()); } if (cc.getSqlType() != null) { column.setSqlType(cc.getSqlType()); } } column.setValue(simpleValue); if (cc != null) { if (cc.getLength() != -1) { column.setLength(cc.getLength()); } if (cc.getPrecision() != -1) { column.setPrecision(cc.getPrecision()); } if (cc.getScale() != -1) { column.setScale(cc.getScale()); } column.setUnique(cc.isUnique()); } bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName); if (table != null) { table.addColumn(column); } simpleValue.addColumn(column); } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
/** * Binds a value for the specified parameters to the meta model. * * @param type The type of the property * @param simpleValue The simple value instance * @param nullable Whether it is nullable * @param columnName The property name/*from w ww . j av a 2 s . c om*/ * @param mappings The mappings */ protected void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable, String columnName, Mappings mappings) { simpleValue.setTypeName(type); Table t = simpleValue.getTable(); Column column = new Column(); column.setNullable(nullable); column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
private static void bindEnumType(GrailsDomainClassProperty property, Class<?> propertyType, SimpleValue simpleValue, String columnName) { Properties enumProperties = new Properties(); enumProperties.put(ENUM_CLASS_PROP, propertyType.getName()); PropertyConfig pc = getPropertyConfig(property); String enumType = pc != null ? pc.getEnumType() : DEFAULT_ENUM_TYPE; if (enumType.equals(DEFAULT_ENUM_TYPE) && IdentityEnumType.supports(propertyType)) { simpleValue.setTypeName(IdentityEnumType.class.getName()); } else {/*from w ww . j a v a 2 s . c o m*/ simpleValue.setTypeName(ENUM_TYPE_CLASS); if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) { enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR)); } else if (!"ordinal".equalsIgnoreCase(enumType)) { LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class [" + property.getDomainClass().getClazz().getName() + "]. Using defaults instead."); } } simpleValue.setTypeParameters(enumProperties); Table t = simpleValue.getTable(); Column column = new Column(); if (property.getDomainClass().isRoot()) { column.setNullable(property.isOptional()); } else { 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()); } } column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); PropertyConfig propertyConfig = getPropertyConfig(property); if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) { bindIndex(columnName, column, propertyConfig.getColumns().get(0), t); } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
private static void bindSimpleValue(GrailsDomainClassProperty grailsProp, GrailsDomainClassProperty parentProperty, SimpleValue simpleValue, String path, PropertyConfig propertyConfig, String sessionFactoryBeanName) { setTypeForPropertyConfig(grailsProp, simpleValue, propertyConfig); if (grailsProp.isDerived()) { Formula formula = new Formula(); formula.setFormula(propertyConfig.getFormula()); simpleValue.addFormula(formula); } else {/*from w ww . j a v a 2s . c om*/ Table table = simpleValue.getTable(); // Add the column definitions for this value/property. Note that // not all custom mapped properties will have column definitions, // in which case we still need to create a Hibernate column for // this value. List<?> columnDefinitions = propertyConfig != null ? propertyConfig.getColumns() : Arrays.asList(new Object[] { null }); if (columnDefinitions.isEmpty()) { columnDefinitions = Arrays.asList(new Object[] { null }); } for (int i = 0, n = columnDefinitions.size(); i < n; i++) { ColumnConfig cc = (ColumnConfig) columnDefinitions.get(i); Column column = new Column(); // Check for explicitly mapped column name and SQL type. if (cc != null) { if (cc.getName() != null) { column.setName(cc.getName()); } if (cc.getSqlType() != null) { column.setSqlType(cc.getSqlType()); } } column.setValue(simpleValue); bindColumn(grailsProp, parentProperty, column, cc, path, table, sessionFactoryBeanName); if (cc != null) { if (cc.getLength() != -1) { column.setLength(cc.getLength()); } if (cc.getPrecision() != -1) { column.setPrecision(cc.getPrecision()); } if (cc.getScale() != -1) { column.setScale(cc.getScale()); } column.setUnique(cc.isUnique()); } if (table != null) table.addColumn(column); simpleValue.addColumn(column); } } }
From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java
License:Apache License
/** * Binds a value for the specified parameters to the meta model. * * @param type The type of the property * @param simpleValue The simple value instance * @param nullable Whether it is nullable * @param columnName The property name//from w ww. ja v a 2 s . c om * @param mappings The mappings */ private static void bindSimpleValue(String type, SimpleValue simpleValue, boolean nullable, String columnName, @SuppressWarnings("unused") Mappings mappings) { simpleValue.setTypeName(type); Table t = simpleValue.getTable(); Column column = new Column(); column.setNullable(nullable); column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); }
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
License:Open Source License
/** * Extra lazy mapping for lists needs a real property for the list index and * a real inverse for the other side as well. * /* w w w . ja v a 2s .c o m*/ * This method iterates over all associations and adds an inverse for the * list and set mappings. */ protected void addExtraLazyInverseProperties() { final Map<String, PersistentClass> persistentClasses = new HashMap<String, PersistentClass>(); for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); if (isClassOrSuperClassEAVMapped(pc)) { continue; } persistentClasses.put(pc.getEntityName(), pc); } for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) { final PersistentClass pc = (PersistentClass) pcs.next(); // copy to prevent concurrent modification final Iterator<?> propIt = pc.getPropertyIterator(); final List<Property> props = new ArrayList<Property>(); while (propIt.hasNext()) { final Property prop = (Property) propIt.next(); props.add(prop); } for (Property prop : props) { EClass eClass = null; if (pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) == null) { if (pc.getEntityName() != null) { eClass = getEntityNameStrategy().toEClass(pc.getEntityName()); } else { eClass = EModelResolver.instance().getEClass(pc.getMappedClass()); } } final EStructuralFeature ef = eClass == null ? null : StoreUtil.getEStructuralFeature(eClass, prop.getName()); if (ef != null && ef instanceof EReference && prop.getValue() instanceof Collection) { final Collection collection = (Collection) prop.getValue(); final EReference eReference = (EReference) ef; // only work for extra lazy if (!collection.isExtraLazy()) { continue; } final Value elementValue = collection.getElement(); final PersistentClass elementPC; if (elementValue instanceof OneToMany) { final OneToMany oneToMany = (OneToMany) elementValue; elementPC = oneToMany.getAssociatedClass(); } else if (elementValue instanceof ManyToOne) { final ManyToOne mto = (ManyToOne) elementValue; elementPC = persistentClasses.get(mto.getReferencedEntityName()); } else { continue; } if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } collection.setInverse(true); // and add an eopposite if (eReference.getEOpposite() == null) { final Table collectionTable = collection.getCollectionTable(); if (isClassOrSuperClassEAVMapped(elementPC)) { continue; } final Property inverseRefProperty = new Property(); inverseRefProperty.setName(StoreUtil.getExtraLazyInversePropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); inverseRefProperty.setMetaAttributes(metas); inverseRefProperty.setNodeName(inverseRefProperty.getName()); inverseRefProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); inverseRefProperty.setLazy(false); final ManyToOne mto = new ManyToOne(getMappings(), collectionTable); mto.setReferencedEntityName(pc.getEntityName()); mto.setLazy(false); mto.setFetchMode(FetchMode.SELECT); inverseRefProperty.setValue(mto); final Iterator<?> it = collection.getKey().getColumnIterator(); while (it.hasNext()) { final Column originalColumn = (Column) it.next(); // final Column newColumn = new // Column(originalColumn.getName()); mto.addColumn(originalColumn); } mto.createForeignKey(); // now determine if a join should be created if (collectionTable.getName().equalsIgnoreCase(elementPC.getTable().getName())) { elementPC.addProperty(inverseRefProperty); } else { // create a join final Join join = new Join(); join.setPersistentClass(elementPC); join.setTable(collectionTable); join.addProperty(inverseRefProperty); final ManyToOne keyValue = new ManyToOne(getMappings(), collectionTable); join.setKey(keyValue); @SuppressWarnings("unchecked") final Iterator<Column> keyColumns = collection.getElement().getColumnIterator(); while (keyColumns.hasNext()) { keyValue.addColumn(keyColumns.next()); } keyValue.setReferencedEntityName(elementPC.getEntityName()); keyValue.setTable(collectionTable); keyValue.createForeignKey(); elementPC.addJoin(join); } } // add an opposite index if (collection.isIndexed() && !collection.isMap()) { Table collectionTable = collection.getCollectionTable(); IndexedCollection indexedCollection = (IndexedCollection) collection; final Column column = (Column) indexedCollection.getIndex().getColumnIterator().next(); final Property indexProperty = new Property(); indexProperty.setName(StoreUtil.getExtraLazyInverseIndexPropertyName(ef)); final Map<Object, Object> metas = new HashMap<Object, Object>(); final MetaAttribute metaAttribute = new MetaAttribute( HbConstants.SYNTHETIC_PROPERTY_INDICATOR); metaAttribute.addValue("true"); metas.put(HbConstants.SYNTHETIC_PROPERTY_INDICATOR, metaAttribute); indexProperty.setMetaAttributes(metas); indexProperty.setNodeName(indexProperty.getName()); indexProperty.setPropertyAccessorName(SyntheticPropertyHandler.class.getName()); // always make this nullable, nullability is controlled // by the main property indexProperty.setOptional(true); Join join = null; @SuppressWarnings("unchecked") final Iterator<Join> it = (Iterator<Join>) elementPC.getJoinIterator(); while (it.hasNext()) { final Join foundJoin = it.next(); if (foundJoin.getTable().getName().equalsIgnoreCase(collectionTable.getName())) { join = foundJoin; collectionTable = join.getTable(); break; } } final SimpleValue sv = new SimpleValue(getMappings(), indexedCollection.getIndex().getTable()); sv.setTypeName("integer"); // final Column svColumn = new Column(column.getName()); sv.addColumn(column); // checkColumnExists(collectionTable, // svColumn)); indexProperty.setValue(sv); if (join != null) { join.addProperty(indexProperty); } else { elementPC.addProperty(indexProperty); } } } } } }
From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java
License:Open Source License
protected void addContainerMappingToPC(PersistentClass pc) { if (log.isDebugEnabled()) { log.debug("Adding eContainer and econtainerfeatureid properties to " + pc.getClassName()); }/* ww w . j av a2 s . c om*/ final EContainerFeaturePersistenceStrategy featurePersistenceStrategy = getPersistenceOptions() .getEContainerFeaturePersistenceStrategy(); final Property eContainer = new Property(); eContainer.setName(HbConstants.PROPERTY_ECONTAINER); eContainer.setMetaAttributes(new HashMap<Object, Object>()); eContainer.setNodeName(eContainer.getName()); eContainer.setPropertyAccessorName(EContainerAccessor.class.getName()); final SimpleValue sv = new SimpleValue(getMappings(), pc.getTable()); sv.setTypeName(EContainerUserType.class.getName()); final Column eccColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerClassColumn()); sv.addColumn(checkColumnExists(pc.getTable(), eccColumn)); final Column ecColumn = new Column( getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerColumn()); sv.addColumn(checkColumnExists(pc.getTable(), ecColumn)); eContainer.setValue(sv); pc.addProperty(eContainer); if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATUREID) || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) { final Property ecFID = new Property(); ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID); ecFID.setMetaAttributes(new HashMap<Object, Object>()); ecFID.setNodeName(ecFID.getName()); ecFID.setPropertyAccessorName(EContainerFeatureIDAccessor.class.getName()); final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable()); svfid.setTypeName("integer"); final Column ecfColumn = new Column( getPersistenceOptions().getSQLColumnNamePrefix() + HbConstants.COLUMN_ECONTAINER_FEATUREID); svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn)); ecFID.setValue(svfid); pc.addProperty(ecFID); } if (featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.FEATURENAME) || featurePersistenceStrategy.equals(EContainerFeaturePersistenceStrategy.BOTH)) { final Property ecFID = new Property(); ecFID.setName(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME); ecFID.setMetaAttributes(new HashMap<Object, Object>()); ecFID.setNodeName(ecFID.getName()); ecFID.setPropertyAccessorName(NewEContainerFeatureIDPropertyHandler.class.getName()); final SimpleValue svfid = new SimpleValue(getMappings(), pc.getTable()); svfid.setTypeName(EContainerFeatureIDUserType.class.getName()); final Column ecfColumn = new Column(getPersistenceOptions().getSQLColumnNamePrefix() + getPersistenceOptions().getEContainerFeatureNameColumn()); ecfColumn.setLength(getEContainerFeatureNameColumnLength()); svfid.addColumn(checkColumnExists(pc.getTable(), ecfColumn)); ecFID.setValue(svfid); pc.addProperty(ecFID); } }
From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java
License:Apache License
protected void bindEnumType(PersistentProperty property, Class<?> propertyType, SimpleValue simpleValue, String columnName) {// w w w . ja v a 2s . c o m PropertyConfig pc = getPropertyConfig(property); final PersistentEntity owner = property.getOwner(); String typeName = getTypeName(property, getPropertyConfig(property), getMapping(owner)); if (typeName == null) { Properties enumProperties = new Properties(); enumProperties.put(ENUM_CLASS_PROP, propertyType.getName()); String enumType = pc == null ? DEFAULT_ENUM_TYPE : pc.getEnumType(); if (enumType.equals(DEFAULT_ENUM_TYPE) && identityEnumTypeSupports(propertyType)) { simpleValue.setTypeName("org.grails.orm.hibernate.cfg.IdentityEnumType"); } else { simpleValue.setTypeName(ENUM_TYPE_CLASS); if (enumType.equals(DEFAULT_ENUM_TYPE) || "string".equalsIgnoreCase(enumType)) { enumProperties.put(ENUM_TYPE_PROP, String.valueOf(Types.VARCHAR)); } else if (!"ordinal".equalsIgnoreCase(enumType)) { LOG.warn("Invalid enumType specified when mapping property [" + property.getName() + "] of class [" + owner.getName() + "]. Using defaults instead."); } } simpleValue.setTypeParameters(enumProperties); } else { simpleValue.setTypeName(typeName); } Table t = simpleValue.getTable(); Column column = new Column(); if (owner.isRoot()) { column.setNullable(property.isNullable()); } else { Mapping mapping = getMapping(owner); 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.isNullable()); } } column.setValue(simpleValue); column.setName(columnName); if (t != null) t.addColumn(column); simpleValue.addColumn(column); PropertyConfig propertyConfig = getPropertyConfig(property); if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) { bindIndex(columnName, column, propertyConfig.getColumns().get(0), t); bindColumnConfigToColumn(property, column, propertyConfig.getColumns().get(0)); } }