Search in sources :

Example 1 with CmpField$JAXB.readCmpField

use of org.apache.openejb.jee.CmpField$JAXB.readCmpField in project tomee by apache.

the class CmpJpaConversion method mapClass2x.

/**
 * Generate the JPA mapping for a CMP 2.x bean.  Since
 * the field accessors are all defined as abstract methods
 * and the fields will not be defined in the implementation
 * class, we don't need to deal with mapped superclasses.
 * All of the fields and concrete methods will be
 * implemented by the generated subclass, so from
 * a JPA standpoint, there are no mapped superclasses
 * required.
 *
 * @param mapping     The mapping information we're updating.
 * @param bean        The entity bean meta data
 * @param classLoader The classloader for resolving class references and
 *                    primary key classes.
 */
private Collection<MappedSuperclass> mapClass2x(final Mapping mapping, final EntityBean bean, final ClassLoader classLoader) {
    final Set<String> allFields = new TreeSet<>();
    // get an acculated set of the CMP fields.
    for (final CmpField cmpField : bean.getCmpField()) {
        allFields.add(cmpField.getFieldName());
    }
    Class<?> beanClass = null;
    try {
        beanClass = classLoader.loadClass(bean.getEjbClass());
    } catch (final ClassNotFoundException e) {
        // if it does fail, just return null from here
        return null;
    }
    // build a map from the field name to the super class that contains that field.
    // If this is a strictly CMP 2.x class, this is generally an empty map.  However,
    // we support some migration steps toward EJB3, so this can be defined completely
    // or partially as a POJO with concrete fields and accessors.  This allows us to
    // locate and generate the mappings
    final Map<String, MappedSuperclass> superclassByField = mapFields(beanClass, allFields);
    for (final Method method : beanClass.getMethods()) {
        if (!Modifier.isAbstract(method.getModifiers())) {
            continue;
        }
        if (method.getParameterTypes().length != 0) {
            continue;
        }
        if (method.getReturnType().equals(Void.TYPE)) {
            continue;
        }
        // Skip relationships: anything of type EJBLocalObject or Collection
        if (EJBLocalObject.class.isAssignableFrom(method.getReturnType())) {
            continue;
        }
        if (Collection.class.isAssignableFrom(method.getReturnType())) {
            continue;
        }
        if (Map.class.isAssignableFrom(method.getReturnType())) {
            continue;
        }
        String name = method.getName();
        if (name.startsWith("get")) {
            name = name.substring("get".length(), name.length());
        } else if (name.startsWith("is")) {
            // boolean.
            if (method.getReturnType() == Boolean.TYPE) {
                name = name.substring("is".length(), name.length());
            } else {
                // not an acceptable "is" method.
                continue;
            }
        } else {
            continue;
        }
        // the property needs the first character lowercased.  Generally,
        // we'll have this field already in our list, but it might have been
        // omitted from the meta data.
        name = Strings.lcfirst(name);
        if (!allFields.contains(name)) {
            allFields.add(name);
            bean.addCmpField(name);
        }
    }
    // 
    // id: the primary key
    // 
    final Set<String> primaryKeyFields = new HashSet<>();
    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;
            // 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);
        }
    }
    // 
    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);
            // 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));
            }
        }
    }
    // the field mappings
    return new HashSet<>(superclassByField.values());
}
Also used : Basic(org.apache.openejb.jee.jpa.Basic) Method(java.lang.reflect.Method) QueryMethod(org.apache.openejb.jee.QueryMethod) AttributeOverride(org.apache.openejb.jee.jpa.AttributeOverride) GeneratedValue(org.apache.openejb.jee.jpa.GeneratedValue) RelationField(org.apache.openejb.jee.jpa.RelationField) CmpField(org.apache.openejb.jee.CmpField) Field(java.lang.reflect.Field) IdClass(org.apache.openejb.jee.jpa.IdClass) CmpField(org.apache.openejb.jee.CmpField) TreeSet(java.util.TreeSet) MappedSuperclass(org.apache.openejb.jee.jpa.MappedSuperclass) Id(org.apache.openejb.jee.jpa.Id) HashSet(java.util.HashSet)

Example 2 with CmpField$JAXB.readCmpField

use of org.apache.openejb.jee.CmpField$JAXB.readCmpField in project tomee by apache.

the class LegacyInterfaceTest method test.

