Package org.apache.openejb.jee.jpa

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


                // Bean already has a persistence-context-ref for cmp
                // which means it has a mapping, so skip this bean
                continue;
            }

            Entity entity = new Entity();

            // description: contains the name of the entity bean
            entity.setDescription(ejbModule.getModuleId() + "#" + bean.getEjbName());

            // name: the name of the entity in queries
            String entityName = bean.getAbstractSchemaName();
            entity.setName(entityName);

            // class: impl class name
            String cmpImplClassName = CmpUtil.getCmpImplClassName(bean.getAbstractSchemaName(), bean.getEjbClass());
            entity.setClazz(cmpImplClassName);

            // add the entity
            entityMappings.getEntity().add(entity);
            entitiesByName.put(bean.getEjbName(), entity);

            if (bean.getCmpVersion() == CmpVersion.CMP2) {
                mapClass2x(entity, bean, classLoader);
            } else {
                // map the cmp class, but if we are using a mapped super class, generate attribute-override instead of id and basic
                Collection<MappedSuperclass> mappedSuperclasses = mapClass1x(bean.getEjbClass(), entity, bean, classLoader);
                for (MappedSuperclass mappedSuperclass : mappedSuperclasses) {
                    mappedSuperclassByClass.put(mappedSuperclass.getClazz(), mappedSuperclass);
                }
            }

            // process queries
            for (Query query : bean.getQuery()) {
                NamedQuery namedQuery = new NamedQuery();
                QueryMethod queryMethod = query.getQueryMethod();

                // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
                StringBuilder name = new StringBuilder();
                name.append(entityName).append(".").append(queryMethod.getMethodName());
                if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                    name.append('(');
                    boolean first = true;
                    for (String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                        if (!first) name.append(",");
                        name.append(methodParam);
                        first = false;
                    }
                    name.append(')');
                }
                namedQuery.setName(name.toString());

                namedQuery.setQuery(query.getEjbQl());
                entity.getNamedQuery().add(namedQuery);
            }
            // todo: there should be a common interface between ejb query object and openejb query object
            EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
            if (ejbDeployment != null) {
                for (org.apache.openejb.jee.oejb3.Query query : ejbDeployment.getQuery()) {
                    NamedQuery namedQuery = new NamedQuery();
                    org.apache.openejb.jee.oejb3.QueryMethod queryMethod = query.getQueryMethod();

                    // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
                    StringBuilder name = new StringBuilder();
                    name.append(entityName).append(".").append(queryMethod.getMethodName());
                    if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                        name.append('(');
                        boolean first = true;
                        for (String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                            if (!first) name.append(",");
                            name.append(methodParam);
                            first = false;
                        }
                        name.append(')');
                    }
                    namedQuery.setName(name.toString());

                    namedQuery.setQuery(query.getObjectQl());
                    entity.getNamedQuery().add(namedQuery);
                }
            }
        }
        entityMappings.getMappedSuperclass().addAll(mappedSuperclassByClass.values());

        Relationships relationships = ejbJar.getRelationships();
        if (relationships != null) {
            for (EjbRelation relation : relationships.getEjbRelation()) {
                List<EjbRelationshipRole> roles = relation.getEjbRelationshipRole();
                // if we don't have two roles, the relation is bad so we skip it
                if (roles.size() != 2) {
                    continue;
                }

                // get left entity
                EjbRelationshipRole leftRole = roles.get(0);
                RelationshipRoleSource leftRoleSource = leftRole.getRelationshipRoleSource();
                String leftEjbName = leftRoleSource == null ? null : leftRoleSource.getEjbName();
                Entity leftEntity = entitiesByName.get(leftEjbName);

                // get right entity
                EjbRelationshipRole rightRole = roles.get(1);
                RelationshipRoleSource rightRoleSource = rightRole.getRelationshipRoleSource();
                String rightEjbName = rightRoleSource == null ? null : rightRoleSource.getEjbName();
                Entity rightEntity = entitiesByName.get(rightEjbName);

                // neither left or right have a mapping which is fine
                if (leftEntity == null && rightEntity == null) {
                    continue;
                }
                // left not found?
                if (leftEntity == null) {
                    throw new OpenEJBException("Role source " + leftEjbName + " defined in relationship role " +
                            relation.getEjbRelationName() + "::" + leftRole.getEjbRelationshipRoleName() + " not found");
                }
                // right not found?
                if (rightEntity == null) {
                    throw new OpenEJBException("Role source " + rightEjbName + " defined in relationship role " +
                            relation.getEjbRelationName() + "::" + rightRole.getEjbRelationshipRoleName() + " not found");
                }

                String leftFieldName = null;
                boolean leftSynthetic = false;
                if (leftRole.getCmrField() != null) {
                    leftFieldName = leftRole.getCmrField().getCmrFieldName();
                } else {
                    leftFieldName = rightEntity.getName() + "_" + rightRole.getCmrField().getCmrFieldName();
                    leftSynthetic = true;
                }
                boolean leftIsOne = leftRole.getMultiplicity() == Multiplicity.ONE;

                String rightFieldName = null;
                boolean rightSynthetic = false;
                if (rightRole.getCmrField() != null) {
                    rightFieldName = rightRole.getCmrField().getCmrFieldName();
                } else {
                    rightFieldName = leftEntity.getName() + "_" + leftRole.getCmrField().getCmrFieldName();
                    rightSynthetic = true;
                }
                boolean rightIsOne = rightRole.getMultiplicity() == Multiplicity.ONE;

                if (leftIsOne && rightIsOne) {
                    //
                    // one-to-one
                    //

                    // left
                    OneToOne leftOneToOne = null;
                    leftOneToOne = new OneToOne();
                    leftOneToOne.setName(leftFieldName);
                    leftOneToOne.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftOneToOne);
                    leftEntity.getAttributes().getOneToOne().add(leftOneToOne);

                    // right
                    OneToOne rightOneToOne = null;
                    rightOneToOne = new OneToOne();
                    rightOneToOne.setName(rightFieldName);
                    rightOneToOne.setSyntheticField(rightSynthetic);
                    rightOneToOne.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightOneToOne);
                    rightEntity.getAttributes().getOneToOne().add(rightOneToOne);

                    // link
                    leftOneToOne.setRelatedField(rightOneToOne);
                    rightOneToOne.setRelatedField(leftOneToOne);
                } else if (leftIsOne && !rightIsOne) {
                    //
                    // one-to-many
                    //

                    // left
                    OneToMany leftOneToMany = null;
                    leftOneToMany = new OneToMany();
                    leftOneToMany.setName(leftFieldName);
                    leftOneToMany.setSyntheticField(leftSynthetic);
                    leftOneToMany.setMappedBy(rightFieldName);
                    setCascade(rightRole, leftOneToMany);
                    leftEntity.getAttributes().getOneToMany().add(leftOneToMany);

                    // right
                    ManyToOne rightManyToOne = null;
                    rightManyToOne = new ManyToOne();
                    rightManyToOne.setName(rightFieldName);
                    rightManyToOne.setSyntheticField(rightSynthetic);
                    setCascade(leftRole, rightManyToOne);
                    rightEntity.getAttributes().getManyToOne().add(rightManyToOne);

                    // link
                    leftOneToMany.setRelatedField(rightManyToOne);
                    rightManyToOne.setRelatedField(leftOneToMany);
                } else if (!leftIsOne && rightIsOne) {
                    //
                    // many-to-one
                    //

                    // left
                    ManyToOne leftManyToOne = null;
                    leftManyToOne = new ManyToOne();
                    leftManyToOne.setName(leftFieldName);
                    leftManyToOne.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftManyToOne);
                    leftEntity.getAttributes().getManyToOne().add(leftManyToOne);

                    // right
                    OneToMany rightOneToMany = null;
                    rightOneToMany = new OneToMany();
                    rightOneToMany.setName(rightFieldName);
                    rightOneToMany.setSyntheticField(rightSynthetic);
                    rightOneToMany.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightOneToMany);
                    rightEntity.getAttributes().getOneToMany().add(rightOneToMany);

                    // link
                    leftManyToOne.setRelatedField(rightOneToMany);
                    rightOneToMany.setRelatedField(leftManyToOne);
                } else if (!leftIsOne && !rightIsOne) {
                    //
                    // many-to-many
                    //

                    // left
                    ManyToMany leftManyToMany = null;
                    leftManyToMany = new ManyToMany();
                    leftManyToMany.setName(leftFieldName);
                    leftManyToMany.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftManyToMany);
                    leftEntity.getAttributes().getManyToMany().add(leftManyToMany);

                    // right
                    ManyToMany rightManyToMany = null;
                    rightManyToMany = new ManyToMany();
                    rightManyToMany.setName(rightFieldName);
                    rightManyToMany.setSyntheticField(rightSynthetic);
                    rightManyToMany.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightManyToMany);
                    rightEntity.getAttributes().getManyToMany().add(rightManyToMany);

                    // link
                    leftManyToMany.setRelatedField(rightManyToMany);
                    rightManyToMany.setRelatedField(leftManyToMany);
                }
