diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index fc3097dde..3d7827405 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -307,13 +307,10 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init PersistentProperty property = association.getInverse(); - if (!mappings.isMapped(property)) { - return; - } - if (maybeAddAssociationLink(builder, mappings, property, links)) { return; } + // Association Link was not added, probably because this isn't a managed type. Add value of property inline. Object propertyValue = wrapper.getProperty(property); try { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index 85b9ba5f0..ba89c70f3 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -104,6 +104,20 @@ public class JpaWebTests extends AbstractWebIntegrationTests { MockHttpServletResponse orders = request(ordersLink); Link creatorLink = assertHasContentLinkWithRel("creator", orders); + assertThat(request(creatorLink), is(notNullValue())); } + + /** + * @see DATAREST-200 + */ + @Test + public void exposesInlinedEntities() throws Exception { + + MockHttpServletResponse response = request("/"); + Link ordersLink = assertHasLinkWithRel("orders", response); + + MockHttpServletResponse orders = request(ordersLink); + assertHasJsonPathValue("$..lineItems", orders); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/LineItem.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/LineItem.java new file mode 100644 index 000000000..9377104ea --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/LineItem.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.jpa; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Oliver Gierke + */ +@Entity +public class LineItem { + + @Id @GeneratedValue// + private Long id; + private String name; + + public LineItem(String name) { + this.name = name; + } + + protected LineItem() { + + } + + public String getName() { + return name; + } +} diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java index 5c0bf7c6c..29f6125b0 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/Order.java @@ -15,11 +15,16 @@ */ package org.springframework.data.rest.webmvc.jpa; +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; import javax.persistence.Table; /** @@ -29,8 +34,12 @@ import javax.persistence.Table; @Table(name = "ORDERS") public class Order { - @Id @GeneratedValue private Long id; - @ManyToOne(fetch = FetchType.LAZY) private Person creator; + @Id @GeneratedValue// + private Long id; + @ManyToOne(fetch = FetchType.LAZY)// + private Person creator; + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)// + private Set lineItems = new HashSet(); public Order(Person creator) { this.creator = creator; @@ -47,4 +56,15 @@ public class Order { public Person getCreator() { return creator; } + + /** + * @return the lineItems + */ + public Set getLineItems() { + return lineItems; + } + + public void add(LineItem item) { + this.lineItems.add(item); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java index d7bba5053..bea692ccb 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/TestDataPopulator.java @@ -34,7 +34,9 @@ public class TestDataPopulator { Person person = people.findAll().iterator().next(); - orders.save(new Order(person)); + Order order = new Order(person); + order.add(new LineItem("Java Chip")); + orders.save(order); } private void populatePeople() {