Example usage for org.hibernate.mapping Property getColumnIterator

List of usage examples for org.hibernate.mapping Property getColumnIterator

Introduction

In this page you can find the example usage for org.hibernate.mapping Property getColumnIterator.

Prototype

public Iterator getColumnIterator() 

Source Link

Usage

From source file:org.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindNaturalIdentifier(Table table, Mapping mapping, PersistentClass persistentClass) {
    Object o = mapping != null ? mapping.getIdentity() : null;
    if (!(o instanceof Identity)) {
        return;/*from   ww  w.  jav a  2  s.  co m*/
    }

    Identity identity = (Identity) o;
    final NaturalId naturalId = identity.getNatural();
    if (naturalId == null || naturalId.getPropertyNames().isEmpty()) {
        return;
    }

    UniqueKey uk = new UniqueKey();
    uk.setTable(table);

    boolean mutable = naturalId.isMutable();

    for (String propertyName : naturalId.getPropertyNames()) {
        Property property = persistentClass.getProperty(propertyName);

        property.setNaturalIdentifier(true);
        if (!mutable)
            property.setUpdateable(false);

        uk.addColumns(property.getColumnIterator());
    }

    setGeneratedUniqueName(uk);

    table.addUniqueKey(uk);
}

From source file:org.jooq.meta.extensions.jpa.AttributeConverterExtractor.java

License:Apache License

@SuppressWarnings("unchecked")
final Map<Name, AttributeConverter<?, ?>> extract() {
    Map<Name, AttributeConverter<?, ?>> result = new LinkedHashMap<>();

    initEntityManagerFactory();//www  .  jav a2  s.c  o m
    for (PersistentClass persistentClass : meta.getEntityBindings()) {
        Table table = persistentClass.getTable();

        Iterator<Property> propertyIterator = persistentClass.getPropertyIterator();

        propertyLoop: while (propertyIterator.hasNext()) {
            Property property = propertyIterator.next();
            Type type = property.getValue().getType();

            if (type instanceof AttributeConverterTypeAdapter) {
                AttributeConverter<?, ?> converter = ((AttributeConverterTypeAdapter<?>) type)
                        .getAttributeConverter().getConverterBean().getBeanInstance();
                Iterator<Column> columnIterator = property.getColumnIterator();

                if (columnIterator.hasNext()) {
                    Column column = columnIterator.next();

                    if (columnIterator.hasNext()) {
                        log.info("AttributeConverter", "Cannot apply AttributeConverter of property " + property
                                + " on several columns.");
                        continue propertyLoop;
                    }

                    result.put(name(table.getCatalog(), table.getSchema(), table.getName(), column.getName()),
                            converter);
                }
            }
        }
    }

    return result;
}

From source file:org.projectforge.database.HibernateUtils.java

License:Open Source License

private Integer internalGetPropertyMaxLength(final String entityName, final String propertyName) {
    Integer length = columnLengthMap.get(getKey(entityName, propertyName));
    if (length != null) {
        return length;
    }//w  ww.  ja  v a 2s  . c  om
    if (columnLengthFailedSet.contains(getKey(entityName, propertyName)) == true) {
        return null;
    }
    final PersistentClass persistentClass = configuration.getClassMapping(entityName);
    if (persistentClass == null) {
        final String msg = "Could not find persistent class for entityName '" + entityName
                + "' (OK for non hibernate objects).";
        if (entityName.endsWith("DO") == true) {
            log.error(msg);
        } else {
            log.info(msg);
        }
        putFailedEntry(entityName, propertyName);
        return null;
    }
    Column column = persistentClass.getTable().getColumn(new Column(propertyName));
    if (column == null) {
        // OK, may be the database name of the column differs, e. g. if a different name is set via @Column(name = "...").
        Property property = null;
        try {
            property = persistentClass.getProperty(propertyName);
        } catch (final MappingException ex) {
            if (TEST_MODE == false) {
                log.error(ex.getMessage(), ex);
            } else {
                log.info("***** TESTMODE: property '" + propertyName + "' not found (OK in test mode).");
            }
            putFailedEntry(entityName, propertyName);
            return null;
        }
        final Iterator<?> it = property.getColumnIterator();
        if (it.hasNext() == true) {
            column = (Column) it.next();
            if (it.hasNext() == true) {
                putFailedEntry(entityName, propertyName);
                throw new UnsupportedOperationException("Multiple columns for selected entity '" + entityName
                        + "' with name '" + propertyName + "' not predictable, aborting.");
            }
        }
    }
    if (column == null) {
        log.error("Could not find column for entity '" + entityName + "' with name '" + propertyName + "'.");
        return null;
    }
    length = column.getLength();
    columnLengthMap.put(entityName + "#" + propertyName, length);
    return length;
}

From source file:org.sparkcommerce.openadmin.server.dao.provider.metadata.DefaultFieldMetadataProvider.java

License:Apache License

