我有一个非常简单的关系,我有一个Person
实体,其中@OneToOne映射到PersonAttributes
实体。
我有一个PersonRepository
:
@Transactional(propagation = Propagation.REQUIRED)
public interface PersonRepository extends JpaRepository<Person, BigDecimal>, JpaSpecificationExecutor<Person> {
}
如果我针对存储库编写测试,我更新了Person和相关的PersonAttributes实体,一切都得到了妥善保存。
如果我写相同的测试&#34;把&#34;对&#34; / api / persons / {theId}&#34;的更新只有Person实体得到更新,PersonAttributes实体才会更新。
我通过实际的客户端应用程序(使用AngularJS)做了同样的事情,但仍然只有Person更新。
此测试有效:
@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() );
}
此测试不起作用:
@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() );
}
思想?
谢谢,
科里。