From 851ae503010604ed667ea238db42f5283cbf4746 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Mon, 15 Oct 2012 07:42:02 -0400 Subject: [PATCH] Tweaked how the version is exposed. It isn't by default so I added a check for `@RestResource` on the version attribute. If a `path` is set, the version is exported under that name. --- .../data/rest/repository/jpa/JpaEntityMetadata.java | 11 ++++++++--- .../springframework/data/rest/test/webmvc/Person.java | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaEntityMetadata.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaEntityMetadata.java index e7b78455f..426bf5d81 100644 --- a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaEntityMetadata.java +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaEntityMetadata.java @@ -61,10 +61,15 @@ public class JpaEntityMetadata implements EntityMetadata { if(repositories.hasRepositoryFor(attrType)) { linkedAttributes.put(name, new JpaAttributeMetadata(entityType, attr)); } else { - if(!(attr instanceof SingularAttribute && ((SingularAttribute)attr).isId()) - && !(attr instanceof SingularAttribute && ((SingularAttribute)attr).isVersion())) { - embeddedAttributes.put(name, new JpaAttributeMetadata(entityType, attr)); + if((attr instanceof SingularAttribute && ((SingularAttribute)attr).isId())) { + // Don't export the id attribute + continue; + } else if(((attr instanceof SingularAttribute) && ((SingularAttribute)attr).isVersion()) + && (null == fieldResourceAnno || !StringUtils.hasText(fieldResourceAnno.path()))) { + // Don't export the version attribute + continue; } + embeddedAttributes.put(name, new JpaAttributeMetadata(entityType, attr)); } } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java index d332a1640..c5c2fe4a3 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java @@ -14,6 +14,7 @@ import javax.persistence.PrePersist; import javax.persistence.Version; import org.codehaus.jackson.annotate.JsonManagedReference; +import org.springframework.data.rest.repository.annotation.RestResource; /** * @author Jon Brisbin @@ -23,6 +24,7 @@ public class Person { @Id @GeneratedValue private Long id; private String name; + @RestResource(path = "version") @Version private Long version; @JsonManagedReference