From d177cc7753dfd494372f2b74fd7333af6c930899 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Thu, 10 May 2012 07:06:28 -0500 Subject: [PATCH] Fixed a bug in saving entities via PUT. --- doc/configuring_path.md | 3 +- .../rest/webmvc/RepositoryRestController.java | 64 +++++++++---------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/doc/configuring_path.md b/doc/configuring_path.md index 7224397ff..81a86130d 100644 --- a/doc/configuring_path.md +++ b/doc/configuring_path.md @@ -100,7 +100,7 @@ This would result in a link value of: ### Hiding certain Repositories, query methods, or fields -You may not want a certain Repository, a query method on a Repository, or a field of your entity to be exported at all. To tell the exporter to not export your these items, annotate them with `@RestResource` and set `exported = false`. +You may not want a certain Repository, a query method on a Repository, or a field of your entity to be exported at all. To tell the exporter to not export these items, annotate them with `@RestResource` and set `exported = false`. For example, to skip exporting a Repository: @@ -121,7 +121,6 @@ To skip exporting a query method: Or to skip exporting a field: @Entity - @RestResource(exported = false) public class Person { @Id @GeneratedValue private Long id; @OneToMany diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index 479111335..4c00e43e3 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -516,44 +516,44 @@ public class RepositoryRestController .idAttribute() .type()); CrudRepository repo = repoMeta.repository(); - Object entity = null; Class domainType = repoMeta.entityMetadata().type(); - switch (request.getMethod()) { - case POST: - entity = domainType.newInstance(); - break; - case PUT: - entity = repo.findOne(serId); - break; - } - if (null == entity) { - model.addAttribute(STATUS, HttpStatus.NOT_FOUND); + final MediaType incomingMediaType = request.getHeaders().getContentType(); + final Object incoming = readIncoming(request, incomingMediaType, domainType); + if (null == incoming) { + throw new HttpMessageNotReadableException("Could not create an instance of " + domainType.getSimpleName() + " from input."); } else { - final MediaType incomingMediaType = request.getHeaders().getContentType(); - final Object incoming = readIncoming(request, incomingMediaType, domainType); - if (null == incoming) { - throw new HttpMessageNotReadableException("Could not create an instance of " + domainType.getSimpleName() + " from input."); + repoMeta.entityMetadata().idAttribute().set(serId, incoming); + if (request.getMethod() == HttpMethod.POST) { + if (null != applicationContext) { + applicationContext.publishEvent(new BeforeSaveEvent(incoming)); + } + Object savedEntity = repo.save(incoming); + if (null != applicationContext) { + applicationContext.publishEvent(new AfterSaveEvent(savedEntity)); + } + URI selfUri = buildUri(baseUri, repository, id); + HttpHeaders headers = new HttpHeaders(); + headers.set(LOCATION, selfUri.toString()); + model.addAttribute(HEADERS, headers); + model.addAttribute(STATUS, HttpStatus.CREATED); } else { - repoMeta.entityMetadata().idAttribute().set(serId, incoming); - if (request.getMethod() == HttpMethod.POST) { - if (null != applicationContext) { - applicationContext.publishEvent(new BeforeSaveEvent(incoming)); - } - Object savedEntity = repo.save(incoming); - if (null != applicationContext) { - applicationContext.publishEvent(new AfterSaveEvent(savedEntity)); - } - URI selfUri = buildUri(baseUri, repository, id); - HttpHeaders headers = new HttpHeaders(); - headers.set(LOCATION, selfUri.toString()); - model.addAttribute(HEADERS, headers); - model.addAttribute(STATUS, HttpStatus.CREATED); + Object entity = repo.findOne(serId); + if (null == entity) { + model.addAttribute(STATUS, HttpStatus.NOT_FOUND); } else { - if (null != applicationContext) { - applicationContext.publishEvent(new BeforeSaveEvent(incoming)); + for (AttributeMetadata attrMeta : (Collection) repoMeta.entityMetadata() + .embeddedAttributes() + .values()) { + Object incomingVal = attrMeta.get(incoming); + if (null != incomingVal) { + attrMeta.set(incomingVal, entity); + } } - Object savedEntity = repo.save(incoming); + if (null != applicationContext) { + applicationContext.publishEvent(new BeforeSaveEvent(entity)); + } + Object savedEntity = repo.save(entity); if (null != applicationContext) { applicationContext.publishEvent(new AfterSaveEvent(savedEntity)); }