gpt4 book ai didi

java - 多对多关系不返回嵌套 DTO 的 Micronaut 数据问题

转载 作者:行者123 更新时间:2023-12-05 06:18:00 27 4
gpt4 key购买 nike

我是 Micronaut 的新手,我一直在按照文档设置一个简单的 REST 应用程序。我想我会从一个简单的多对多关系开始。一切似乎都在运作。数据库表在启动时正确生成,并且端点返回可分页项目而不是嵌套的报价列表,即使我包含了一个急切的获取注释。我错过了什么? :-( 我不明白为什么。

这是我所拥有的...

项目 Controller :

@Get(value = "/{?args*}")
public Page<ItemDTO> list(@Valid Pageable args) {
return itemRepository.findAllOrderByName(Pageable.from(args.getNumber(), args.getSize(), args.getSort() ));
}

项目实体:

@Entity
@Table(name = "item")
public class Item {

public Item() {}

public Item(@NotNull String name) {
this.name = name;
}

public static ItemDTO toDTO(Item item) {
return new ItemDTO(item);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Version
private Long version;

@NotNull
@Column(name = "name", nullable = false, unique = true)
private String name;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "item_offer",
joinColumns = @JoinColumn(name = "item_id"),
inverseJoinColumns = @JoinColumn(name = "offer_id"))
private Set<Offer> offers = new HashSet<>();

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Set<Offer> getOffers() {
return offers;
}

public Item setOffers(Set<Offer> offers) {
this.offers = offers;
return this;
}

public Item addOffer(Offer offer) {
offers.add(offer);
offer.getItems().add(this);
return this;
}

}

项目 DTO:

@Introspected
public class ItemDTO {

@NotNull
private String name;

private Set<OfferDTO> offers = new HashSet<>();

public ItemDTO() {
}

public ItemDTO(String name) {
this.name = name;
}

public ItemDTO(Item item) {
this.name = item.getName();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

要约实体:

@Entity
@Table(name = "offer")
public class Offer {

public Offer() { }

public Offer(String name) {
this.name = name;
}

public static OfferDTO toDTO(Offer offer) {
return new OfferDTO(offer);
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Version
private Long version;

@Column(name = "name", nullable = false, unique = true)
private String name;

@ManyToMany(mappedBy = "offers", fetch = FetchType.EAGER)
private Set<Item> items = new HashSet<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getVersion() {
return version;
}

public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<Item> getItems() {
return items;
}

public void setItems(Set<Item> items) {
this.items = items;
}
}

提供 DTO:

@Introspected
public class OfferDTO {

@NotNull
private String name;

public OfferDTO() {
}

public OfferDTO(String name) {
this.name = name;
}

public OfferDTO(Offer offer) {
this.name = offer.getName();
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

元素库:

import io.micronaut.data.model.Page;
import io.micronaut.data.model.Pageable;
import io.micronaut.data.repository.PageableRepository;
import knitwizzs.domains.Item;
import knitwizzs.dtos.ItemDTO;

public interface ItemRepository extends PageableRepository<Item, Long> {

Page<ItemDTO> findAllOrderByName(Pageable pageable);

}

这是我得到的输出:

{
"content": [
{
"name": "Item name one"
},
{
"name": "Item name two"
}
],
"pageable": {
"number": 0,
"sort": {
"sorted": false
},
"size": 100,
"offset": 0,
"sorted": false
},
"totalSize": 2,
"totalPages": 1,
"empty": false,
"size": 100,
"offset": 0,
"pageNumber": 0,
"numberOfElements": 2
}

所以没有嵌套商品的迹象?

就是这样。当我使用 Micronaut 数据时,我认为它会起作用。显然我错过了一些东西。

提前感谢您提供任何有用的信息。

最佳答案

@JsonIgnore 注释将从 JSON 输出中排除该字段,因为它在序列化期间被有效忽略。

因此您只需更改 ItemDTO 类中的字段:

@JsonIgnore
private Set<OfferDTO> offers = new HashSet<>();

到:

private Set<OfferDTO> offers = new HashSet<>();

关于java - 多对多关系不返回嵌套 DTO 的 Micronaut 数据问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61424706/

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