Persisting oneToOne composition via REST

I want to store an entity which has an onToOne property. Like Customer with an address.


 @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
protected address

If I send a json to the customer REST-endpoint I get an error


{
  "error": "Entity creation failed",
  "details": "Referenced entity for property 'address' with id = 9e866ec7-d967-0237-06d0-b43d2fc0e565 is missing"
}

Is there a way to send the address within one POST-request to create a customer with address?

Hi,

As I understood, Person and Address entities should be persisted together. In the case, the link attribute should have the @Composition annotation.

Please see the following documentation:

  1. https://doc.cuba-platform.com/manual-6.5/rest_api_v2_ex_create_entity.html

  2. http://files.cuba-platform.com/swagger/#!/Entities/post_entities_entityName

In my test project, I created a couple of entities and adjusted the relation between them similar to yours.


    @Composition
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "ADDRESS_ID")
    protected Address address;

Then I created Person with Address with the following POST request:


{
  "name": "Person17",
   "address": 
    {
    "_entityName": "testrest$Address",
        "postcode": "PCOne",
        "version": 1,
        "line2": "123",
        "line1": "456"
        }
}

And it was sucessfull:


{
    "_entityName": "testrest$Person",
    "id": "18685161-e208-4a74-22f9-07827460b9a1",
    "address": {
        "_entityName": "testrest$Address",
        "id": "678051ab-cd60-e59e-074d-1233219bc142",
        "createdBy": "admin",
        "postcode": "PCOne",
        "createTs": "2017-05-04 14:24:43.559",
        "version": 1,
        "updateTs": "2017-05-04 14:24:43.559",
        "line2": "123",
        "line1": "456"
    },
    "createdBy": "admin",
    "name": "Person17",
    "createTs": "2017-05-04 14:24:43.559",
    "version": 1,
    "updateTs": "2017-05-04 14:24:43.559"
}

Regards.

If the link attribute has no @Composition annotation the entities are handled in another way: before saving Person the associated Address is searched (by ID) in the database and if there is no appropriate instance the error occurs.

If @Composition is inadmissible the client should send Address before Person in a separate transaction.

Thanks for the quick answer! @Composition works!

I thought @Composition is only for many associations.

In platform 6.5 @Composition for one-to-one references affects only REST API, but we are going to support it in the generic UI too, see PL-8708.