Search code examples
javaapacheodataolingo

Ignoring Field in Apache Olingo


Currently my Project is using the JPA for the Database Connection.

I'm also using the default OdataJPA Processor.

How can i achieve not to include certain fields for example ("password") in my odata2 API response. Or do I really have to implement a customOdataJPAProcessor?


Solution

  • The easiest way for excluding some JPA entity attributes is to define a JPA-EDM mapping model. This is basically an XML file which adheres to this schema. You can read more about it in the documentation here: redefining OData JPA metadata.

    You have two different ways of linking the mapping model XML, either you specify a file name of a file located in the WEB-INF folder (assuming that you are building a WAR) or, if this is not flexible enough, you can create a JPA EDM extension and return the mapping model file as a stream.

    This is how such a file may look like:

    <?xml version="1.0" encoding="UTF-8"?>
    <JPAEDMMappingModel xmlns="http://www.apache.org/olingo/odata2/jpa/processor/api/model/mapping">
        <PersistenceUnit name="My_Persistence_Unit">
            <JPAEntityTypes>
                <JPAEntityType name="MyEntity">
                    <EDMEntityType>MyEntity</EDMEntityType>
                    <EDMEntitySet>MyEntities</EDMEntitySet>
                    <JPAAttributes>
                        <JPAAttribute name="attribute" exclude="true" />
                    </JPAAttributes>
                    <JPARelationships />
                </JPAEntityType>
            </JPAEntityTypes>
            <JPAEmbeddableTypes />
        </PersistenceUnit>
    </JPAEDMMappingModel>