Example usage for org.hibernate.mapping Property getValue

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

Introduction

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

Prototype

public Value getValue() 

Source Link

Usage

From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java

License:Open Source License

/** Sets the tuplizer */
protected void setTuplizer() {
    for (Iterator<?> pcs = getClassMappings(); pcs.hasNext();) {
        final PersistentClass pc = (PersistentClass) pcs.next();
        if (pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) != null) { // featuremap
            // entry
            pc.addTuplizer(EntityMode.MAP,
                    getHbContext().getFeatureMapEntryTuplizer(getHibernateConfiguration()).getName());
        } else if (pc.getMetaAttribute(HbMapperConstants.ECLASS_NAME_META) != null) {
            // only the pc's with this meta should get a tuplizer

            pc.addTuplizer(EntityMode.MAP,
                    getHbContext().getEMFTuplizerClass(getHibernateConfiguration()).getName());
            pc.addTuplizer(EntityMode.POJO,
                    getHbContext().getEMFTuplizerClass(getHibernateConfiguration()).getName());
        } else if (pc.getMetaAttribute(HbMapperConstants.ECLASS_NAME_META) == null) {
            // don't change these pc's any further, these are not eclasses
            continue;
        }//from  w  ww.  ja v  a2s  .com

        // also set the tuplizer for the components, and register for the
        // component

        // Build a list of all properties.
        java.util.List<Property> properties = new ArrayList<Property>();
        final Property identifierProperty = pc.getIdentifierProperty();
        if (identifierProperty != null) {
            properties.add(identifierProperty);
        }
        for (Iterator<?> it = pc.getPropertyIterator(); it.hasNext();) {
            properties.add((Property) it.next());
        }

        // Now set component tuplizers where necessary.
        for (Object name2 : properties) {
            Property prop = (Property) name2;
            if (prop.getName().compareTo("_identifierMapper") == 0) {
                continue; // ignore this one
            }
            final Value value = prop.getValue();
            if (value instanceof Component) {
                setComponentTuplizer((Component) value, getHibernateConfiguration());
            } else if (value instanceof Collection && ((Collection) value).getElement() instanceof Component) {
                setComponentTuplizer((Component) ((Collection) value).getElement(),
                        getHibernateConfiguration());
            }
        }
    }
}

From source file:org.eclipse.emf.teneo.hibernate.HbDataStore.java

License:Open Source License

/**
 * Computes the referers, handles the lazy for containment
 *///w w w.ja  v  a 2s . co m
