I have a manytomany relation mapped with a custom table to add an extra column. I can deserialize it, but deserialized it shows the elements of the join table, I would to just the joined element. So what I currently get is the following:
{
"id": 122,
"materials": [
{
"id": {
"materialId": 162,
"homeworkId": 122
},
"position": 1
},
{
"id": {
"materialId": 163,
"homeworkId": 122
},
"position": 1
}
]
}
I would rather get something like this:
{
"id": 122,
"materials": [
162, 163
]
}
I am using standard Spring mapper to deserialize:
@GetMapping("/api/homework/get")
public Homework getHomework(@RequestParam Long id) {
return homeworkService.findById(id).orElseThrow(HomeworkNotFoundException::new);
}
I have the following java classes (omitted unnecessary methods and functions)
Material
@Getter @Setter @NoArgsConstructor
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typeName")
@JsonIdentityInfo(scope = Material.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonSubTypes({
@JsonSubTypes.Type(value = FileMaterial.class, name = "fileMaterial"),
@JsonSubTypes.Type(value = LinkMaterial.class, name = "linkMaterial")
})
@Entity
@Table(name="material")
@Inheritance(strategy = InheritanceType.JOINED)
public class Material implements FileOwner {
private static final String sequenceName = "material_id_sequence";
@Id
@Column(nullable = false)
@SequenceGenerator(allocationSize=10, sequenceName=sequenceName, name=sequenceName)
@GeneratedValue(generator=sequenceName, strategy=GenerationType.SEQUENCE)
protected Long id;
@OneToMany(mappedBy = "material", cascade = CascadeType.ALL, orphanRemoval = true)
@Fetch(value = FetchMode.SUBSELECT)
@JsonManagedReference
List<HomeworkMaterial> homeworks = new ArrayList<>();
}
Homework
@Getter @Setter @NoArgsConstructor
@JsonIdentityInfo(scope = Homework.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity
@Table(name = "homework")
public class Homework {
private static final String sequenceName = "homework_id_sequence";
@Id
@Column(nullable = false)
@SequenceGenerator(allocationSize=10, sequenceName=sequenceName, name=sequenceName)
@GeneratedValue(generator=sequenceName, strategy= GenerationType.SEQUENCE)
protected Long id;
@OneToMany(mappedBy = "homework", cascade = CascadeType.ALL, orphanRemoval = true)
@Fetch(value = FetchMode.SUBSELECT)
@JsonManagedReference
List<HomeworkMaterial> materials = new ArrayList<>();
}
HomeworkMaterial
@Getter @Setter @NoArgsConstructor
@Entity
@Table(name = "homework_material")
public class HomeworkMaterial {
@EmbeddedId
private HomeworkMaterialId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("materialId")
@JsonBackReference
private Material material;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("homeworkId")
@JsonBackReference
private Homework homework;
@Column(nullable = false)
private int position;
public HomeworkMaterial(Material material, Homework homework, int position) {
this.material = material;
this.homework = homework;
this.position = position;
this.id = new HomeworkMaterialId(material.getId(), homework.getId());
}
}
HomeworkMaterialId
@Embeddable
@NoArgsConstructor @AllArgsConstructor @Getter @Setter
public class HomeworkMaterialId implements Serializable {
@Column(name = "material_id")
private long materialId;
@Column(name = "homework_id")
private long homeworkId;
}
For standard @manytomany mappings I was able to achieve this behaviour with @jsonignoreproperties but i can not transfer it to custom join-tables.
Thanks in advance!