View Full Code Here


        if (!(enterpriseBean instanceof EntityBean)) {
          continue;
        }

        EntityBean entityBean = (EntityBean) enterpriseBean;
        Entity entity = getMapping(cmpMappings, entityBean.getEjbName());

                convertBeanToPojo(entityBean, entity);
      }

            if (relationships != null) {
View Full Code Here

  public void testShouldNotThrowExceptionIfTableNameIsMissing() throws Exception {
    Mockery context = new Mockery();
    IJDTFacade jdtFacade = context.mock(IJDTFacade.class);

        AppModule appModule = new TestFixture().getAppModule("notablename-ejb-jar.xml", "notablename-openejb-jar.xml");
    Entity entity = appModule.getCmpMappings().getEntityMap().get("openejb.java.lang.Product");
    EntityBean entityBean = (EntityBean) appModule.getEjbModules().get(0).getEjbJar().getEnterpriseBean("ProductEJB");
   
    new EntityBeanConverter(jdtFacade).addTableAnnotation(entityBean, entity);
  }
View Full Code Here

  public void testShouldAddTableAnnotation() throws Exception {
    Mockery context = new Mockery();
    final IJDTFacade jdtFacade = context.mock(IJDTFacade.class);

        AppModule appModule = new TestFixture().getAppModule("basicentity-ejb-jar.xml", "basicentity-openejb-jar.xml");
    Entity entity = appModule.getCmpMappings().getEntityMap().get("openejb.java.lang.Product");
    EntityBean entityBean = (EntityBean) appModule.getEjbModules().get(0).getEjbJar().getEnterpriseBean("ProductEJB");
   
    context.checking(new Expectations() {
      {
        Map<String,Object> properties = new HashMap<String, Object>();
View Full Code Here

  public void testShouldAddColumnAnnotation() throws Exception {
    Mockery context = new Mockery();
    final IJDTFacade jdtFacade = context.mock(IJDTFacade.class);

        AppModule appModule = new TestFixture().getAppModule("basicentity-ejb-jar.xml", "basicentity-openejb-jar.xml");
    Entity entity = appModule.getCmpMappings().getEntityMap().get("openejb.java.lang.Product");
    EntityBean entityBean = (EntityBean) appModule.getEjbModules().get(0).getEjbJar().getEnterpriseBean("ProductEJB");
   
    context.checking(new Expectations() {
      {
        Map<String, Object> nameColumnProperties = new HashMap<String, Object>();
        nameColumnProperties.put("name", "name");
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getName", new String[0], Column.class, nameColumnProperties );

        Map<String, Object> nameBasicProperties = new HashMap<String, Object>();
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getName", new String[0], Basic.class, nameBasicProperties );
       
        Map<String, Object> codeColumnProperties = new HashMap<String, Object>();
        codeColumnProperties.put("name", "code");
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getCode", new String[0], Column.class, codeColumnProperties );

        Map<String, Object> codeBasicProperties = new HashMap<String, Object>();
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getCode", new String[0], Basic.class, codeBasicProperties );

        Map<String, Object> descriptionColumnProperties = new HashMap<String, Object>();
        descriptionColumnProperties.put("name", "description");
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getDescription", new String[0], Column.class, descriptionColumnProperties );

        Map<String, Object> descriptionBasicProperties = new HashMap<String, Object>();
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getDescription", new String[0], Basic.class, descriptionBasicProperties );

      }
    });
   
    new EntityBeanConverter(jdtFacade).addBasicAnnotations(entityBean, entity.getAttributes().getBasic());
  }
View Full Code Here

        String ejbJarFilename = "basicentity-ejb-jar.xml";
        String openejbJarFilename = "basicentity-openejb-jar.xml";

        AppModule appModule = new TestFixture().getAppModule(ejbJarFilename, openejbJarFilename);
    Entity entity = appModule.getCmpMappings().getEntityMap().get("openejb.java.lang.Product");
    EntityBean entityBean = (EntityBean) appModule.getEjbModules().get(0).getEjbJar().getEnterpriseBean("ProductEJB");
   
    context.checking(new Expectations() {
      {
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getId", new String[0], Id.class, null);
        Map<String, Object> generatedvalueProps = new HashMap<String, Object>();
        generatedvalueProps.put("strategy", GenerationType.IDENTITY);
        one(jdtFacade).addMethodAnnotation("org.superbiz.ProductBean", "getId", new String[0], GeneratedValue.class, generatedvalueProps );
      }
    });
   
    new EntityBeanConverter(jdtFacade).addIdAnnotation(entityBean, entity.getAttributes().getId().get(0));
  }
View Full Code Here

                // Bean already has a persistence-context-ref for cmp
                // which means it has a mapping, so skip this bean
                continue;
            }

            Entity entity = new Entity();

            // description: contains the name of the entity bean
            entity.setDescription(ejbModule.getModuleId() + "#" + bean.getEjbName());

            // name: the name of the entity in queries
            String entityName = bean.getAbstractSchemaName();
            entity.setName(entityName);

            // class: impl class name
            String cmpImplClassName = CmpUtil.getCmpImplClassName(bean.getAbstractSchemaName(), bean.getEjbClass());
            entity.setClazz(cmpImplClassName);

            // add the entity
            entityMappings.getEntity().add(entity);
            entitiesByName.put(bean.getEjbName(), entity);

            if (bean.getCmpVersion() == CmpVersion.CMP2) {
                mapClass2x(entity, bean, classLoader);
            } else {
                // map the cmp class, but if we are using a mapped super class, generate attribute-override instead of id and basic
                Collection<MappedSuperclass> mappedSuperclasses = mapClass1x(bean.getEjbClass(), entity, bean, classLoader);
                for (MappedSuperclass mappedSuperclass : mappedSuperclasses) {
                    mappedSuperclassByClass.put(mappedSuperclass.getClazz(), mappedSuperclass);
                }
            }

            // process queries
            for (Query query : bean.getQuery()) {
                NamedQuery namedQuery = new NamedQuery();
                QueryMethod queryMethod = query.getQueryMethod();

                // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
                StringBuilder name = new StringBuilder();
                name.append(entityName).append(".").append(queryMethod.getMethodName());
                if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                    name.append('(');
                    boolean first = true;
                    for (String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                        if (!first) name.append(",");
                        name.append(methodParam);
                        first = false;
                    }
                    name.append(')');
                }
                namedQuery.setName(name.toString());

                namedQuery.setQuery(query.getEjbQl());
                entity.getNamedQuery().add(namedQuery);
            }
            // todo: there should be a common interface between ejb query object and openejb query object
            EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
            if (ejbDeployment != null) {
                for (org.apache.openejb.jee.oejb3.Query query : ejbDeployment.getQuery()) {
                    NamedQuery namedQuery = new NamedQuery();
                    org.apache.openejb.jee.oejb3.QueryMethod queryMethod = query.getQueryMethod();

                    // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
                    StringBuilder name = new StringBuilder();
                    name.append(entityName).append(".").append(queryMethod.getMethodName());
                    if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                        name.append('(');
                        boolean first = true;
                        for (String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                            if (!first) name.append(",");
                            name.append(methodParam);
                            first = false;
                        }
                        name.append(')');
                    }
                    namedQuery.setName(name.toString());

                    namedQuery.setQuery(query.getObjectQl());
                    entity.getNamedQuery().add(namedQuery);
                }
            }
        }
        entityMappings.getMappedSuperclass().addAll(mappedSuperclassByClass.values());

        Relationships relationships = ejbJar.getRelationships();
        if (relationships != null) {
            for (EjbRelation relation : relationships.getEjbRelation()) {
                List<EjbRelationshipRole> roles = relation.getEjbRelationshipRole();
                // if we don't have two roles, the relation is bad so we skip it
                if (roles.size() != 2) {
                    continue;
                }

                // get left entity
                EjbRelationshipRole leftRole = roles.get(0);
                RelationshipRoleSource leftRoleSource = leftRole.getRelationshipRoleSource();
                String leftEjbName = leftRoleSource == null ? null : leftRoleSource.getEjbName();
                Entity leftEntity = entitiesByName.get(leftEjbName);

                // get right entity
                EjbRelationshipRole rightRole = roles.get(1);
                RelationshipRoleSource rightRoleSource = rightRole.getRelationshipRoleSource();
                String rightEjbName = rightRoleSource == null ? null : rightRoleSource.getEjbName();
                Entity rightEntity = entitiesByName.get(rightEjbName);

                // neither left or right have a mapping which is fine
                if (leftEntity == null && rightEntity == null) {
                    continue;
                }
                // left not found?
                if (leftEntity == null) {
                    throw new OpenEJBException("Role source " + leftEjbName + " defined in relationship role " +
                            relation.getEjbRelationName() + "::" + leftRole.getEjbRelationshipRoleName() + " not found");
                }
                // right not found?
                if (rightEntity == null) {
                    throw new OpenEJBException("Role source " + rightEjbName + " defined in relationship role " +
                            relation.getEjbRelationName() + "::" + rightRole.getEjbRelationshipRoleName() + " not found");
                }

                String leftFieldName = null;
                boolean leftSynthetic = false;
                if (leftRole.getCmrField() != null) {
                    leftFieldName = leftRole.getCmrField().getCmrFieldName();
                } else {
                    leftFieldName = rightEntity.getName() + "_" + rightRole.getCmrField().getCmrFieldName();
                    leftSynthetic = true;
                }
                boolean leftIsOne = leftRole.getMultiplicity() == Multiplicity.ONE;

                String rightFieldName = null;
                boolean rightSynthetic = false;
                if (rightRole.getCmrField() != null) {
                    rightFieldName = rightRole.getCmrField().getCmrFieldName();
                } else {
                    rightFieldName = leftEntity.getName() + "_" + leftRole.getCmrField().getCmrFieldName();
                    rightSynthetic = true;
                }
                boolean rightIsOne = rightRole.getMultiplicity() == Multiplicity.ONE;

                if (leftIsOne && rightIsOne) {
                    //
                    // one-to-one
                    //

                    // left
                    OneToOne leftOneToOne = null;
                    leftOneToOne = new OneToOne();
                    leftOneToOne.setName(leftFieldName);
                    leftOneToOne.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftOneToOne);
                    leftEntity.getAttributes().getOneToOne().add(leftOneToOne);

                    // right
                    OneToOne rightOneToOne = null;
                    rightOneToOne = new OneToOne();
                    rightOneToOne.setName(rightFieldName);
                    rightOneToOne.setSyntheticField(rightSynthetic);
                    rightOneToOne.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightOneToOne);
                    rightEntity.getAttributes().getOneToOne().add(rightOneToOne);

                    // link
                    leftOneToOne.setRelatedField(rightOneToOne);
                    rightOneToOne.setRelatedField(leftOneToOne);
                } else if (leftIsOne && !rightIsOne) {
                    //
                    // one-to-many
                    //

                    // left
                    OneToMany leftOneToMany = null;
                    leftOneToMany = new OneToMany();
                    leftOneToMany.setName(leftFieldName);
                    leftOneToMany.setSyntheticField(leftSynthetic);
                    leftOneToMany.setMappedBy(rightFieldName);
                    setCascade(rightRole, leftOneToMany);
                    leftEntity.getAttributes().getOneToMany().add(leftOneToMany);

                    // right
                    ManyToOne rightManyToOne = null;
                    rightManyToOne = new ManyToOne();
                    rightManyToOne.setName(rightFieldName);
                    rightManyToOne.setSyntheticField(rightSynthetic);
                    setCascade(leftRole, rightManyToOne);
                    rightEntity.getAttributes().getManyToOne().add(rightManyToOne);

                    // link
                    leftOneToMany.setRelatedField(rightManyToOne);
                    rightManyToOne.setRelatedField(leftOneToMany);
                } else if (!leftIsOne && rightIsOne) {
                    //
                    // many-to-one
                    //

                    // left
                    ManyToOne leftManyToOne = null;
                    leftManyToOne = new ManyToOne();
                    leftManyToOne.setName(leftFieldName);
                    leftManyToOne.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftManyToOne);
                    leftEntity.getAttributes().getManyToOne().add(leftManyToOne);

                    // right
                    OneToMany rightOneToMany = null;
                    rightOneToMany = new OneToMany();
                    rightOneToMany.setName(rightFieldName);
                    rightOneToMany.setSyntheticField(rightSynthetic);
                    rightOneToMany.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightOneToMany);
                    rightEntity.getAttributes().getOneToMany().add(rightOneToMany);

                    // link
                    leftManyToOne.setRelatedField(rightOneToMany);
                    rightOneToMany.setRelatedField(leftManyToOne);
                } else if (!leftIsOne && !rightIsOne) {
                    //
                    // many-to-many
                    //

                    // left
                    ManyToMany leftManyToMany = null;
                    leftManyToMany = new ManyToMany();
                    leftManyToMany.setName(leftFieldName);
                    leftManyToMany.setSyntheticField(leftSynthetic);
                    setCascade(rightRole, leftManyToMany);
                    leftEntity.getAttributes().getManyToMany().add(leftManyToMany);

                    // right
                    ManyToMany rightManyToMany = null;
                    rightManyToMany = new ManyToMany();
                    rightManyToMany.setName(rightFieldName);
                    rightManyToMany.setSyntheticField(rightSynthetic);
                    rightManyToMany.setMappedBy(leftFieldName);
                    setCascade(leftRole, rightManyToMany);
                    rightEntity.getAttributes().getManyToMany().add(rightManyToMany);

                    // link
                    leftManyToMany.setRelatedField(rightManyToMany);
                    rightManyToMany.setRelatedField(leftManyToMany);
                }
