Skip to content

Parameter Construction

There are a number of ways to access the information in the service metadata document to pass them as parameters and enable Dynamic API to interact with OData provider. They are listed in order of ease of use:

  1. Proxy Classes
  2. Dynamic API
  3. Metadata API

This section compares proxy classes with Dynamic API to illustrate the convenience offered by the proxy classes. Please note that both ways require knowledge of the data model with one significant difference: IDE can leverage the proxy classes to offer syntax assist and code completion.

Entity Set

1
2
3
4
5
6
7
// Proxy Class
// Use the generated metadata class and the class variable for the entity set
EntitySet eventSet = EventServiceMetadata.EntitySets.events;

// Dynamic API
// Look up via DataService using the name of the entity set
EntitySet eventSet = dataService.getEntitySet("Events");
1
2
3
4
5
6
7
// Proxy Class
// Use the generated metadata class and the class variable for the entity set
val eventSet = EventServiceMetadata.EntitySets.events

// Dynamic API
// Look up via DataService using the name of the entity set
val eventSet = dataService.getEntitySet("Events")

Entity Type

1
2
3
4
5
6
7
// Proxy Class
// use the generated metadata class and the class variable for the entity type
EntityType eventType = EventServiceMetadata.EntityTypes.event;
// Dynamic API
// Retrieve the entity type via the entity set 
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
1
2
3
4
5
6
7
// Proxy Class
// use the generated metadata class and the class variable for the entity type
val eventType = EventServiceMetadata.EntityTypes.event
// Dynamic API
// Retrieve the entity type via the entity set 
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entityType

Note

You can only get to an entity type without first looking up the entity set using Metadata API.

Property

For entity:

1
2
3
4
5
6
7
8
9
// Proxy Class
// use the generated strongly typed class and the class variable for the property
Property eventNameProp = Event.name;
// Dynamic API
// Retrieve the entity type via the entity set
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
// Retrieve a property of the entity type using its name
Property eventNameProp = eventType.getProperty("Name");
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Proxy Class
// use the generated strongly typed class and the class variable for the property
val eventNameProp = Event.name

// Dynamic API
// Retrieve the entity type via the entity set
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entityType
// Retrieve a property of the entity type using its name
val eventNameProp = eventType.getProperty("Name")

We do not have an example for complex type in our sample OData service. However, their properties are retrieved in a similar way. Consider we have a complex type Address with street, city, state and zipcode, to access the property for city:

1
2
3
4
5
6
7
// Proxy Class
// use the generated strongly typed complex class and the class variable for the property
Property addressCityProp = Address.city;
// Dynamic API
// Retrieve the complex type from DataService, use namespace + name
ComplexType addressType = dataService.getMetadata().getComplexType("ODataAPI.Address");
Property addressCityProp = addressType.getProperty("City");
1
2
3
4
5
6
7
// Proxy Class
// use the generated strongly typed complex class and the class variable for the property
val addressCityProp = Address.city
// Dynamic API
// Retrieve the complex type from DataService, use namespace + name
val addressType = dataService.metadata.getComplexType("ODataAPI.Address")
val addressCityProp = addressType.getProperty("City")

EntityValue

To create an instance of EntityValue for an entity type:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Proxy Class
// Use the constructor of the generated strongly type generated class
Event event = new Event();

// Dynamic API 
EntitySet eventSet = dataService.getEntitySet("Events");
EntityType eventType = eventSet.getEntityType();
// Create a new instance of Event
EntityValue entityValue = EntityValue.ofType(eventType);
To set the property values of an EntityValue instance
// Proxy Class
event.setName("SAP Sapphire 2016");
event.setDescription("ASUG Annual Conference");
event.setCountry("USA");

// Dynamic API
eventType.getProperty("Name").setString(entityValue, "SAP Sapphire 2016");
eventType.getProperty("Description").setString(entityValue, "ASUG Annual Conference");
eventType.getProperty("Country").setString(entityValue, "USA");
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Proxy Class
// Use the constructor of the generated strongly type generated class
val event = Event()

// Dynamic API 
val eventSet = dataService.getEntitySet("Events")
val eventType = eventSet.entitySet
// Create a new instance of Event
val entityValue = EntityValue.ofType(eventType)
To set the property values of an EntityValue instance
// Proxy Class
event.name = "SAP Sapphire 2016"
event.description = "ASUG Annual Conference"
event.country = "USA"

// Dynamic API
eventType.getProperty("Name").setString(entityValue, "SAP Sapphire 2016")
eventType.getProperty("Description").setString(entityValue, "ASUG Annual Conference")
eventType.getProperty("Country").setString(entityValue, "USA")

Similarly, to retrieve the property value from an EntityValue instance

1
2
3
4
5
6
7
8
9
// Proxy Class
String name = event.getName();
String description = event.getDescription();
String country = event.getCountry();

// Dynamic API
String name = eventType.getProperty("Name").getNullableString(entityValue);
String description = eventType.getProperty("Description").getNullableString(entityValue);
String country = eventType.getProperty("Country").getNullableString(entityValue);
1
2
3
4
5
6
7
8
9
// Proxy Class
val name = event.name
val description = event.description
val country = event.country

// Dynamic API
val name = eventType.getProperty("Name").getNullableString(entityValue)
val description = eventType.getProperty("Description").getNullableString(entityValue)
val country = eventType.getProperty("Country").getNullableString(entityValue)

Note

The generated getter handles if the value of the property is nullable and returns null when appropriate. You must use the nullable version of the getter method to avoid an exception if the property is nullable. For example:

1
2
// getString may throw an error if the value is indeed null
String country = eventType.getProperty("Country").getString(entityValue);
1
2
// getString may throw an error if the value is indeed null
val country = eventType.getProperty("Country").getString(entityValue)

Entity Key

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Proxy Class
// Simple key, use the generated strongly typed class key method
EntityKey eventKey = Event.key(1000L);

// Composite key
// Use the generated strongly typed class key method
EntityKey userSessionAgendaKey = UserSessionAgenda.key(1000L, "user");

// Dynamic API
// Simple Key
DataValue keyValue = LongValue.of(1000L);
EntityKey eventKey = new EntityKey().with("EventID", keyValue);

// Composite Key
DataValue sessionIdValue = LongValue.of(1000L);
DataValue userIdValue = StringValue.of("user");
EntityKey key = new EntityKey().with("SessionID", sessionIdValue).with("UserID", userIdValue);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Proxy Class
// Simple key, use the generated strongly typed class key method
va; eventKey = Event.key(1000L)

// Composite key
// Use the generated strongly typed class key method
val userSessionAgendaKey = UserSessionAgenda.key(1000L, "user")

// Dynamic API
// Simple Key
val keyValue = LongValue.of(1000L)
val eventKey = EntityKey().with("EventID", keyValue)

// Composite Key
val sessionIdValue = LongValue.of(1000L)
val userIdValue = StringValue.of("user")
val key = EntityKey().with("SessionID", sessionIdValue).with("UserID", userIdValue)