protected HashMap<String, java.util.List<ReferenceTo>> computeReferers() {
    final HashMap<String, java.util.List<ReferenceTo>> result = new HashMap<String, java.util.List<ReferenceTo>>();

    final Iterator<?> it = getClassMappings();
    final ArrayList<String> fmes = new ArrayList<String>();
    while (it.hasNext()) {
        final PersistentClass pc = (PersistentClass) it.next();

        // keep track which are the feature map entries
        if (pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) != null) {
            fmes.add(getMappedName(pc));
        }

        // everyone should have a list otherwise the copying of referers to
        // super types to
        // this type does not work
        if (result.get(getMappedName(pc)) == null) {
            result.put(getMappedName(pc), new ArrayList<ReferenceTo>());
        }

        final Iterator<?> propIt = pc.getPropertyIterator();
        while (propIt.hasNext()) {
            // handle few cases
            // OneToOne or ManyToOne, referenced class can be obtained from
            // Value and then getReferencedEntityName
            // List: in this case search for a structural feature and get
            // the EType from it
            // if no structural feature then use the type name and hope for
            // the best.

            final Property prop = (Property) propIt.next();
            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());
            try {
                String toEntity = "";
                boolean isContainer = false;
                boolean isMany = false;

                if (prop.getValue() instanceof ManyToOne) {
                    final ManyToOne mto = (ManyToOne) prop.getValue();
                    toEntity = mto.getReferencedEntityName();
                    if (ef != null) {
                        isContainer = ef instanceof EReference && ((EReference) ef).isContainment();
                    } else {
                        isContainer = prop.getCascadeStyle().hasOrphanDelete()
                                || prop.getCascade().compareTo("all") == 0; // ugly
                        // but
                    }
                    // this was
                    // the only
                    // way to
                    // get all
                    // there!
                } else if (prop.getValue() instanceof OneToOne) {
                    final OneToOne oto = (OneToOne) prop.getValue();
                    toEntity = oto.getReferencedEntityName();
                    if (ef != null) {
                        isContainer = ef instanceof EReference && ((EReference) ef).isContainment();
                    } else {
                        isContainer = prop.getCascadeStyle().hasOrphanDelete()
                                || prop.getCascadeStyle() == CascadeStyle.ALL;
                    }
                } else if (prop.getValue() instanceof Collection) {
                    isMany = true;
                    if (ef == null) { // TODO can this happen?
                        isContainer = prop.getCascadeStyle().hasOrphanDelete()
                                || prop.getCascadeStyle() == CascadeStyle.ALL;
                        if (((Collection) prop.getValue()).getElement() instanceof OneToMany) {
                            final Collection coll = (Collection) prop.getValue();
                            toEntity = ((OneToMany) coll.getElement()).getReferencedEntityName();
                        } else if (((Collection) prop.getValue()).getElement() instanceof ManyToOne) {
                            final Collection coll = (Collection) prop.getValue();
                            toEntity = ((ManyToOne) coll.getElement()).getReferencedEntityName();
                        } else {
                            continue;
                            // throw new HbMapperException("Type "
                            // + ((Collection)
                            // prop.getValue()).getElement().getClass().getName()
                            // + " not supported, property " +
                            // prop.getName());
                        }
                    } else {
                        // in case of featuremap set containment always on
                        // true because only the featuremap entries
                        // themselves know if they are containment
                        if (ef instanceof EAttribute
                                && ((EAttribute) ef).getEType().getInstanceClass() == Entry.class) {
                            isContainer = true;
                            // composite-elements are not supported.
                            if (!(((Collection) prop.getValue()).getElement() instanceof OneToMany)) {
                                continue;
                            }
                            final OneToMany otm = (OneToMany) ((Collection) prop.getValue()).getElement();
                            toEntity = otm.getReferencedEntityName();
                        } else if (ef instanceof EReference) {
                            final EReference er = (EReference) ef;
                            isContainer = er.isContainment(); // prop.getCascadeStyle().
                            // hasOrphanDelete()
                            // ||
                            // prop.getCascadeStyle()
                            // ==
                            // CascadeStyle.ALL;
                            toEntity = getEntityNameStrategy()
                                    .toEntityName(((EReference) ef).getEReferenceType());
                        } else if (ef instanceof EAttribute && ef.getEType() instanceof EClass) { // TODO
                            // can
                            // this
                            // ever
                            // happen?
                            isContainer = true; // prop.getCascadeStyle().hasOrphanDelete()
                            // || prop.getCascadeStyle()
                            // == CascadeStyle.ALL;
                            toEntity = getEntityNameStrategy().toEntityName((EClass) ef.getEType());
                        }
                        // filter out non eobjects
                        else {
                            continue;
                        }
                    }
                } else {
                    continue;
                }

                java.util.List<ReferenceTo> list = result.get(toEntity);
                if (list == null) {
                    list = new ArrayList<ReferenceTo>();
                    result.put(toEntity, list);
                }

                list.add(new ReferenceTo(getMappedName(pc), prop, isContainer, isMany, toEntity));
            } catch (StoreClassLoadException e) {
                throw new HbMapperException("Class not found using property: " + prop.getName() + " of " + prop,
                        e);
            }
        }
    }

    // at the end for each class all the refersto of superclasses and
    // interfaces are added also
    final ArrayList<EClass> classDone = new ArrayList<EClass>();
    for (String em : result.keySet()) {
        // only do this if not a fme
        if (!fmes.contains(em)) {
            setRefersToOfSupers(em, result, classDone);
        }
    }
    return result;
}

From source file:org.eclipse.emf.teneo.hibernate.HbUtil.java

License:Open Source License

