Spring Data Serialization of Embeddable with ManyToOne References
Asked Answered
G

1

8

I have an interesting problem. My data-model is the following:

Type A:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class A {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}

Type B:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class B {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}

Embeddable C:

@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class C {
  @ManyToOne
  private A a;
  @ManyToOne
  private B b;
}

And Type D:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class D {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @ElementCollection
  @OrderColumn(name = "ORDER_INDEX")
  @CollectionTable(
    name = "d_c_join",
    joinColumns = @JoinColumn(name = "d_id")
  )
  private List<C> listOfC;
}

Deserialization (and storing) the entities works fine. When an object of class D is serialized the following is the outcome:

{
  "_embedded" : {
    "ds" : [ {
      "id" : 1,
      "listOfC" : [ { }, { } ],
      "_links" : {
        "self" : {
          "href" : "http://localhost:8000/ds/1"
        }
      }
    } ]
  }
}

How can I configure Spring-Data to serialize A and B in C (best would be by their URI).

Granulite answered 13/10, 2015 at 16:9 Comment(3)
i think you just mark the fields on C as eagerload, right?Fructification
no spring-data will look on fields and references of an entity not depending on it is being eager or lazy loadedGranulite
I'm having a similar issue. Relationships in @Embeddable objects not being serialized. Did you ever manage to resolve this issue?Repose
R
4

I'm pretty positive what you're looking for are Projections. I don't think Spring will serialize referenced collections without it.

See my answer here for more details: Spring Data-Rest POST to sub-resource

Release answered 19/10, 2015 at 23:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.