My customer has, among other data, the address. The address consists of the following data:
Street and number ZIP code City Country
I create the class CustomerAddress, which looks like this:
@Embeddable
public class CustomerAddress {
@Column(name = "street")
private String street;
@Column(name = "zipCode")
private String zipCode;
@Column(name = "city")
private String city;
@Column(name = "country")
private String country;
public CustomerAddress(String street, String zipCode, String city, String country) {
this.street = street;
this.zipCode = zipCode;
this.city = city;
this.country = country;
}
public CustomerAddress() {
}
public String getStreet() { return street;
}
public void setStreet(String street) { this.street = street;
}
public String getZipCode() {return zipCode;
}
public void setZipCode(String zipCode) {this.zipCode = zipCode;
}
public String getCity() { return city;
}
public void setCity(String city) { this.city = city;
}
public String getCountry() { return country;
}
public void setCountry(String country) { this.country = country;
}
}
My Customer Repository class looks like this:
@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
long countById(Long id);
}
I am writing the test where my customer is being saved in the database:
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class CustomerRepositoryTests {
@Autowired
private CustomerRepository customerRepository;
@Test
public void testAddNewCustomer() {
Customer customer = new Customer();
customer.setUserLoginName("Endera_Hifhra");
customer.setUserEmail("[email protected]");
customer.setUserPassword("querty1234");
customer.setCustomerName("Endera");
customer.setCustomerSurname("Hifhra");
customer.setRole(Role.CUSTOMER);
LocalDate birthDate = LocalDate.of(1990, 9, 14);
customer.setCustomerBirthDate(Date.valueOf(birthDate).toLocalDate());
CustomerAddress address = new CustomerAddress();
address.setCity("Musterstadt");
address.setCountry("Musterland");
address.setStreet("Feuer Str. 21");
address.setZipCode("81546");
customer.setCustomerAddress(address);
Customer savedCustomer = customerRepository.save(customer);
Assertions.assertThat(savedCustomer.getId()).isNotNull();
}
}
Why are all the data except the address being written to the database?
I tryed to add Constructor, but it doesn't work.