Example usage for org.hibernate.mapping Property getCascadeStyle

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

Introduction

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

Prototype

public CascadeStyle getCascadeStyle() throws MappingException 

Source Link

Usage

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

License:Open Source License

/**
 * Computes the referers, handles the lazy for containment
 *//*from www .  j  a va2 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:tools.xor.util.Hibernate4Support.java

License:Apache License

@Override
public boolean isCascaded(Configuration configuration, String entityName, String inputPropertyName) {

    if (configuration.getClassMapping(entityName) == null)
        return false;

    Property property = configuration.getClassMapping(entityName).getProperty(inputPropertyName);
    CascadeStyle style = property.getCascadeStyle();

    return style.doCascade(CascadingActions.SAVE_UPDATE) || style.doCascade(CascadingActions.PERSIST);
}