3.9.11.5. 新建实体实例

可以使用 POST 请求地址创建新的 sales$Order 实体实例:

http://localhost:8080/app/rest/v2/entities/sales$Order

必须将 OAuth 令牌放在带有 Bearer 类型的 Authorization 请求头中。

请求体必须包含一个描述新实体实例的 JSON 对象,例如:

  1. {
  2. "number": "00017",
  3. "date": "2016-09-01",
  4. "description": "Back to school",
  5. "items": [
  6. {
  7. "_entityName": "sales$OrderItem",
  8. "price": 100,
  9. "name": "School bag"
  10. },
  11. {
  12. "_entityName": "sales$OrderItem",
  13. "price": 9.90,
  14. "name": "Pencils"
  15. }
  16. ],
  17. "customer": {
  18. "id": "4aa9a9d8-01df-c8df-34c8-c385b566ea05"
  19. }
  20. }

以下是 cURL POST 请求的示例,该请求创建了一个新的 Order 实例:

  1. curl -H "Authorization: Bearer d335902c-9cb4-455e-bf92-24ca1d66d72f" -H "Content-Type: application/json" -X POST -d "{\"date\": \"2018-10-12 15:47:28\", \"amount\": 9.90, \"customer\": {\"id\": \"383ebce2-b295-7378-36a1-bcf93693821f\"}}" http://localhost:8080/app/rest/v2/entities/sales$Order

订单项(items)集合和 customer 的引用需要通过请求体传递。我们来看看如何处理这些属性。

首先,我们快速浏览一下 Order 类:

  1. package com.company.sales.entity;
  2. import com.haulmont.chile.core.annotations.Composition;
  3. import com.haulmont.chile.core.annotations.NamePattern;
  4. import com.haulmont.cuba.core.entity.StandardEntity;
  5. import com.haulmont.cuba.core.entity.annotation.OnDelete;
  6. import com.haulmont.cuba.core.global.DeletePolicy;
  7. import javax.persistence.*;
  8. import java.util.Date;
  9. import java.util.Set;
  10. @NamePattern("%s|number")
  11. @Table(name = "SALES_ORDER")
  12. @Entity(name = "sales$Order")
  13. public class Order extends StandardEntity {
  14. private static final long serialVersionUID = 7565070704618724997L;
  15. @Column(name = "NUMBER_")
  16. protected String number;
  17. @Temporal(TemporalType.DATE)
  18. @Column(name = "DATE_")
  19. protected Date date;
  20. @Column(name = "DESCRIPTION")
  21. protected String description;
  22. @ManyToOne(fetch = FetchType.LAZY)
  23. @JoinColumn(name = "CUSTOMER_ID")
  24. protected Customer customer;
  25. @Composition
  26. @OnDelete(DeletePolicy.CASCADE)
  27. @OneToMany(mappedBy = "order")
  28. protected Set<OrderItem> items;
  29. //getters and setters omitted
  30. }

items 集合属性使用@Composition注解。用于实体创建和更新的 REST API 方法将为此类集合的所有成员创建新的实体实例。在我们的例子中,将使用 Order 实体创建两个 OrderItem 实体实例。

customer 引用没有 @Composition 注解,这也是为什么 REST API 会尝试查找具有给定 id 的客户并将其设置给 customer 字段。如果未找到客户,则不会创建订单,并且该方法将返回错误。

在成功执行方法的情况下,返回所创建实体的完整对象图:

  1. {
  2. "_entityName": "sales$Order",
  3. "id": "5d7ff8e3-7828-ba94-d6ba-155c5c4f2a50",
  4. "date": "2016-09-01",
  5. "description": "Back to school",
  6. "version": 1,
  7. "number": "00017",
  8. "createdBy": "admin",
  9. "createTs": "2016-10-13 18:12:21.047",
  10. "updateTs": "2016-10-13 18:12:21.047",
  11. "items": [
  12. {
  13. "_entityName": "sales$OrderItem",
  14. "id": "3158b8ed-7b7a-568e-aec5-0822c3ebbc24",
  15. "createdBy": "admin",
  16. "price": 9.9,
  17. "name": "Pencils",
  18. "createTs": "2016-10-13 18:12:21.047",
  19. "version": 1,
  20. "updateTs": "2016-10-13 18:12:21.047",
  21. "order": {
  22. "_entityName": "sales$Order",
  23. "id": "5d7ff8e3-7828-ba94-d6ba-155c5c4f2a50"
  24. }
  25. },
  26. {
  27. "_entityName": "sales$OrderItem",
  28. "id": "72774b8b-4fea-6403-7b52-4a6a749215fc",
  29. "createdBy": "admin",
  30. "price": 100,
  31. "name": "School bag",
  32. "createTs": "2016-10-13 18:12:21.047",
  33. "version": 1,
  34. "updateTs": "2016-10-13 18:12:21.047",
  35. "order": {
  36. "_entityName": "sales$Order",
  37. "id": "5d7ff8e3-7828-ba94-d6ba-155c5c4f2a50"
  38. }
  39. }
  40. ],
  41. "customer": {
  42. "_entityName": "sales$Customer",
  43. "id": "4aa9a9d8-01df-c8df-34c8-c385b566ea05",
  44. "firstName": "Toby",
  45. "lastName": "Burns",
  46. "createdBy": "admin",
  47. "createTs": "2016-10-13 15:32:01.657",
  48. "version": 1,
  49. "updateTs": "2016-10-13 15:32:01.657"
  50. }
  51. }