public void test() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class));
    ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN));
    // <entity>
    // <ejb-name>License</ejb-name>
    // <local-home>org.apache.openejb.test.entity.cmr.onetoone.LicenseLocalHome</local-home>
    // <local>org.apache.openejb.test.entity.cmr.onetoone.LicenseLocal</local>
    // <ejb-class>org.apache.openejb.test.entity.cmr.onetoone.LicenseBean</ejb-class>
    // <persistence-type>Container</persistence-type>
    // <prim-key-class>java.lang.Integer</prim-key-class>
    // <reentrant>false</reentrant>
    // <cmp-version>2.x</cmp-version>
    // <abstract-schema-name>License</abstract-schema-name>
    // <cmp-field>
    // <field-name>id</field-name>
    // </cmp-field>
    // <cmp-field>
    // <field-name>number</field-name>
    // </cmp-field>
    // <cmp-field>
    // <field-name>points</field-name>
    // </cmp-field>
    // <cmp-field>
    // <field-name>notes</field-name>
    // </cmp-field>
    // <primkey-field>id</primkey-field>
    // <query>
    // <!-- CompondPK one-to-one shares the local home interface so we need to declare this useless finder -->
    // <query-method>
    // <method-name>findByPrimaryKey</method-name>
    // <method-params>
    // <method-param>org.apache.openejb.test.entity.cmr.onetoone.LicensePk</method-param>
    // </method-params>
    // </query-method>
    // <ejb-ql>SELECT OBJECT(DL) FROM License DL</ejb-ql>
    // </query>
    // </entity>
    final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER));
    cmp.setPrimKeyClass(Integer.class.getName());
    cmp.setPrimkeyField("id");
    cmp.getCmpField().add(new CmpField("id"));
    cmp.getCmpField().add(new CmpField("name"));
    final Query query = new Query();
    query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName()));
    query.setEjbQl("SELECT OBJECT(DL) FROM License DL");
    cmp.getQuery().add(query);
    final List<ContainerTransaction> transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction();
    // <container-transaction>
    // <method>
    // <ejb-name>MyBean</ejb-name>
    // <method-name>*</method-name>
    // </method>
    // <trans-attribute>Supports</trans-attribute>
    // </container-transaction>
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*"));
    final File f = new File("test").getAbsoluteFile();
    if (!f.exists() && !f.mkdirs()) {
        throw new Exception("Failed to create test directory: " + f);
    }
    final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath());
    module.getEjbModules().add(new EjbModule(ejbJar));
    assembler.createApplication(config.configureApplication(module));
}
Also used : AppModule(org.apache.openejb.config.AppModule) NamedQuery(org.apache.openejb.jee.jpa.NamedQuery) Query(org.apache.openejb.jee.Query) QueryMethod(org.apache.openejb.jee.QueryMethod) EjbModule(org.apache.openejb.config.EjbModule) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) RemoveException(javax.ejb.RemoveException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) CreateException(javax.ejb.CreateException) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) CmpField(org.apache.openejb.jee.CmpField) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) EntityBean(org.apache.openejb.jee.EntityBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) File(java.io.File) EjbJar(org.apache.openejb.jee.EjbJar)

Example 3 with CmpField$JAXB.readCmpField

use of org.apache.openejb.jee.CmpField$JAXB.readCmpField in project tomee by apache.

the class LegacyInterfaceTest method testCustomCmpMappings.

