Spring 使用额外列反序列化 manytomany table
Spring Deserialize manytomany table with extra column
我有一个映射了自定义 table 的多对多关系,以添加一个额外的列。我可以反序列化它,但反序列化后它显示了连接 table 的元素,我只想连接元素。所以我目前得到的是:
{
"id": 122,
"materials": [
{
"id": {
"materialId": 162,
"homeworkId": 122
},
"position": 1
},
{
"id": {
"materialId": 163,
"homeworkId": 122
},
"position": 1
}
]
}
我宁愿得到这样的东西:
{
"id": 122,
"materials": [
162, 163
]
}
我正在使用标准 Spring 映射器反序列化:
@GetMapping("/api/homework/get")
public Homework getHomework(@RequestParam Long id) {
return homeworkService.findById(id).orElseThrow(HomeworkNotFoundException::new);
}
我有以下java类(省略不必要的方法和函数)
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<>();
}
作业
@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<>();
}
作业Material
@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());
}
}
作业Material身份证
@Embeddable
@NoArgsConstructor @AllArgsConstructor @Getter @Setter
public class HomeworkMaterialId implements Serializable {
@Column(name = "material_id")
private long materialId;
@Column(name = "homework_id")
private long homeworkId;
}
对于标准的@manytomany 映射,我能够使用@jsonignoreproperties 实现此行为,但我无法将其转移到自定义连接-tables。
提前致谢!
您似乎希望将一组数字返回给客户端。
您可以创建一个 getter returns id 数组,并通过注释将其他关系变为不可序列化。
您只需要其中一个 ID 的事实可能表明数据库中的映射不理想。
我有一个映射了自定义 table 的多对多关系,以添加一个额外的列。我可以反序列化它,但反序列化后它显示了连接 table 的元素,我只想连接元素。所以我目前得到的是:
{
"id": 122,
"materials": [
{
"id": {
"materialId": 162,
"homeworkId": 122
},
"position": 1
},
{
"id": {
"materialId": 163,
"homeworkId": 122
},
"position": 1
}
]
}
我宁愿得到这样的东西:
{
"id": 122,
"materials": [
162, 163
]
}
我正在使用标准 Spring 映射器反序列化:
@GetMapping("/api/homework/get")
public Homework getHomework(@RequestParam Long id) {
return homeworkService.findById(id).orElseThrow(HomeworkNotFoundException::new);
}
我有以下java类(省略不必要的方法和函数)
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<>();
}
作业
@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<>();
}
作业Material
@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());
}
}
作业Material身份证
@Embeddable
@NoArgsConstructor @AllArgsConstructor @Getter @Setter
public class HomeworkMaterialId implements Serializable {
@Column(name = "material_id")
private long materialId;
@Column(name = "homework_id")
private long homeworkId;
}
对于标准的@manytomany 映射,我能够使用@jsonignoreproperties 实现此行为,但我无法将其转移到自定义连接-tables。
提前致谢!
您似乎希望将一组数字返回给客户端。
您可以创建一个 getter returns id 数组,并通过注释将其他关系变为不可序列化。
您只需要其中一个 ID 的事实可能表明数据库中的映射不理想。