Odata 如何在olingo中创建复杂类型集合
我尝试按照下面的olingo文档创建odata服务 但是我无法创建一个属性类型为ComplexType列表的实体。Odata 如何在olingo中创建复杂类型集合,odata,olingo,Odata,Olingo,我尝试按照下面的olingo文档创建odata服务 但是我无法创建一个属性类型为ComplexType列表的实体。 任何人都有这样的例子。还是不支持它?我昨天在项目中遇到了类似的需求。就在那时,我注意到你的问题没有得到回答。 我实施了一种适合我的方法。我在下面描述它。希望这会有所帮助 简而言之:我的理解是,我们不能使用ComplexType将实体作为列表。我们需要使用导航属性。 问题陈述:我必须阅读实体-EntityA。EntityA与EntityB相关。关系是一对多的,即EntityA包含E
任何人都有这样的例子。还是不支持它?我昨天在项目中遇到了类似的需求。就在那时,我注意到你的问题没有得到回答。 我实施了一种适合我的方法。我在下面描述它。希望这会有所帮助 简而言之:我的理解是,我们不能使用ComplexType将实体作为列表。我们需要使用导航属性。 问题陈述:我必须阅读实体-EntityA。EntityA与EntityB相关。关系是一对多的,即EntityA包含EntityB的列表。典型的JSON表示如下所示:
EntityA:{
"id":"entityA_1",
"entityBList":[
{//EntityB Element
"id" : "entityB_1",
"description":"value1"
},
{//EntityB Element
"id" : "entityB_2",
"description":"value1"
}
]
}
为了从客户端读取它,我使用了一个OData查询expand-OData.sv/EntityA(1)?$expand=EntityB
解决方案:
为了在服务器端启用上述场景,我必须使用导航属性
- 首先,我们创建两个独立的实体-EntityA和EntityB。然后我们 通过以下方式定义关系:
- getAssociation方法的实现及其应用
- 在EdmProvider中,在定义EntityA时设置导航属性
- 接下来,在读取实体(readEntity)时,我们提供回调
方法,重写“retrieveFeedResult”以填充EntityB
希望这有帮助。也许当时Utsav的回答是正确的,但Odata v4确实支持一个属性类型为“复合类型列表”的实体。
证明: 在entityType中,Person具有属性AddressInfo,其类型是ComplexType的集合:
<ComplexType Name="Location" OpenType="true">
<Property Name="Address" Type="Edm.String" Nullable="false"/>
<Property Name="City" Type="Microsoft.OData.SampleService.Models.TripPin.City" Nullable="false"/>
</ComplexType>
...
<EntityType Name="Person" OpenType="true">
<Property Name="AddressInfo" Type="Collection(Microsoft.OData.SampleService.Models.TripPin.Location)"/>
...
</EntityType>
在getEntityType()
方法中,必须将属性创建为复杂类型对象的集合:
//...initialization of entityType...
List<CsdlProperty> properties = new ArrayList<>();
FullQualifiedName type;
//...initialization of the type as a complex type...
properties.add(new CsdlProperty().setName("propertyName").setType(type).setCollection(true));
entityType.setProperties(properties);
//...
OData v4允许您使用ComplexType将实体作为列表。@Taraskoh但我们可以使用嵌套的ComplexType。(即实体A有一个复杂类型的实体B集合)这个答案对我来说非常有帮助,因为我正在处理odata服务。但在进行了以下实践之后,我仍然被一个问题所阻碍,即在尝试访问实体集时,我遇到了错误:找不到名称为namespace.complexTypeName的类型。有趣的是,在元数据中,EntityType和它的属性与ComplexType之间的关系是正确的,但是当我尝试获取EntitySet时,出现了错误。答案是很久以前的了,你还记得细节吗?@Blangero如果没有一个最小的可复制代码样本,就不可能回答你。以这个项目为例:或者在这里问一个新问题
//...initialization of entityType...
List<CsdlProperty> properties = new ArrayList<>();
FullQualifiedName type;
//...initialization of the type as a complex type...
properties.add(new CsdlProperty().setName("propertyName").setType(type).setCollection(true));
entityType.setProperties(properties);
//...
EntityCollection entities = new EntityCollection();
List<Entity> eList = entities.getEntities();
Entity e = new Entity();
List<Map> data;
//...initialization of complex type's data...
List<ComplexValue> properties = new ArrayList<>();
for (Object complexObject : data) {
ComplexValue complexValue = new ComplexValue();
for (Map.Entry<String, Object> entry : complexObject.entrySet()) {
complexValue.getValue().add(new Property(null, entry.getKey(), ValueType.PRIMITIVE, entry.getValue()));
}
properties.add(complexValue);
}
e.addProperty(new Property(null, "propertyName", ValueType.COLLECTION_COMPLEX, properties););
eList.add(e);
//...serialize and set the result to response