public void testCustomCmpMappings() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class));
    ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN));
    final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER));
    cmp.setPrimKeyClass(Integer.class.getName());
    cmp.setPrimkeyField("id");
    cmp.getCmpField().add(new CmpField("id"));
    cmp.getCmpField().add(new CmpField("name"));
    final Query query = new Query();
    query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName()));
    query.setEjbQl("SELECT OBJECT(DL) FROM License DL");
    cmp.getQuery().add(query);
    final List<ContainerTransaction> transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction();
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*"));
    final File f = new File("test").getAbsoluteFile();
    if (!f.exists() && !f.mkdirs()) {
        throw new Exception("Failed to create test directory: " + f);
    }
    final EntityMappings entityMappings = new EntityMappings();
    final Entity entity = new Entity();
    entity.setClazz("openejb.org.apache.openejb.core.MyCmpBean");
    entity.setName("MyCmpBean");
    entity.setDescription("MyCmpBean");
    entity.setAttributes(new Attributes());
    final NamedQuery namedQuery = new NamedQuery();
    namedQuery.setQuery("SELECT OBJECT(DL) FROM License DL");
    entity.getNamedQuery().add(namedQuery);
    final Id id = new Id();
    id.setName("id");
    entity.getAttributes().getId().add(id);
    final Basic basic = new Basic();
    basic.setName("name");
    final Column column = new Column();
    column.setName("wNAME");
    column.setLength(300);
    basic.setColumn(column);
    entity.getAttributes().getBasic().add(basic);
    entityMappings.getEntity().add(entity);
    final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath());
    final EjbModule ejbModule = new EjbModule(ejbJar);
    ejbModule.getAltDDs().put("openejb-cmp-orm.xml", entityMappings);
    module.getEjbModules().add(ejbModule);
    assertNull(module.getCmpMappings());
    assembler.createApplication(config.configureApplication(module));
    assertNotNull(module.getCmpMappings());
    final List<Basic> basicList = module.getCmpMappings().getEntityMap().get("openejb.org.apache.openejb.core.MyCmpBean").getAttributes().getBasic();
    assertEquals(1, basicList.size());
    assertEquals(300, basicList.get(0).getColumn().getLength().intValue());
    assertEquals("wNAME", basicList.get(0).getColumn().getName());
}
Also used : Entity(org.apache.openejb.jee.jpa.Entity) Basic(org.apache.openejb.jee.jpa.Basic) AppModule(org.apache.openejb.config.AppModule) NamedQuery(org.apache.openejb.jee.jpa.NamedQuery) Query(org.apache.openejb.jee.Query) QueryMethod(org.apache.openejb.jee.QueryMethod) Attributes(org.apache.openejb.jee.jpa.Attributes) EjbModule(org.apache.openejb.config.EjbModule) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) CmpField(org.apache.openejb.jee.CmpField) Column(org.apache.openejb.jee.jpa.Column) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar) RemoveException(javax.ejb.RemoveException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) CreateException(javax.ejb.CreateException) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) EntityBean(org.apache.openejb.jee.EntityBean) EntityMappings(org.apache.openejb.jee.jpa.EntityMappings) Assembler(org.apache.openejb.assembler.classic.Assembler) NamedQuery(org.apache.openejb.jee.jpa.NamedQuery) Id(org.apache.openejb.jee.jpa.Id) File(java.io.File)

Example 4 with CmpField$JAXB.readCmpField

use of org.apache.openejb.jee.CmpField$JAXB.readCmpField in project tomee by apache.

the class LegacyInterfaceTest method testCustomCmpMappingsWithMappingFileDefinedInPersistenceXml.

public void testCustomCmpMappingsWithMappingFileDefinedInPersistenceXml() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new SingletonBean(MySingletonBean.class));
    ejbJar.addEnterpriseBean(new EntityBean(MyBmpBean.class, PersistenceType.BEAN));
    final EntityBean cmp = ejbJar.addEnterpriseBean(new EntityBean(MyCmpBean.class, PersistenceType.CONTAINER));
    cmp.setPrimKeyClass(Integer.class.getName());
    cmp.setPrimkeyField("id");
    cmp.getCmpField().add(new CmpField("id"));
    cmp.getCmpField().add(new CmpField("name"));
    final Query query = new Query();
    query.setQueryMethod(new QueryMethod("findByPrimaryKey", Integer.class.getName()));
    query.setEjbQl("SELECT OBJECT(DL) FROM License DL");
    cmp.getQuery().add(query);
    final List<ContainerTransaction> transactions = ejbJar.getAssemblyDescriptor().getContainerTransaction();
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyBmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MyCmpBean", "*"));
    transactions.add(new ContainerTransaction(TransAttribute.SUPPORTS, null, "MySingletonBean", "*"));
    final File f = new File("test").getAbsoluteFile();
    if (!f.exists() && !f.mkdirs()) {
        throw new Exception("Failed to create test directory: " + f);
    }
    final AppModule module = new AppModule(this.getClass().getClassLoader(), f.getAbsolutePath());
    final EjbModule ejbModule = new EjbModule(ejbJar);
    Persistence persistence = new Persistence();
    PersistenceUnit pu = persistence.addPersistenceUnit("cmp");
    pu.setTransactionType(TransactionType.JTA);
    pu.setJtaDataSource("fake");
    pu.setNonJtaDataSource("fake");
    pu.getMappingFile().add("test-orm.xml");
    pu.getClazz().add("openejb.org.apache.openejb.core.MyCmpBean");
    module.addPersistenceModule(new PersistenceModule("pu", persistence));
    module.getEjbModules().add(ejbModule);
    assertNull(module.getCmpMappings());
    assembler.createApplication(config.configureApplication(module));
    assertNotNull(module.getCmpMappings());
    // no mapping should be automatically generated
    assertTrue(module.getCmpMappings().getEntityMap().isEmpty());
    // pu should not be modified, no duplicate classes
    assertEquals(1, pu.getClazz().size());
    assertEquals("openejb.org.apache.openejb.core.MyCmpBean", pu.getClazz().get(0));
    assertEquals(1, pu.getMappingFile().size());
    assertEquals("test-orm.xml", pu.getMappingFile().get(0));
}
Also used : AppModule(org.apache.openejb.config.AppModule) NamedQuery(org.apache.openejb.jee.jpa.NamedQuery) Query(org.apache.openejb.jee.Query) QueryMethod(org.apache.openejb.jee.QueryMethod) EjbModule(org.apache.openejb.config.EjbModule) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) PersistenceModule(org.apache.openejb.config.PersistenceModule) RemoveException(javax.ejb.RemoveException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) CreateException(javax.ejb.CreateException) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) SingletonBean(org.apache.openejb.jee.SingletonBean) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) CmpField(org.apache.openejb.jee.CmpField) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) EntityBean(org.apache.openejb.jee.EntityBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) File(java.io.File) EjbJar(org.apache.openejb.jee.EjbJar)