View Full Code Here

        // get left entity
        final EjbRelationshipRole leftRole = roles.get(0);
        final RelationshipRoleSource leftRoleSource = leftRole.getRelationshipRoleSource();
        final String leftEjbName = leftRoleSource == null ? null : leftRoleSource.getEjbName();
        final Entity leftEntity = entitiesByEjbName.get(leftEjbName);

        // get right entity
        final EjbRelationshipRole rightRole = roles.get(1);
        final RelationshipRoleSource rightRoleSource = rightRole.getRelationshipRoleSource();
        final String rightEjbName = rightRoleSource == null ? null : rightRoleSource.getEjbName();
        final Entity rightEntity = entitiesByEjbName.get(rightEjbName);

        // neither left or right have a mapping which is fine
        if (leftEntity == null && rightEntity == null) {
            return;
        }
        // left not found?
        if (leftEntity == null) {
            throw new OpenEJBException("Role source " + leftEjbName + " defined in relationship role " +
                relation.getEjbRelationName() + "::" + leftRole.getEjbRelationshipRoleName() + " not found");
        }
        // right not found?
        if (rightEntity == null) {
            throw new OpenEJBException("Role source " + rightEjbName + " defined in relationship role " +
                relation.getEjbRelationName() + "::" + rightRole.getEjbRelationshipRoleName() + " not found");
        }

        final Attributes rightAttributes = rightEntity.getAttributes();
        final Map<String, RelationField> rightRelationships = rightAttributes.getRelationshipFieldMap();
        final Attributes leftAttributes = leftEntity.getAttributes();
        final Map<String, RelationField> leftRelationships = leftAttributes.getRelationshipFieldMap();

        final String leftFieldName;
        boolean leftSynthetic = false;
        if (leftRole.getCmrField() != null) {
            leftFieldName = leftRole.getCmrField().getCmrFieldName();
        } else {
            leftFieldName = rightEntity.getName() + "_" + rightRole.getCmrField().getCmrFieldName();
            leftSynthetic = true;
        }
        final boolean leftIsOne = leftRole.getMultiplicity() == Multiplicity.ONE;

        final String rightFieldName;