/** Returns the correct accessor on the basis of the type of property */
public static PropertyAccessor getPropertyAccessor(Property mappedProperty, HbDataStore ds, String entityName,
        EClass mappedEClass) {/*from  w  w  w  . java 2 s  .com*/
    if (mappedProperty.getMetaAttribute(HbConstants.SYNTHETIC_PROPERTY_INDICATOR) != null) { // synthetic
        return new SyntheticPropertyHandler(mappedProperty.getName());
    } else if (mappedProperty.getMetaAttribute(HbMapperConstants.ID_META) != null) { // synthetic
        // ID
        return new IdentifierPropertyHandler();
    } else if (mappedProperty.getMetaAttribute(HbMapperConstants.VERSION_META) != null) {
        return ds.getHbContext().createVersionAccessor();
    } else if (mappedProperty.getName().compareToIgnoreCase("_identifierMapper") == 0) { // name
        // is
        // used
        // by
        // hb
        return new EmbeddedPropertyAccessor(); // new
        // DummyPropertyHandler();
    } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER) == 0) {
        return ds.getHbContext().createEContainerAccessor();
    } else if (mappedProperty.getName()
            .compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_NAME) == 0) {
        return ds.getExtensionManager().getExtension(NewEContainerFeatureIDPropertyHandler.class);
    } else if (mappedProperty.getName().compareToIgnoreCase(HbConstants.PROPERTY_ECONTAINER_FEATURE_ID) == 0) {
        return ds.getHbContext().createEContainerFeatureIDAccessor();
    }

    EClass eClass = null;
    if (mappedEClass != null) {
        eClass = mappedEClass;
    } else {
        eClass = ds.getEntityNameStrategy().toEClass(entityName);
        if (eClass == null) {
            // for components this is the case
            eClass = ERuntime.INSTANCE.getEClass(entityName);
        }
    }
    final EStructuralFeature efeature = StoreUtil.getEStructuralFeature(eClass, mappedProperty.getName());

    if (efeature == null) {
        throw new HbMapperException("Feature not found for eclass/entity/property " + eClass.getName() + "/"
                + entityName + "/" + mappedProperty.getName());
    }

    if (log.isDebugEnabled()) {
        log.debug("Creating property accessor for " + mappedProperty.getName() + "/" + entityName + "/"
                + efeature.getName());
    }

    // check extra lazy
    final boolean extraLazy = mappedProperty.getValue() instanceof Collection
            && ((Collection) mappedProperty.getValue()).isExtraLazy();

    if (FeatureMapUtil.isFeatureMap(efeature)) {
        return ds.getHbContext().createFeatureMapPropertyAccessor(efeature);
    } else if (efeature instanceof EReference) {
        final EReference eref = (EReference) efeature;
        if (eref.isMany()) {
            return ds.getHbContext().createEListAccessor(efeature, extraLazy,
                    ds.getPersistenceOptions().isMapEMapAsTrueMap());
        } else {
            PropertyAccessor erefPropertyHandler = ds.getHbContext().createEReferenceAccessor(eref);
            if (erefPropertyHandler instanceof EReferencePropertyHandler) {
                if (mappedProperty.getPersistentClass() != null) {
                    ((EReferencePropertyHandler) erefPropertyHandler).setId(
                            mappedProperty == mappedProperty.getPersistentClass().getIdentifierProperty());
                }
            }
            return erefPropertyHandler;
        }
    } else {
        final EAttribute eattr = (EAttribute) efeature;
        if (eattr.isMany()) {
            return ds.getHbContext().createEListAccessor(efeature, extraLazy,
                    ds.getPersistenceOptions().isMapEMapAsTrueMap());
        } else {
            // note also array types are going here!
            final PropertyAccessor pa = ds.getHbContext().createEAttributeAccessor(eattr);
            // note this check is necessary because maybe somebody override
            // HBContext.createEAttributeAccessor
            // to not return a EAttributePropertyHandler
            if (pa instanceof EAttributePropertyHandler) {
                final EAttributePropertyHandler eAttributePropertyHandler = (EAttributePropertyHandler) pa;
                eAttributePropertyHandler.setPersistenceOptions(ds.getPersistenceOptions());
                if (mappedProperty.getPersistentClass() != null) {
                    eAttributePropertyHandler.setId(
                            mappedProperty == mappedProperty.getPersistentClass().getIdentifierProperty());
                }
            }
            return pa;
        }
    }
}

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

License:Apache License

protected boolean isBidirectionalManyToOneWithListMapping(PersistentProperty grailsProperty, Property prop) {
    if (grailsProperty instanceof Association) {

        Association association = (Association) grailsProperty;
        Association otherSide = association.getInverseSide();
        return association.isBidirectional() && otherSide != null && prop.getValue() instanceof ManyToOne
                && List.class.isAssignableFrom(otherSide.getType());
    }/* w  ww . ja  v a 2s.c om*/
    return false;
}

From source file:org.jboss.forge.addon.database.tools.generate.DatabaseTableSelectionStep.java

License:Open Source License

private boolean isSelected(Collection<String> selection, POJOClass element) {
    boolean result = false;
    if (element instanceof ComponentPOJOClass) {
        ComponentPOJOClass cpc = (ComponentPOJOClass) element;
        Iterator<?> iterator = cpc.getAllPropertiesIterator();
        result = true;/*from   w  w w.j  a  v  a 2 s. c o  m*/
        while (iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof Property) {
                Property property = (Property) object;
                String tableName = property.getValue().getTable().getName();
                if (!selection.contains(tableName)) {
                    result = false;
                    break;
                }
            }
        }
    } else if (element instanceof EntityPOJOClass) {
        EntityPOJOClass epc = (EntityPOJOClass) element;
        Object object = epc.getDecoratedObject();
        if (object instanceof PersistentClass) {
            PersistentClass pc = (PersistentClass) object;
            Table table = pc.getTable();
            if (selection.contains(table.getName())) {
                result = true;
            }
        }
    }
    return result;
}

