SAP / olingo-jpa-processor-v4

The JPA Processor fills the gap between Olingo V4 and the database, by providing a mapping between JPA metadata and OData metadata, generating queries and supporting the entity manipulations.
Apache License 2.0
123 stars 76 forks source link

How to define OData entity/attribute names different than those of the JPA entities/attributes #134

Closed blxbrgld closed 2 years ago

blxbrgld commented 3 years ago

Hello,

i'm trying to implement a mock OData server using the jpa-processor, in order to be able to write E2E tests for a flow involving an OData service. The real OData server is not under my control and defines some entities, properties etc. with names in kebab case, like the following:

<EntityType Name="elm_role" BaseType="mscrm.crmbaseentity">
        <Key>
          <PropertyRef Name="elm_role_id" />
        </Key>
        <Property Name="elm_role_name" Type="Edm.String">
        ...
</EntityType>

I do not want to define the JPA entity attributes in kebab case, and even if i do, i cannot (for example) do the same thing for the entity itself (it's a class).

Is there a way to define the desired name(s) for these entities, properties, and leave everything else to the processor? I tried to register a custom JPAEdmNameBuilder in the servlet the way shown in the following code snippet but with no luck.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
  try {
    EntityManagerFactory emf = JPAEntityManagerFactory.getEntityManagerFactory(PUNIT_NAME, new HashMap<>());
    JPAEdmProvider provider = new JPAEdmProvider(emf, null, new String[] { PACKAGE_NAME }, new ElmNameBuilder(NAMESPACE));
    OData odata = OData.newInstance();
    ServiceMetadata edm = odata.createServiceMetadata(provider, new ArrayList<>());
    odata.createHandler(edm).process(request, response);
  } catch (Exception e) {
    throw new ServletException(e);
  }
}

Is the JPAEdmNameBuilder the way to go? If not, is there some other option?

I understand that for a server under my control the whole thing would not be an issue, but still it would be nice to have a way to handle it.

Thank you.

wog48 commented 2 years ago

Hello,

In fact the JPAEdmNameBuilder is the foreseen way to override the default name creation. For a known reason in some parts some old code is in place. The JPAServiceDocumentFactory does not take the name builder and therefor it is not forwarded and not used when the name of an entity type is created.

I will provide a fix for this.

I like o mention, that for my understanding hyphens are not allowed in entity names. See: https://docs.oasis-open.org/odata/odata-csdl-json/v4.01/odata-csdl-json-v4.01.html#sec_SimpleIdentifier. This will lead to trouble in case you use Olingo or the JPA Processor e.g. to retrieve data.

wog48 commented 2 years ago

Hello,

a fix is part of version 0.3.10. If you still have a problem, reopne the issue.