Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data-rest integration test fails with simple json request

My spring-data-rest integration test fails for a simple json request. Consider the below jpa models

Order.java

public class Order {

    @Id @GeneratedValue//
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)//
    private Person creator;
    private String type;

    public Order(Person creator) {
        this.creator = creator;
    }

    // getters and setters
}

Person.java

ic class Person {

    @Id @GeneratedValue private Long id;

    @Description("A person's first name") //
    private String firstName;

    @Description("A person's last name") //
    private String lastName;

    @Description("A person's siblings") //
    @ManyToMany //
    private List<Person> siblings = new ArrayList<Person>();

    @ManyToOne //
    private Person father;

    @Description("Timestamp this person object was created") //
    private Date created;

    @JsonIgnore //
    private int age;

    private int height, weight;
    private Gender gender;

    // ... getters and setters
}

In my test I created a person by using personRepository and inited order by passing person

Person creator = new Person();
creator.setFirstName("Joe");
creator.setLastName("Keith");
created.setCreated(new Date());
created.setAge("30");
creator = personRepository.save(creator);

Order order = new Order(creator);
String orderJson = new ObjectMapper().writeValueAsString(order);

mockMvc.perform(post("/orders").content(orderJson).andDoPrint());

Order is created but creator is not associated with the order. Also I want to pass request body as a json object. In this my json object should contain creator as follows

{
"type": "1",
"creator": {
    "id": 1,
    "firstName": "Joe",
    "lastName": "Keith",
    "age": 30
}
}

If I send request body with the following json, the call works fine

{
"type": "1",
"creator": "http://localhost/people/1"
}

But I don't want to send the second json. Any idea how to solve the issue. Because already my client is consuming the server response by sending first json. Now I migrated my server to use spring-data-rest. After that all my client code is not working.

How to solve this?

like image 336
Achaius Avatar asked Dec 06 '16 07:12

Achaius


2 Answers

You are correctly associating order with the creator, however the Person is not associated with the orders. You are missing the List<Order> orders field in Person class. Add this, add annotations, add methods for adding order to person and then before sending JSON you should call something like this:

creator.addOrder(order);
order.setCreator(cretr);
like image 74
TomOw Avatar answered Nov 10 '22 05:11

TomOw


Did you try using cascade = CascadeType.ALL in @ManyToOne annotation

public class Order {
    @Id @GeneratedValue//
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)//
    private Person creator;
    private String type;

    public Order(Person creator) {
        this.creator = creator;
    }

    // getters and setters
}
like image 7
so-random-dude Avatar answered Nov 10 '22 06:11

so-random-dude