@Override
public FieldProviderResponse addMetadataFromMappingData(
        AddMetadataFromMappingDataRequest addMetadataFromMappingDataRequest, FieldMetadata metadata) {
    BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) metadata;
    fieldMetadata.setFieldType(addMetadataFromMappingDataRequest.getType());
    fieldMetadata.setSecondaryType(addMetadataFromMappingDataRequest.getSecondaryType());
    if (addMetadataFromMappingDataRequest.getRequestedEntityType() != null
            && !addMetadataFromMappingDataRequest.getRequestedEntityType().isCollectionType()) {
        Column column = null;/* w ww.j a  va 2 s .c o m*/
        for (Property property : addMetadataFromMappingDataRequest.getComponentProperties()) {
            if (property.getName().equals(addMetadataFromMappingDataRequest.getPropertyName())) {
                Object columnObject = property.getColumnIterator().next();
                if (columnObject instanceof Column) {
                    column = (Column) columnObject;
                }
                break;
            }
        }
        if (column != null) {
            fieldMetadata.setLength(column.getLength());
            fieldMetadata.setScale(column.getScale());
            fieldMetadata.setPrecision(column.getPrecision());
            fieldMetadata.setRequired(!column.isNullable());
            fieldMetadata.setUnique(column.isUnique());
        }
        fieldMetadata.setForeignKeyCollection(false);
    } else {
        fieldMetadata.setForeignKeyCollection(true);
    }
    fieldMetadata.setMutable(true);
    fieldMetadata.setMergedPropertyType(addMetadataFromMappingDataRequest.getMergedPropertyType());
    if (SupportedFieldType.BROADLEAF_ENUMERATION.equals(addMetadataFromMappingDataRequest.getType())) {
        try {
            setupSparkEnumeration(fieldMetadata.getSparkEnumeration(), fieldMetadata,
                    addMetadataFromMappingDataRequest.getDynamicEntityDao());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return FieldProviderResponse.HANDLED;
}

From source file:org.teiid.spring.views.ViewBuilder.java

License:Apache License

@SuppressWarnings("unchecked")
boolean propertyMatches(org.hibernate.mapping.Property property, String colName) {
    if (property.isComposite()) {
        Component comp = (Component) property.getValue();
        Iterator<org.hibernate.mapping.Property> compIt = comp.getPropertyIterator();
        while (compIt.hasNext()) {
            property = compIt.next();/*from  w  w w  .  j  a  va 2 s . com*/
            if (propertyMatches(property, colName)) {
                return true;
            }
        }
        return false;
    }
    Iterator<?> columnIterator = property.getColumnIterator();
    if (columnIterator.hasNext()) {
        org.hibernate.mapping.Column col = (org.hibernate.mapping.Column) columnIterator.next();
        assert !columnIterator.hasNext();
        if (col.getName().equals(colName)) {
            return true;
        }
    }
    return false;
}

From source file:org.unitime.commons.hibernate.util.HibernateUtil.java

License:Open Source License

public static void fixSchemaInFormulas(Configuration cfg) {
    cfg.buildMappings();/* w w w.  j a  v  a 2  s  . c om*/
    String schema = cfg.getProperty("default_schema");
    if (schema != null) {
        for (Iterator i = cfg.getClassMappings(); i.hasNext();) {
            PersistentClass pc = (PersistentClass) i.next();
            for (Iterator j = pc.getPropertyIterator(); j.hasNext();) {
                Property p = (Property) j.next();
                for (Iterator k = p.getColumnIterator(); k.hasNext();) {
                    Selectable c = (Selectable) k.next();
                    if (c instanceof Formula) {
                        Formula f = (Formula) c;
                        if (f.getFormula() != null && f.getFormula().indexOf("%SCHEMA%") >= 0) {
                            f.setFormula(f.getFormula().replaceAll("%SCHEMA%", schema));
                            sLog.debug("Schema updated in " + pc.getClassName() + "." + p.getName() + " to "
                                    + f.getFormula());
                        }
                    }
                }
            }
        }
    }
}

From source file:org.web4thejob.orm.UniqueKeyConstraintImpl.java

License:Open Source License

public PropertyMetadata getPropertyForColumn(Column column) {
    PersistentClass pc = ContextUtil.getBean(HibernateConfiguration.class).getConfiguration()
            .getClassMapping(entityMetadata.getName());

    while (pc != null) {
        for (Iterator<?> iterProps = pc.getPropertyIterator(); iterProps.hasNext();) {
            Property property = (Property) iterProps.next();
            for (Iterator<?> iterCols = property.getColumnIterator(); iterCols.hasNext();) {
                Column col = (Column) iterCols.next();
                if (col.equals(column)) {
                    return entityMetadata.getPropertyMetadata(property.getName());
                }//  w  ww  . j a v a 2s  .c  o m
            }
        }

        pc = pc.getSuperclass();
    }

    return null;
}

From source file:org.zht.framework.service.impl.BaseServiceImpl.java

License:Apache License

private String checkLenth(M m) {
    // final PersistentClass clazz = factory.getConfiguration().getClassMapping(m.getClass().getName()); 
    if (clazz == null) {
        throw new ServiceLogicalException("[]???");
    }// w w  w  . jav  a 2s. com
    // final Table table = clazz.getTable(); 
    //          if(table==null){
    //             throw new ServiceLogicalException("[]???");
    //          }
    @SuppressWarnings("unchecked")
    final Iterator<Property> iterator = clazz.getPropertyIterator();
    if (iterator == null) {
        throw new ServiceLogicalException(
                "[]???");
    }
    // MethodAccess access = MethodAccess.get(m.getClass());
    while (iterator.hasNext()) {
        Property property = iterator.next();
        if ("id".equals(property.getName())) {
            continue;
        }
        Iterator<?> columnIterator = property.getColumnIterator();
        if (columnIterator.hasNext()) {
            Column column = (Column) columnIterator.next();
            int lenth = column.getLength();
            Object value = (Object) access.invoke(m, "get" + ZStrUtil.toUpCaseFirst(property.getName()));
            if (value != null) {
                if (value instanceof java.lang.String) {
                    if (lenth < (((java.lang.String) value).length())) {
                        return (" [" + property.getName() + "] ??");
                    }

                }
            }
        }
    }
    return null;
}

From source file:org.zht.framework.service.impl.BaseServiceImpl.java

License:Apache License

private String checkNotNull(M m) {
    // final PersistentClass clazz = factory.getConfiguration().getClassMapping(m.getClass().getName()); 
    if (clazz == null) {
        throw new ServiceLogicalException("[]???");
    }/*from   ww w  . ja va  2s  . c  o m*/
    //final Table table = clazz.getTable(); 
    //          if(table==null){
    //             throw new ServiceLogicalException("[]???");
    //          }
    @SuppressWarnings("unchecked")
    final Iterator<Property> iterator = clazz.getPropertyIterator();
    if (iterator == null) {
        throw new ServiceLogicalException(
                "[]???");
    }
    // MethodAccess access = MethodAccess.get(m.getClass());
    while (iterator.hasNext()) {
        Property property = iterator.next();
        if ("id".equals(property.getName())) {
            continue;
        }
        Iterator<?> columnIterator = property.getColumnIterator();
        if (columnIterator.hasNext()) {
            Column column = (Column) columnIterator.next();
            if (!column.isNullable()) {

                Object value = (Object) access.invoke(m, "get" + ZStrUtil.toUpCaseFirst(property.getName()));
                //System.out.println(property.getName()+" "+value);
                if (ZStrUtil.trimToNullIfStr(value) == null) {
                    return (" [" + property.getName() + "] ?");
                }

            }
        }
    }
    return null;
}

From source file:org.zht.framework.service.impl.BaseServiceImpl.java

License:Apache License

@SuppressWarnings("unchecked")
private String checkmanyTone$OneToOneNullForeign(M m) {
    // final PersistentClass clazz = factory.getConfiguration().getClassMapping(m.getClass().getName()); 
    if (clazz == null) {
        throw new ServiceLogicalException("[]???");
    }//from  w  ww.  ja v a  2s . c  o m
    final Iterator<Property> iterator = clazz.getPropertyIterator();
    if (iterator == null) {
        throw new ServiceLogicalException(
                "[]???");
    }
    // MethodAccess access = MethodAccess.get(m.getClass());
    while (iterator.hasNext()) {
        Property property = iterator.next();
        if ("id".equals(property.getName())) {
            continue;
        }
        Field field = null;
        try {
            field = m.getClass().getDeclaredField(property.getName());
            if (!(field.isAnnotationPresent(javax.persistence.ManyToOne.class)
                    || field.isAnnotationPresent(javax.persistence.OneToOne.class))) {
                continue;
            }
        } catch (Exception e) {
            continue;
        }
        Iterator<?> columnIterator = property.getColumnIterator();
        if (columnIterator.hasNext()) {
            Column column = (Column) columnIterator.next();
            Object value = (Object) access.invoke(m, "get" + ZStrUtil.toUpCaseFirst(property.getName()));
            if (!column.isNullable()) {
                if (ZStrUtil.trimToNullIfStr(value) == null) {
                    return (" [" + property.getName() + "] ?");
                }
                MethodAccess accessZZZ = MethodAccess.get(value.getClass());
                Object foreignModelId = (Object) accessZZZ.invoke(value, "getId");
                if (foreignModelId == null) {
                    return (" [" + property.getName() + "] ?");
                }
            } else {
                if (ZStrUtil.trimToNullIfStr(value) != null) {
                    Object foreignModelId = null;
                    foreignModelId = ZPropertyUtil.getInvokeValue(value, "getId");

                    if (foreignModelId != null && foreignModelId.equals(-972855736L)) {
                        throw new ServiceLogicalException("[]??");
                    }
                    if (foreignModelId == null) {
                        value = null;
                        access.invoke(m, "set" + ZStrUtil.toUpCaseFirst(property.getName()), value);
                    }
                    //                     
                }
                //                  else{
                //                     valueTemp=null;
                //                     access.invoke(m, "set"+ZStrUtil.toUpCaseFirst(property.getName()),valueTemp);
                //                  }
            }
        }
    }
    return null;
}