0

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?

2 Answers 2

1

The easiest way to solve this is to use the @Embedded and @Embeddable annotations.


@Entity
public class Customer{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
@Embedded
private Address address;
}


@Entity
@Embeddable 
public class Address{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String street;
private String city;
private String state;
private String zipCode
}

This will solve your problem, @Embedded will embed Address into Customer Entity hence solve your problem.

Sign up to request clarification or add additional context in comments.

4 Comments

you might as well need to remove @Entity in Address Entity, this will work fine for you
The RESTController is still passing a null address object.
please contact me on my email [email protected] we might need to see the full code and address the issue
Thank you, but I realized it wasn't working because I didn't use dot notation in the query.
0

If you use ‘@Valid Customer customer’ and when you did ‘/customers?state=IL’no Adress instance will be created automatically.

Because your request wait for a body. Here if you want init a address you should put @param(´state’) String state and instanciate manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.