gpt4 book ai didi

java - Spring反序列化带有额外列的manytomany表

转载 作者:行者123 更新时间:2023-12-04 17:15:17 24 4
gpt4 key购买 nike

我有一个映射到自定义表的多对多关系以添加额外的列。我可以反序列化它,但反序列化后它显示了连接表的元素,我只想连接元素。所以我目前得到的是以下内容:

{
"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 实现此行为,但我无法将其传输到自定义连接表。

提前致谢!

最佳答案

您似乎希望将一组数字返回给客户端。

您可以创建一个返回 id 数组的 getter,并通过注释将其他关系变为不可序列化。

您只需要其中一个 ID 的事实可能表明数据库中的映射不理想。

关于java - Spring反序列化带有额外列的manytomany表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68817275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com