Example 5 with CmpField$JAXB.readCmpField

use of org.apache.openejb.jee.CmpField$JAXB.readCmpField in project tomee by apache.

the class CmpJpaConversion method mapClass1x.

/**
 * Create the class mapping for a CMP 1.x entity bean.
 * Since the fields for 1.x persistence are defined
 * in the objects directly, we need to create superclass
 * mappings for each of the defined fields to identify
 * which classes implement each of the managed fields.
 *
 * @param ejbClassName The name of the class we're processing.
 * @param mapping      The mappings we're going to generate.
 * @param bean         The bean metadata for the ejb.
 * @param classLoader  The classloader used to load the bean class for
 *                     inspection.
 * @return The set of mapped superclasses used in this
 * bean mapping.
 */
private Collection<MappedSuperclass> mapClass1x(final String ejbClassName, final Mapping mapping, final EntityBean bean, final ClassLoader classLoader) {
    final Class ejbClass = loadClass(classLoader, ejbClassName);
    // build a set of all field names
    final Set<String> allFields = new TreeSet<>();
    for (final CmpField cmpField : bean.getCmpField()) {
        allFields.add(cmpField.getFieldName());
    }
    // build a map from the field name to the super class that contains that field
    final Map<String, MappedSuperclass> superclassByField = mapFields(ejbClass, allFields);
    // 
    // id: the primary key
    // 
    final Set<String> primaryKeyFields = new HashSet<>();
    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();
                // 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);
        }
    }
    // 
    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));
        }
    }
    // the field mappings
    return new HashSet<>(superclassByField.values());
}
Also used : Basic(org.apache.openejb.jee.jpa.Basic) AttributeOverride(org.apache.openejb.jee.jpa.AttributeOverride) GeneratedValue(org.apache.openejb.jee.jpa.GeneratedValue) RelationField(org.apache.openejb.jee.jpa.RelationField) CmpField(org.apache.openejb.jee.CmpField) Field(java.lang.reflect.Field) IdClass(org.apache.openejb.jee.jpa.IdClass) CmpField(org.apache.openejb.jee.CmpField) TreeSet(java.util.TreeSet) MappedSuperclass(org.apache.openejb.jee.jpa.MappedSuperclass) IdClass(org.apache.openejb.jee.jpa.IdClass) Id(org.apache.openejb.jee.jpa.Id) HashSet(java.util.HashSet)

Aggregations

CmpField (org.apache.openejb.jee.CmpField)8 QueryMethod (org.apache.openejb.jee.QueryMethod)6 EntityBean (org.apache.openejb.jee.EntityBean)5 Query (org.apache.openejb.jee.Query)5 File (java.io.File)3 RemoteException (java.rmi.RemoteException)3 CreateException (javax.ejb.CreateException)3 EJBException (javax.ejb.EJBException)3 RemoveException (javax.ejb.RemoveException)3 Assembler (org.apache.openejb.assembler.classic.Assembler)3 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)3 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)3 AppModule (org.apache.openejb.config.AppModule)3 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)3 EjbModule (org.apache.openejb.config.EjbModule)3 InitContextFactory (org.apache.openejb.core.ivm.naming.InitContextFactory)3 ContainerTransaction (org.apache.openejb.jee.ContainerTransaction)3 EjbJar (org.apache.openejb.jee.EjbJar)3 SingletonBean (org.apache.openejb.jee.SingletonBean)3 NamedQuery (org.apache.openejb.jee.jpa.NamedQuery)3