I have a pretty simple relationship where I have a Person
entity with a @OneToOne mapping to a PersonAttributes
entity.
I have a PersonRepository
as such:
@Transactional(propagation = Propagation.REQUIRED)
public interface PersonRepository extends JpaRepository<Person, BigDecimal>, JpaSpecificationExecutor<Person> {
}
If I write a test against the repository where I update both the Person and the related PersonAttributes entity everything gets saved fine.
If I write the same test "putting" the updates to "/api/persons/{theId}" only the Person entity gets updated, the PersonAttributes entity does not.
I've done the same thing through the actual client application ( with AngularJS ) and still only the Person gets updated.
This test works:
@Test
public void testUpdatePersonData() throws Exception {
Person person = personRepository.findOne(new BigDecimal(411));
assertNotNull( person );
assertFalse(person.getPersonData().getLastName().equalsIgnoreCase("ZZZ"));
person.setFormerSsn("dummySSN");
person.getPersonData().setLastName("ZZZ");
personRepository.saveAndFlush(person);
Person updatedPerson = personRepository.findOne(new BigDecimal(411));
assertEquals( "dummySSN", updatedPerson.getFormerSsn() );
assertEquals( "ZZZ", updatedPerson.getPersonData().getLastName() );
}
This test does not work:
@Test
public void testUpdatePersonData() throws Exception {
ObjectMapper mapper = new ObjectMapper();
MvcResult result = mockMvc.perform(get("/api/persons/411")).andReturn();
MockHttpServletResponse response = result.getResponse();
byte [] content = response.getContentAsByteArray();
// Convert from JSON to object
Person person = mapper.readValue(content, Person.class);
assertNotNull( person );
assertFalse(person.getPersonData().getLastName().equalsIgnoreCase("ZZZ"));
person.setFormerSsn("dummySSN");
person.getPersonData().setLastName("ZZZ");
String json = mapper.writeValueAsString(person);
result = mockMvc.perform(put( "/api/persons/411")
.content(json).contentType(MediaType.APPLICATION_JSON)).andReturn();
assertNotNull(result);
assertEquals( 204, result.getResponse().getStatus() );
personRepository.flush();
result = mockMvc.perform(get("/api/persons/411")).andReturn();
response = result.getResponse();
content = response.getContentAsByteArray();
Person updatedPerson = mapper.readValue(content, Person.class);
assertNotNull( updatedPerson );
assertEquals( "812818181", updatedPerson.getFormerSsn() );
// This seems to be a bug
// Refer to: http://stackoverflow.com/questions/24699480/spring-data-rest-onetoone-not-saving-the-relationship-updated-entity
// assertEquals( "ZZZ", updatedPerson.getPersonData().getLastName() );
}
Thoughts?
Thanks,
Cory.