Here, we are using the following classes, Customer and Address. Assume getters and setters are implemented, and the constructors fill all fields except for the automatically filled id:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
@OneToOne
private Address address;
/** ... */
}
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String street;
private String city;
private String state;
private String zipCode;
/** ... */
}
Notice how the field state is nested inside of the Address instance object inside of the Customer class.
Now our project implements the JpaRepository<Customer, Long> interface in order to handle all queries, and said interface includes a findAll function that takes an Example<Customer> and searches the database according to that example. So we set up our handler
@GetMapping
public ResponseEntity<List<Customer>> getCustomers(@Valid Customer customer) {
/** call to customerRepository.findAll(Example.of(customer)) inside here */
}
Now in our RESTful application, we type in the URL /customers?state=IL, expecting the customer argument to be fed
new Customer(null, null, new Address(null, null, "IL", null))
but instead, it is fed
new Customer(null, null, null)
Why is that, and what do we have to do to make sure the first object is passed instead of the second?