0

I'm playing with the Spring Data examples. I've defined an entity Parent that has a set of Child entities associated.

@Entity
@Table
@Data
public class Parent {

    @Id
    @GeneratedValue
    private Integer id;

    @NotNull
    private String name;

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(fetch = EAGER, cascade = {ALL}, orphanRemoval = true)
    private Set<Child> childs;

}

@Entity
@Table
@Data
public class Item {

    @Id
    @GeneratedValue
    private Integer id;

    @NotNull
    private String name;

}

And the corresponding repositories. My problem is that when posting with curl a new Parent with a Child, I run into the error Could not read JSON document: Failed to convert from type [java.net.URI] to type also described here. The answer to that question states that child entities need to be posted before, and then use the returned URL. That's the same process described in this answer by Oliver Gierke.

Is there any way of configuring Spring Data to deserialize full child entities?

Community
  • 1
  • 1
garci560
  • 2,993
  • 4
  • 25
  • 34

1 Answers1

1

If you do not export the repository for Item (by annotating the repository with @RepositoryRestResource(exported = false)) you will always get the child serialized and also you can provide it with a POST on the parent.

But also there will be no top-level REST API endpoint for Item anymore and also no association resource on the parent.

You already have cascade=ALL on the relationship - so this approach should work.

Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70