@Test public void checkCreateLinkedEntity() throws ODataJPAProcessException, ODataJPAModelException { final JPAEntityType et = helper.getJPAEntityType("AdministrativeDivisions"); final JPAAssociationPath path = et.getAssociationPath("Children"); final Map<JPAAssociationPath, List<JPARequestEntity>> deepEntities = new HashMap<>(); final JPARequestEntity deepEntity = mock(JPARequestEntity.class); final Map<String, Object> deepData = new HashMap<>(); final AdministrativeDivision parent = new AdministrativeDivision(new AdministrativeDivisionKey("Eurostat", "NUTS1", "DE5")); doReturn(et).when(requestEntity).getEntityType(); keys.put("divisionCode", "DE5"); keys.put("codeID", "NUTS1"); keys.put("codePublisher", "Eurostat"); doReturn(deepEntities).when(requestEntity).getRelatedEntities(); deepEntities.put(path, Arrays.asList(deepEntity)); doReturn(et).when(deepEntity).getEntityType(); doReturn(deepData).when(deepEntity).getData(); doReturn(new JPAModifyUtil()).when(deepEntity).getModifyUtil(); deepData.put("DivisionCode", "DE52"); deepData.put("CodeID", "NUTS2"); deepData.put("CodePublisher", "Eurostat"); deepData.put("CountryCode", "DEU"); doReturn(parent).when(em).getReference(eq(et.getTypeClass()), any()); final AdministrativeDivision act = (AdministrativeDivision) cut.createEntity(requestEntity, em); assertNotNull(act.getChildren()); assertEquals(1, act.getChildren().size()); }
@Override public Object createEntity(final JPARequestEntity requestEntity, final EntityManager em) throws ODataJPAProcessException { Object instance = null; if (requestEntity.getKeys().isEmpty()) { instance = createOneEntity(requestEntity, em, null); final Object old = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil().createPrimaryKey(requestEntity.getEntityType(), instance)); if (old != null) throw new JPAExampleModifyException(ENTITY_ALREADY_EXISTS, HttpStatusCode.BAD_REQUEST); } else { instance = findEntity(requestEntity, em); } processRelatedEntities(requestEntity.getRelatedEntities(), instance, requestEntity.getModifyUtil(), em); em.persist(instance); return instance; }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public Object createEntity(final JPARequestEntity requestEntity, final EntityManager em) throws ODataJPAProcessException { Object instance = null; if (requestEntity.getKeys().isEmpty()) { instance = createOneEntity(requestEntity, em, null); final Object old = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil().createPrimaryKey(requestEntity.getEntityType(), instance)); if (old != null) throw new JPAExampleModifyException(ENTITY_ALREADY_EXISTS, HttpStatusCode.BAD_REQUEST); } else { instance = findEntity(requestEntity, em); } processRelatedEntities(requestEntity.getRelatedEntities(), instance, requestEntity.getModifyUtil(), em); em.persist(instance); return instance; } }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public Object createEntity(final JPARequestEntity requestEntity, final EntityManager em) throws ODataJPAProcessException { Object instance = null; if (requestEntity.getKeys().isEmpty()) { instance = createOneEntity(requestEntity, em, null); final Object old = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil().createPrimaryKey(requestEntity.getEntityType(), instance)); if (old != null) throw new JPAExampleModifyException(ENTITY_ALREADY_EXISTS, HttpStatusCode.BAD_REQUEST); } else { instance = findEntity(requestEntity, em); } processRelatedEntities(requestEntity.getRelatedEntities(), instance, requestEntity.getModifyUtil(), em); em.persist(instance); return instance; } JPAExampleCUDRequestHandler(); }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public Object createEntity(final JPARequestEntity requestEntity, final EntityManager em) throws ODataJPAProcessException { Object instance = null; if (requestEntity.getKeys().isEmpty()) { instance = createOneEntity(requestEntity, em, null); final Object old = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil().createPrimaryKey(requestEntity.getEntityType(), instance)); if (old != null) throw new JPAExampleModifyException(ENTITY_ALREADY_EXISTS, HttpStatusCode.BAD_REQUEST); } else { instance = findEntity(requestEntity, em); } processRelatedEntities(requestEntity.getRelatedEntities(), instance, requestEntity.getModifyUtil(), em); em.persist(instance); return instance; } JPAExampleCUDRequestHandler(); @Override Object createEntity(final JPARequestEntity requestEntity, final EntityManager em); @Override void deleteEntity(JPARequestEntity requestEntity, EntityManager em); @Override JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em,
      final HttpMethod method); @Override void validateChanges(EntityManager em); }
JPAExampleCUDRequestHandler extends JPAAbstractCUDRequestHandler { @Override public Object createEntity(final JPARequestEntity requestEntity, final EntityManager em) throws ODataJPAProcessException { Object instance = null; if (requestEntity.getKeys().isEmpty()) { instance = createOneEntity(requestEntity, em, null); final Object old = em.find(requestEntity.getEntityType().getTypeClass(), requestEntity.getModifyUtil().createPrimaryKey(requestEntity.getEntityType(), instance)); if (old != null) throw new JPAExampleModifyException(ENTITY_ALREADY_EXISTS, HttpStatusCode.BAD_REQUEST); } else { instance = findEntity(requestEntity, em); } processRelatedEntities(requestEntity.getRelatedEntities(), instance, requestEntity.getModifyUtil(), em); em.persist(instance); return instance; } JPAExampleCUDRequestHandler(); @Override Object createEntity(final JPARequestEntity requestEntity, final EntityManager em); @Override void deleteEntity(JPARequestEntity requestEntity, EntityManager em); @Override JPAUpdateResult updateEntity(final JPARequestEntity requestEntity, final EntityManager em,
      final HttpMethod method); @Override void validateChanges(EntityManager em);  }