From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.OrmShape.java

License:Open Source License

/**
 * creates children of the shape, //from  w  w w  .j  a  v a  2 s .c o  m
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void initModel() {
    Object ormElement = getOrmElement();
    if (ormElement instanceof RootClass) {
        RootClass rootClass = (RootClass) ormElement;
        Property identifierProperty = rootClass.getIdentifierProperty();
        if (identifierProperty != null) {
            addChild(new Shape(identifierProperty, getConsoleConfigName()));
        }

        KeyValue identifier = rootClass.getIdentifier();
        if (identifier instanceof Component) {
            Component component = (Component) identifier;
            if (component.isEmbedded()) {
                Iterator<Property> iterator = ((Component) identifier).getPropertyIterator();
                while (iterator.hasNext()) {
                    Property property = iterator.next();
                    addChild(new Shape(property, getConsoleConfigName()));
                }
            }
        }

        Iterator<Property> iterator = rootClass.getPropertyIterator();
        while (iterator.hasNext()) {
            Property field = iterator.next();
            if (!field.isBackRef()) {
                if (!field.isComposite()) {
                    final Value val = field.getValue();
                    Shape bodyOrmShape = null;
                    if (val.isSimpleValue() && !((SimpleValue) val).isTypeSpecified()) {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    } else {
                        if (val instanceof Collection) {
                            bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
                        } else {
                            Type type = getTypeUsingExecContext(val);
                            if (type != null && type.isEntityType()) {
                                bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                            } else {
                                bodyOrmShape = new Shape(field, getConsoleConfigName());
                            }
                        }
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
    } else if (ormElement instanceof Subclass) {
        RootClass rootClass = ((Subclass) ormElement).getRootClass();

        Property identifierProperty = rootClass.getIdentifierProperty();
        if (identifierProperty != null) {
            addChild(new Shape(identifierProperty, getConsoleConfigName()));
        }

        KeyValue identifier = rootClass.getIdentifier();
        if (identifier instanceof Component) {
            Iterator<Property> iterator = ((Component) identifier).getPropertyIterator();
            while (iterator.hasNext()) {
                Property property = iterator.next();
                addChild(new Shape(property, getConsoleConfigName()));
            }
        }

        Iterator<Property> iterator = rootClass.getPropertyIterator();
        while (iterator.hasNext()) {
            Property field = iterator.next();
            if (!field.isBackRef()) {
                if (!field.isComposite()) {

                    boolean typeIsAccessible = true;
                    if (field.getValue().isSimpleValue()
                            && ((SimpleValue) field.getValue()).isTypeSpecified()) {
                        try {
                            field.getValue().getType();
                        } catch (Exception e) {
                            typeIsAccessible = false;
                        }
                    }
                    Shape bodyOrmShape = null;
                    if (typeIsAccessible && field.getValue().isSimpleValue()) {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    } else if (typeIsAccessible && field.getValue().getType().isEntityType()) {
                        bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    } else if (typeIsAccessible && field.getValue().getType().isCollectionType()) {
                        bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
                    } else {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
        Iterator<Property> iter = ((Subclass) ormElement).getPropertyIterator();
        while (iter.hasNext()) {
            Property property = iter.next();
            if (!property.isBackRef()) {
                if (!property.isComposite()) {

                    boolean typeIsAccessible = true;
                    if (property.getValue().isSimpleValue()
                            && ((SimpleValue) property.getValue()).isTypeSpecified()) {
                        try {
                            property.getValue().getType();
                        } catch (Exception e) {
                            typeIsAccessible = false;
                        }
                    }
                    Shape bodyOrmShape = null;
                    if (typeIsAccessible && property.getValue().getType().isEntityType()) {
                        bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
                    } else if (typeIsAccessible && property.getValue().getType().isCollectionType()) {
                        bodyOrmShape = new ComponentShape(property, getConsoleConfigName());
                    } else {
                        bodyOrmShape = new Shape(property, getConsoleConfigName());
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
    } else if (ormElement instanceof Table) {
        Iterator iterator = ((Table) getOrmElement()).getColumnIterator();
        while (iterator.hasNext()) {
            Column column = (Column) iterator.next();
            Shape bodyOrmShape = new Shape(column, getConsoleConfigName());
            addChild(bodyOrmShape);
        }
    }
}

From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.SpecialOrmShape.java

License:Open Source License

/**
 * creates children of the shape, //from www .  j  a  v a2  s.  c o  m
 */
