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.beangle.orm.hibernate.tool.DdlGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void commentProperty(Class<?> clazz, Table table, Property p) {
    if (null == p)
        return;/*from w  ww  .  j av a2 s.c o  m*/
    if (p.getColumnSpan() == 1) {
        Column column = (Column) p.getColumnIterator().next();
        if (isForeignColumn(table, column)) {
            column.setComment(messages.get(clazz, p.getName()) + " ID");
        } else {
            column.setComment(messages.get(clazz, p.getName()));
        }
    } else if (p.getColumnSpan() > 1) {
        Component pc = ((Component) p.getValue());
        Class<?> columnOwnerClass = pc.getComponentClass();
        commentProperties(columnOwnerClass, table, pc.getPropertyIterator());
    }
}

From source file:org.beangle.orm.hibernate.tool.HbmGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
public void gen(String file) throws Exception {
    hbconfig = new OverrideConfiguration();
    hbconfig.getProperties().put(Environment.DIALECT, new Oracle10gDialect());
    ConfigBuilder.build(hbconfig);// ww  w.  j  a va 2  s .  co m
    freemarkerConfig = new freemarker.template.Configuration();
    freemarkerConfig.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));

    Iterator<PersistentClass> iter = hbconfig.getClassMappings();
    List<PersistentClass> pcs = CollectUtils.newArrayList();
    while (iter.hasNext()) {
        PersistentClass pc = iter.next();
        Class<?> cls = pc.getMappedClass();
        Iterator<Property> pi = pc.getPropertyIterator();
        // For AnnotationBinder don't set column'length and nullable in ,let's we do it.
        while (pi.hasNext()) {
            Property p = pi.next();
            if (p.getColumnSpan() != 1)
                continue;
            Column column = (Column) p.getColumnIterator().next();
            if (column.getLength() == Column.DEFAULT_LENGTH) {
                Size size = findAnnotation(cls, Size.class, p.getName());
                if (null != size)
                    column.setLength(size.max());
            }
            if (column.isNullable()) {
                NotNull notnull = findAnnotation(cls, NotNull.class, p.getName());
                if (null != notnull)
                    column.setNullable(false);
            }
        }
        if (!pc.getClassName().contains(".example."))
            pcs.add(pc);
    }
    Map<String, Object> data = CollectUtils.newHashMap();
    data.put("classes", pcs);
    data.put("generator", this);
    Template freemarkerTemplate = freemarkerConfig.getTemplate("/hbm.ftl");
    FileWriter fw = new FileWriter("/tmp/hibernate.hbm.xml");
    freemarkerTemplate.process(data, fw);
}

From source file:org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl.java

License:Apache License

protected FieldMetadata getFieldMetadata(String prefix, String propertyName, List<Property> componentProperties,
        SupportedFieldType type, SupportedFieldType secondaryType, Type entityType, Class<?> targetClass,
        FieldPresentationAttributes presentationAttribute, MergedPropertyType mergedPropertyType)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
        IllegalAccessException, InvocationTargetException {
    FieldMetadata fieldMetadata = new FieldMetadata();
    fieldMetadata.setFieldType(type);//from  w w w . j  av a2 s. c  o  m
    fieldMetadata.setSecondaryType(secondaryType);
    if (entityType != null && !entityType.isCollectionType()) {
        Column column = null;
        for (Property property : componentProperties) {
            if (property.getName().equals(propertyName)) {
                column = (Column) property.getColumnIterator().next();
                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.setCollection(false);
    } else {
        fieldMetadata.setCollection(true);
    }
    fieldMetadata.setMutable(true);
    fieldMetadata.setInheritedFromType(targetClass.getName());
    fieldMetadata.setAvailableToTypes(new String[] { targetClass.getName() });
    if (presentationAttribute != null) {
        fieldMetadata.setPresentationAttributes(presentationAttribute);
    }
    fieldMetadata.setMergedPropertyType(mergedPropertyType);
    if (SupportedFieldType.BROADLEAF_ENUMERATION.equals(type)) {
        setupBroadleafEnumeration(presentationAttribute.getBroadleafEnumeration(), fieldMetadata);
    }

    return fieldMetadata;
}

From source file:org.broadleafcommerce.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;/*from  w w  w  .  j a  va2 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 {
            setupBroadleafEnumeration(fieldMetadata.getBroadleafEnumeration(), fieldMetadata,
                    addMetadataFromMappingDataRequest.getDynamicEntityDao());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return FieldProviderResponse.HANDLED;
}

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

License:Apache License

@SuppressWarnings("unchecked")
protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {/*from  w w w  .jav a2  s. c  o m*/
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new GrailsDomainException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new GrailsDomainException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}

From source file:org.codehaus.groovy.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   w ww  .j a v  a2  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());
    }

    setUniqueName(uk);

    table.addUniqueKey(uk);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

