Example usage for org.hibernate.mapping PersistentClass getMetaAttribute

List of usage examples for org.hibernate.mapping PersistentClass getMetaAttribute

Introduction

In this page you can find the example usage for org.hibernate.mapping PersistentClass getMetaAttribute.

Prototype

public MetaAttribute getMetaAttribute(String name) 

Source Link

Usage

From source file:org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.CDORevisionTuplizer.java

License:Open Source License

private void initEClass(PersistentClass mappingInfo) {
    if (eClass != null) {
        return;/*  w  w  w  .j  a v a  2 s. co  m*/
    }

    HibernateStore hbStore = HibernateStore.getCurrentHibernateStore();

    // find the EClass/Package
    String entityName = mappingInfo.getEntityName();
    String ePackageURI = mappingInfo.getMetaAttribute(EPACKAGE_META).getValue();
    String eClassName = mappingInfo.getMetaAttribute(ECLASSNAME_META).getValue();

    if (ePackageURI == null || eClassName == null) {
        throw new IllegalArgumentException("The mapping for the persistentclass " + mappingInfo.getEntityName() //$NON-NLS-1$
                + " is incorrect, there should be meta data tags for both epackage and " //$NON-NLS-1$
                + "eclassname, one or both are missing."); //$NON-NLS-1$
    }

    if (TRACER.isEnabled()) {
        TRACER.trace("EntityName/eclassname/packageURI " + entityName + "/" + eClassName + "/" + ePackageURI); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    for (EPackage ePackage : hbStore.getPackageHandler().getEPackages()) {
        if (ePackage.getNsURI().compareTo(ePackageURI) != 0) {
            continue;
        }

        for (EClass localCdoClass : EMFUtil.getPersistentClasses(ePackage)) {
            if (localCdoClass.getName().compareTo(eClassName) == 0) {
                eClass = localCdoClass;
                break;
            }
        }
    }

    if (eClass == null && ePackageURI.compareTo(EresourcePackage.eINSTANCE.getNsURI()) == 0) {
        for (EClass localCdoClass : EMFUtil.getPersistentClasses(EresourcePackage.eINSTANCE)) {
            if (localCdoClass.getName().compareTo(eClassName) == 0) {
                eClass = localCdoClass;
                if (TRACER.isEnabled()) {
                    TRACER.trace("Class is CDOResource class"); //$NON-NLS-1$
                }

                break;
            }
        }
    }

    // add the entityName <--> EClass mapping
    HibernateStore.getCurrentHibernateStore().addEntityNameEClassMapping(entityName, eClass);

    if (eClass == null) {
        throw new IllegalArgumentException("The mapped class " + mappingInfo.getEntityName() //$NON-NLS-1$
                + " does not have a eClass equivalent"); //$NON-NLS-1$
    }
}

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.
 * /*from  w w w .  ja  v a2 s .com*/
 * 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

/** 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;
        }//  w  ww. j  ava2 s.  co  m

        // 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
 *///from  w w  w .  j  av a2  s  .c o 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 meta class uri, if not found then null is returned */
public static String getEClassNameFromFeatureMapMeta(PersistentClass pc) {
    MetaAttribute ma = pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META);
    if (ma == null) {
        return null;
    }/*ww w .  j  ava2  s  .  c  om*/
    return ma.getValue();
}

From source file:org.eclipse.emf.teneo.hibernate.mapping.elist.FeatureMapEntryInstantiator.java

License:Open Source License

/** Constructor */
public FeatureMapEntryInstantiator(PersistentClass pc) {
    AssertUtil.assertTrue(pc.getEntityName() + " does not have a meta attribute",
            pc.getMetaAttribute(HbMapperConstants.FEATUREMAP_META) != null);
    if (log.isDebugEnabled()) {
        log.debug("Creating fme instantiator for " + pc.getEntityName());
    }/*from  w  w w .jav  a  2  s .c  o m*/
    proxyInterface = pc.getProxyInterface();
    persistentClass = pc;
}

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

License:Open Source License

public static String getMetaAttribute(PersistentClass persistentClass, String name) {
    return persistentClass.getMetaAttribute(name).getValue();
}

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

License:Open Source License

public static boolean hasMetaAttribute(PersistentClass persistentClass, String name) {
    final MetaAttribute metaAttribute = persistentClass.getMetaAttribute(name);
    if (metaAttribute != null) {
        try {//from w  w w.j  a  v a  2  s. c  om
            return metaAttribute.getValue() != null;
        } catch (final Exception e) {
            return false;
        }
    }
    return false;
}