@SuppressWarnings("unchecked")
@Override
protected void initModel() {
    RootClass rootClass = (RootClass) getOrmElement();
    Property identifierProperty = rootClass.getIdentifierProperty();
    if (identifierProperty != null) {
        addChild(new Shape(identifierProperty, getConsoleConfigName()));
    }

    SpecialRootClass src = (SpecialRootClass) getOrmElement();
    if (src.getParentProperty() != null) {
        Shape bodyOrmShape = new Shape(src.getParentProperty(), getConsoleConfigName());
        addChild(bodyOrmShape);
        parentShape = bodyOrmShape;
    }

    Iterator<Property> iterator = rootClass.getPropertyIterator();
    while (iterator.hasNext()) {
        Property field = iterator.next();
        Type type = getTypeUsingExecContext(field.getValue());
        Shape bodyOrmShape = null;
        if (type != null && type.isEntityType()) {
            bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
        } else if (type != null && type.isCollectionType()) {
            bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
        } else {
            bodyOrmShape = new Shape(field, getConsoleConfigName());
        }
        addChild(bodyOrmShape);
    }
}

From source file:org.jboss.tools.hibernate.ui.diagram.editors.model.SpecialRootClass.java

License:Open Source License

@SuppressWarnings("unchecked")
private void generate() {
    if (property == null) {
        return;//from ww w.  jav a  2 s  . c om
    }
    Component component = null;
    if (property.getValue() instanceof Collection) {
        Collection collection = (Collection) property.getValue();
        component = (Component) collection.getElement();
    } else if (property.getValue() instanceof Component) {
        component = (Component) property.getValue();
    }
    if (component != null) {
        setClassName(component.getComponentClassName());
        setEntityName(component.getComponentClassName());
        PersistentClass ownerClass = component.getOwner();
        if (component.getParentProperty() != null) {
            parentProperty = new Property();
            parentProperty.setName(component.getParentProperty());
            parentProperty.setPersistentClass(ownerClass);
        }
        Iterator<Property> iterator = component.getPropertyIterator();
        while (iterator.hasNext()) {
            Property property = iterator.next();
            if (property != null) {
                addProperty(property);
            }
        }
    }
}

From source file:org.jboss.tools.hibernate3_6.console.EclipseHQLCompletionRequestor.java

License:Open Source License

private Image getImage(HQLCompletionProposal proposal) {
    String key = null;//from  w  w w. j a v a 2  s  .  co  m

    switch (proposal.getCompletionKind()) {
    case HQLCompletionProposal.ENTITY_NAME:
    case HQLCompletionProposal.ALIAS_REF:
        key = ImageConstants.MAPPEDCLASS;
        break;
    case HQLCompletionProposal.PROPERTY:
        Property property = proposal.getProperty();
        if (property != null) {
            if (property.getPersistentClass() != null
                    && property.getPersistentClass().getIdentifierProperty() == property) {
                key = ImageConstants.IDPROPERTY;
            } else {
                key = getIconNameForValue(property.getValue());
            }
        } else {
            key = ImageConstants.PROPERTY;
        }
        break;
    case HQLCompletionProposal.KEYWORD:
        key = null;
        break;
    case HQLCompletionProposal.FUNCTION:
        key = ImageConstants.FUNCTION;
        break;
    default:
        key = null;
    }

    return key == null ? null : EclipseImages.getImage(key);
}

From source file:org.jdbcluster.template.hibernate.HibernateConfiguration.java

License:Apache License

/**
 * calculated the max length of an attribute of type String
 * @param c Cluster that holds the attribute
 * @param attributeName name of the attribute
 * @return max allowed length of the attribute
 *///from  w  w w.j  a v  a2s.c  om
public int getLenthOfStringAttribute(ICluster c, String attributeName) {

    Assert.notNull(c, "c may not be null");
    Assert.notNull(attributeName, "attributeName may not be null");

    PersistentClass pc = cfg.getClassMapping(c.getDaoClass().getName());

    if (pc == null)
        throw new ConfigurationException(
                "Persistent Dao Class from Cluster " + c.getClass().getName() + " is not Hibernate configured");

    Property p = pc.getProperty(attributeName);

    if (p == null)
        throw new ConfigurationException(
                "Property " + attributeName + " in cluster " + c.getClass().getName() + " not found");

    Iterator<Column> i = (Iterator<Column>) p.getValue().getColumnIterator();
    Column column = i.next();
    return column.getLength();
}