Package org.apache.openejb.jee.jpa

Examples of org.apache.openejb.jee.jpa.MappedSuperclass


     * @param className    The name of the class of interest.
     * @return Returns the superclass mapping for the named class, or
     * null if the class is not in the mapping set.
     */
    private MappedSuperclass removeMappedSuperclass(final EntityMappings userMappings, final String className) {
        final MappedSuperclass mappedSuperclass;

        mappedSuperclass = userMappings.getMappedSuperclassMap().get(className);
        if (mappedSuperclass != null) {
            userMappings.getMappedSuperclassMap().remove(mappedSuperclass.getKey());
        }
        return mappedSuperclass;
    }
View Full Code Here


        final Set<String> primaryKeyFields = new HashSet<String>();


        if (bean.getPrimkeyField() != null) {
            final String fieldName = bean.getPrimkeyField();
            final MappedSuperclass superclass = superclassByField.get(fieldName);
            // this need not be here...for CMP 2.x, these are generally autogenerated fields.
            if (superclass != null) {
                // ok, add this field to the superclass mapping
                superclass.addField(new Id(fieldName));
                // the direct mapping is an over ride
                mapping.addField(new AttributeOverride(fieldName));
            } else {
                // this is a normal generated field, it will be in the main class mapping.
                mapping.addField(new Id(fieldName));
            }
            primaryKeyFields.add(fieldName);
        } else if ("java.lang.Object".equals(bean.getPrimKeyClass())) {
            // the automatically generated keys use a special property name
            // and will always be in the generated superclass.
            final String fieldName = "OpenEJB_pk";
            final Id field = new Id(fieldName);
            field.setGeneratedValue(new GeneratedValue(GenerationType.AUTO));
            mapping.addField(field);
            primaryKeyFields.add(fieldName);
        } else if (bean.getPrimKeyClass() != null) {
            Class<?> pkClass = null;
            try {
                pkClass = classLoader.loadClass(bean.getPrimKeyClass());
                MappedSuperclass idclass = null;
                // now validate the primary class fields against the bean cmp fields
                // to make sure everything maps correctly.
                for (final Field pkField : pkClass.getFields()) {
                    final String pkFieldName = pkField.getName();
                    final int modifiers = pkField.getModifiers();
                    if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && allFields.contains(pkFieldName)) {
                        // see if the bean field is concretely defined in one of the superclasses
                        final MappedSuperclass superclass = superclassByField.get(pkFieldName);
                        if (superclass != null) {
                            // ok, we have an override that needs to be specified at the main class level.
                            superclass.addField(new Id(pkFieldName));
                            mapping.addField(new AttributeOverride(pkFieldName));
                            idclass = resolveIdClass(idclass, superclass, beanClass);
                        } else {
                            // this field will be autogenerated
                            mapping.addField(new Id(pkFieldName));
                        }
                        primaryKeyFields.add(pkFieldName);
                    }
                }
                // if we've located an ID class, set it as such
                if (idclass != null) {
                    idclass.setIdClass(new IdClass(bean.getPrimKeyClass()));
                } else {
                    // do this for the toplevel mapping
                    mapping.setIdClass(new IdClass(bean.getPrimKeyClass()));
                }
            } catch (final ClassNotFoundException e) {
                throw (IllegalStateException) new IllegalStateException("Could not find entity primary key class " + bean.getPrimKeyClass()).initCause(e);
            }
        }

        //
        // basic: cmp-fields
        // This again, adds all of the additional cmp-fields to the mapping
        //
        for (final CmpField cmpField : bean.getCmpField()) {
            // only add entries for cmp fields that are not part of the primary key
            if (!primaryKeyFields.contains(cmpField.getFieldName())) {
                final String fieldName = cmpField.getFieldName();
                // this will be here if we've already processed this
                final MappedSuperclass superclass = superclassByField.get(fieldName);
                // if this field is defined by one of the superclasses, then
                // we need to provide a mapping for this.
                if (superclass != null) {
                    // we need to mark this as being in one of the superclasses
                    superclass.addField(new Basic(fieldName));
                    mapping.addField(new AttributeOverride(fieldName));
                } else {
                    // directly generated.
                    mapping.addField(new Basic(fieldName));
                }
View Full Code Here

        // id: the primary key
        //
        final Set<String> primaryKeyFields = new HashSet<String>();
        if (bean.getPrimkeyField() != null) {
            final String fieldName = bean.getPrimkeyField();
            final MappedSuperclass superclass = superclassByField.get(fieldName);
            if (superclass == null) {
                throw new IllegalStateException("Primary key field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
            }
            superclass.addField(new Id(fieldName));
            mapping.addField(new AttributeOverride(fieldName));
            primaryKeyFields.add(fieldName);
        } else if ("java.lang.Object".equals(bean.getPrimKeyClass())) {
            // a primary field type of Object is an automatically generated
            // pk field.  Mark it as such and add it to the mapping. 
            final String fieldName = "OpenEJB_pk";
            final Id field = new Id(fieldName);
            field.setGeneratedValue(new GeneratedValue(GenerationType.AUTO));
            mapping.addField(field);
        } else if (bean.getPrimKeyClass() != null) {
            // we have a primary key class.  We need to define the mappings between the key class fields
            // and the bean's managed fields.

            Class<?> pkClass = null;
            try {
                pkClass = classLoader.loadClass(bean.getPrimKeyClass());
                MappedSuperclass superclass = null;
                MappedSuperclass idclass = null;
                for (final Field pkField : pkClass.getFields()) {
                    final String fieldName = pkField.getName();
                    final int modifiers = pkField.getModifiers();
                    // the primary key fields must be public, non-static, must be defined as a CMP field,
                    // AND must also exist in the class hierarchy (not enforced by mapFields());
                    if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers) && allFields.contains(fieldName)) {
                        superclass = superclassByField.get(fieldName);
                        if (superclass == null) {
                            throw new IllegalStateException("Primary key field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
                        }
                        // these are defined ast ID fields because they are part of the primary key
                        superclass.addField(new Id(fieldName));
                        mapping.addField(new AttributeOverride(fieldName));
                        primaryKeyFields.add(fieldName);
                        idclass = resolveIdClass(idclass, superclass, ejbClass);
                    }
                }

                // if we've located an ID class, set it as such
                if (idclass != null) {
                    idclass.setIdClass(new IdClass(bean.getPrimKeyClass()));
                }
            } catch (final ClassNotFoundException e) {
                throw (IllegalStateException) new IllegalStateException("Could not find entity primary key class " + bean.getPrimKeyClass()).initCause(e);
            }
        }

        //
        // basic: cmp-fields
        //
        for (final CmpField cmpField : bean.getCmpField()) {
            final String fieldName = cmpField.getFieldName();
            // all of the primary key fields have been processed, so only handle whatever is left over
            if (!primaryKeyFields.contains(fieldName)) {
                final MappedSuperclass superclass = superclassByField.get(fieldName);
                if (superclass == null) {
                    throw new IllegalStateException("CMP field " + fieldName + " is not defined in class " + ejbClassName + " or any super classes");
                }
                superclass.addField(new Basic(fieldName));
                mapping.addField(new AttributeOverride(fieldName));
            }
        }

        // all of the fields should now be identified by type, so return a set of
View Full Code Here

        // spin down the class hierarchy until we've either processed all of the fields
        // or we've reached the Object class.
        while (!persistantFields.isEmpty() && !clazz.equals(Object.class)) {
            // This is a single target for the relationship mapping for each
            // class in the hierarchy.
            final MappedSuperclass superclass = new MappedSuperclass(clazz.getName());
            for (final Field field : clazz.getDeclaredFields()) {
                if (!field.isSynthetic()) {
                    final String fieldName = field.getName();
                    // if this is one of bean's persistence fields, create the mapping
                    if (persistantFields.contains(fieldName)) {
                        fields.put(fieldName, superclass);
                        persistantFields.remove(fieldName);
                    } else if (!ENHANCED_FIELDS.contains(fieldName)) {
                        // these are fields we need to identify as transient for the persistence engine.
                        final Transient transientField = new Transient(fieldName);
                        superclass.addField(transientField);
                    }
                }
            }
            clazz = clazz.getSuperclass();
        }
View Full Code Here

TOP

Related Classes of org.apache.openejb.jee.jpa.MappedSuperclass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.