@Entity with @Embedded List<T> causes it to not render propertly

This test case exposes a glitch in Spring Data REST, probably because it confused when handling more than one item when rendering a link.

In the test case, change LineItem inside Order to List<LineItem> to simulate multiple entries, and the the line items themselves won't render
the target property, probably because the target property needs to move from top level _links to an embedded link for each line Item
This commit is contained in:
Greg Turnquist
2014-07-18 14:12:26 -05:00
parent 3a583ba511
commit 893f2c8c41

View File

@@ -24,8 +24,11 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -117,11 +120,39 @@ public class DataRest262Tests {
String result = mapper.writeValueAsString(resource);
System.out.println(result);
assertThat(JsonPath.read(result, "$_links.self"), is(notNullValue()));
assertThat(JsonPath.read(result, "$_links.airport"), is(notNullValue()));
assertThat(JsonPath.read(result, "$_links.originOrDestinationAirport"), is(notNullValue()));
}
@Test
public void serializeCircularReference() throws IOException, InterruptedException {
Order order = new Order();
order.name = "Shopping basket #1";
order.id = 1L;
//relatedOrder.lineItems = new ArrayList<LineItem>();
LineItem lineItem = new LineItem();
lineItem.description = "iPhone case";
lineItem.target = order;
//relatedOrder.lineItems.add(lineItem);
order.lineItem = lineItem;
JpaPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(Order.class);
Resource<Object> resource = PersistentEntityResource.build(order, persistentEntity).//
withLink(new Link("/api/relatedOrder/" + order.id)).//
build();
String result = mapper.writeValueAsString(resource);
System.out.println(result);
}
public interface AircraftMovementRepository extends CrudRepository<AircraftMovement, Long> {
}
@@ -130,6 +161,10 @@ public class DataRest262Tests {
}
public interface OrderRepository extends CrudRepository<Order, Long> {
}
@Entity(name = "aircraftmovement")
public static class AircraftMovement {
@@ -140,11 +175,29 @@ public class DataRest262Tests {
@Embeddable
public static class FlightPart {
@ManyToOne Airport airport;
}
@Entity(name = "airport")
public static class Airport {
@Id @GeneratedValue Long id;
}
@Entity(name = "system")
public static class Order {
@Id @GeneratedValue Long id;
String name;
// Change lineItem to List<LineItem>, and the whole field gets dropped
@Embedded LineItem lineItem;
}
@Embeddable
public static class LineItem {
@OneToOne Order target;
String description;
}
}