Spring data rest status 500 for entity with relation

后端 未结 1 1371
暖寄归人
暖寄归人 2021-01-13 13:12

I have two entities with a relation one to many

public class Order {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    pri         


        
相关标签:
1条回答
  • 2021-01-13 14:00

    Just add parameter exported=false to @RepositoryRestResrouce annotation in your Item repo. Then you can create Order with Item in one request:

    {
        "userName": "Marco",
        "company": "MP",
        "items": [
            {
                 "name": "CD",
                 "quantity": "10"
            },
            { 
                 "name": "DVD",
                 "quantity": "5" 
            }
        ]
    }
    

    I recommend you to add cascade = CascadeType.ALL, orphanRemoval = true to items in Order to make items fully managed by Order.

    Additional info.


    If you want to work with Order and Item independently you can leave all as is but first create you items:

    POST http://localhost:8080/items
    {
            {
                 "name": "CD",
                 "quantity": "10"
            },
            { 
                 "name": "DVD",
                 "quantity": "5" 
            }
    }
    

    then create Order that contains them:

    POST http://localhost:8080/orders
    {
        {
            "userName": "Marco",
            "company": "MP",
            "items": [
                "http://localhost:8080/items/1",
                "http://localhost:8080/items/2"
            ]
        }
    }
    

    You use @JoinColumn annotation with items - so I think you must implement synchronization between Order and Items in that case...


    And please don't post JSON in one line ))

    0 讨论(0)
提交回复
热议问题