View Full Code Here

                entityMappings.getMappedSuperclass().add(mappedSuperclass);
            }
        }

        // Look for an existing mapping using the openejb generated subclass name
        Entity entity = removeEntity(userMappings, jpaEntityClassName);

        // DMB: For the first iteration, we're not going to allow
        // anything other than the ugly mapping file we generate.
        // So if they supplied an entity, it better be correct
        // because we are going to ignore all other xml metadata.
        if (entity != null) {
            // XmlMetadataComplete is an OpenEJB specific flag that
            // tells all other legacy descriptor converters to keep
            // their hands off.
            entity.setXmlMetadataComplete(true);

            entityMappings.getEntity().add(entity);

            return;
        }

// This section is an in progress TODO
//        if (entity == null){
//            entity = removeEntity(userMappings, ejbClass.getName());
//            // OVERWRITE: class: impl class name
//            if (entity != null) {
//                entity.setClazz(jpaEntityClassName);
//
//                if (Modifier.isAbstract(ejbClass.getModifiers())){
//                    // This is a CMP2 bean and we allowed the user to
//                    // define it via the orm.xml file as an <entity>
//                    // We need to split this definition.  We need
//                    // an <entity> definition for the generated subclass
//                    // and a <mapped-superclass> for the bean class
//
//
//                }
//            }
//        }

        if (entity == null) {
            entity = new Entity(jpaEntityClassName);
        }

        // Aggressively add an "Attributes" instance so we don't
        // have to check for null everywhere.
        if (entity.getAttributes() == null) {
            entity.setAttributes(new Attributes());
        }

        // add the entity
        entityMappings.getEntity().add(entity);

        // OVERWRITE: description: contains the name of the entity bean
        entity.setDescription(ejbModule.getModuleId() + "#" + bean.getEjbName());


        // PRESERVE has queries: name: the name of the entity in queries
        final String entityName = bean.getAbstractSchemaName();
        entity.setName(entityName);
        entity.setEjbName(bean.getEjbName());


        final ClassLoader classLoader = ejbModule.getClassLoader();
        final Collection<MappedSuperclass> mappedSuperclasses;
        if (bean.getCmpVersion() == CmpVersion.CMP2) {
            // perform the 2.x class mapping.  This really just identifies the primary key and
            // other cmp fields that will be generated for the concrete class and identify them
            // to JPA.
            mappedSuperclasses = mapClass2x(entity, bean, classLoader);
        } else {
            // map the cmp class, but if we are using a mapped super class,
            // generate attribute-override instead of id and basic
            mappedSuperclasses = mapClass1x(bean.getEjbClass(), entity, bean, classLoader);
        }

        // if we have superclass mappings to process, add those to the
        // configuration. f
        if (mappedSuperclasses != null) {
            // now that things are mapped out, add the superclass mappings to the entity mappings
            // that will get passed to the JPA engine.
            for (final MappedSuperclass mappedSuperclass : mappedSuperclasses) {
                entityMappings.getMappedSuperclass().add(mappedSuperclass);
            }
        }

        // process queries
        for (final Query query : bean.getQuery()) {
            final NamedQuery namedQuery = new NamedQuery();
            final QueryMethod queryMethod = query.getQueryMethod();

            // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
            final StringBuilder name = new StringBuilder();
            name.append(entityName).append(".").append(queryMethod.getMethodName());
            if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                name.append('(');
                boolean first = true;
                for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                    if (!first) {
                        name.append(",");
                    }
                    name.append(methodParam);
                    first = false;
                }
                name.append(')');
            }
            namedQuery.setName(name.toString());

            namedQuery.setQuery(query.getEjbQl());
            entity.getNamedQuery().add(namedQuery);
        }

        // todo: there should be a common interface between ejb query object and openejb query object
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
        if (ejbDeployment != null) {
            for (final org.apache.openejb.jee.oejb3.Query query : ejbDeployment.getQuery()) {
                final NamedQuery namedQuery = new NamedQuery();
                final org.apache.openejb.jee.oejb3.QueryMethod queryMethod = query.getQueryMethod();

                // todo deployment id could change in one of the later conversions... use entity name instead, but we need to save it off
                final StringBuilder name = new StringBuilder();
                name.append(entityName).append(".").append(queryMethod.getMethodName());
                if (queryMethod.getMethodParams() != null && !queryMethod.getMethodParams().getMethodParam().isEmpty()) {
                    name.append('(');
                    boolean first = true;
                    for (final String methodParam : queryMethod.getMethodParams().getMethodParam()) {
                        if (!first) {
                            name.append(",");
                        }
                        name.append(methodParam);
                        first = false;
                    }
                    name.append(')');
                }
                namedQuery.setName(name.toString());

                namedQuery.setQuery(query.getObjectQl());
                entity.getNamedQuery().add(namedQuery);
            }
        }
    }
View Full Code Here

            }
        }
    }

    private Entity removeEntity(final EntityMappings userMappings, final String className) {
        final Entity entity;

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

TOP

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

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.