我是 Micronaut 的新手,我一直在按照文档设置一个简单的 REST 应用程序。我想我会从一个简单的多对多关系开始。一切似乎都在工作。数据库表在启动时正确生成,并且端点正在返回可分页项,但不是嵌套的 Offer 列表,即使我包含了一个 Eager fetch 注释。我错过了什么?:-( 我不知道为什么。
这就是我所拥有的...
物品控制器:
@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 数据时,我认为它会起作用。显然我错过了一些东西。
提前感谢您提供任何有用的信息。