@SuppressWarnings("unchecked")
private static String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {//from w ww  .  j  a  va  2 s  .co m
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new GrailsDomainException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new GrailsDomainException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindNaturalIdentifier(Table table, Mapping mapping, PersistentClass persistentClass) {
    Object o = mapping != null ? mapping.getIdentity() : null;
    if (o instanceof Identity) {
        Identity identity = (Identity) o;
        final NaturalId naturalId = identity.getNatural();
        if (naturalId != null && !naturalId.getPropertyNames().isEmpty()) {
            UniqueKey uk = new UniqueKey();
            uk.setName("_UniqueKey");
            uk.setTable(table);/*w  ww . j a  va 2 s  .  c om*/

            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());
            }

            table.addUniqueKey(uk);
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinderTests.java

License:Apache License

/**
 * Tests that single- and multi-column user type mappings work
 * correctly. Also Checks that the "sqlType" property is honoured.
 *//* w w w.  jav a  2 s  .  c om*/
public void testUserTypeMappings() {
    DefaultGrailsDomainConfiguration config = getDomainConfig(MULTI_COLUMN_USER_TYPE_DEFINITION);
    PersistentClass persistentClass = config.getClassMapping("Item");

    // First check the "name" property and its associated column.
    Property nameProperty = persistentClass.getProperty("name");
    assertEquals(1, nameProperty.getColumnSpan());
    assertEquals("name", nameProperty.getName());

    Column column = (Column) nameProperty.getColumnIterator().next();
    assertEquals("s_name", column.getName());
    assertEquals("text", column.getSqlType());

    // Next the "other" property.
    Property otherProperty = persistentClass.getProperty("other");
    assertEquals(1, otherProperty.getColumnSpan());
    assertEquals("other", otherProperty.getName());

    column = (Column) otherProperty.getColumnIterator().next();
    assertEquals("other", column.getName());
    assertEquals("wrapper-characters", column.getSqlType());
    assertEquals(MyUserType.class.getName(), column.getValue().getType().getName());
    assertTrue(column.getValue() instanceof SimpleValue);
    SimpleValue v = (SimpleValue) column.getValue();
    assertEquals("myParam1", v.getTypeParameters().get("param1"));
    assertEquals("myParam2", v.getTypeParameters().get("param2"));

    // And now for the "price" property, which should have two
    // columns.
    Property priceProperty = persistentClass.getProperty("price");
    assertEquals(2, priceProperty.getColumnSpan());
    assertEquals("price", priceProperty.getName());

    Iterator colIter = priceProperty.getColumnIterator();
    column = (Column) colIter.next();
    assertEquals("value", column.getName());
    assertNull("SQL type should have been 'null' for 'value' column.", column.getSqlType());

    column = (Column) colIter.next();
    assertEquals("currency_code", column.getName());
    assertEquals("text", column.getSqlType());
}

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

License:Apache License

@SuppressWarnings("unchecked")
protected String buildOrderByClause(String hqlOrderBy, PersistentClass associatedClass, String role,
        String defaultOrder) {/*from www .  java2  s.  c o m*/
    String orderByString = null;
    if (hqlOrderBy != null) {
        List<String> properties = new ArrayList<String>();
        List<String> ordering = new ArrayList<String>();
        StringBuilder orderByBuffer = new StringBuilder();
        if (hqlOrderBy.length() == 0) {
            //order by id
            Iterator<?> it = associatedClass.getIdentifier().getColumnIterator();
            while (it.hasNext()) {
                Selectable col = (Selectable) it.next();
                orderByBuffer.append(col.getText()).append(" asc").append(", ");
            }
        } else {
            StringTokenizer st = new StringTokenizer(hqlOrderBy, " ,", false);
            String currentOrdering = defaultOrder;
            //FIXME make this code decent
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                if (isNonPropertyToken(token)) {
                    if (currentOrdering != null) {
                        throw new DatastoreConfigurationException(
                                "Error while parsing sort clause: " + hqlOrderBy + " (" + role + ")");
                    }
                    currentOrdering = token;
                } else {
                    //Add ordering of the previous
                    if (currentOrdering == null) {
                        //default ordering
                        ordering.add("asc");
                    } else {
                        ordering.add(currentOrdering);
                        currentOrdering = null;
                    }
                    properties.add(token);
                }
            }
            ordering.remove(0); //first one is the algorithm starter
            // add last one ordering
            if (currentOrdering == null) {
                //default ordering
                ordering.add(defaultOrder);
            } else {
                ordering.add(currentOrdering);
                currentOrdering = null;
            }
            int index = 0;

            for (String property : properties) {
                Property p = BinderHelper.findPropertyByName(associatedClass, property);
                if (p == null) {
                    throw new DatastoreConfigurationException("property from sort clause not found: "
                            + associatedClass.getEntityName() + "." + property);
                }
                PersistentClass pc = p.getPersistentClass();
                String table;
                if (pc == null) {
                    table = "";
                }

                else if (pc == associatedClass || (associatedClass instanceof SingleTableSubclass
                        && pc.getMappedClass().isAssignableFrom(associatedClass.getMappedClass()))) {
                    table = "";
                } else {
                    table = pc.getTable().getQuotedName() + ".";
                }

                Iterator<?> propertyColumns = p.getColumnIterator();
                while (propertyColumns.hasNext()) {
                    Selectable column = (Selectable) propertyColumns.next();
                    orderByBuffer.append(table).append(column.getText()).append(" ").append(ordering.get(index))
                            .append(", ");
                }
                index++;
            }
        }
        orderByString = orderByBuffer.substring(0, orderByBuffer.length() - 2);
    }
    return orderByString;
}