Spring REST中未显示ManyToMany关系条目

时间:2019-02-23 00:24:59

标签: java rest spring-boot spring-data

我有两个班级:MenuItem.javaTag.java。我正在使用JPA的Hibernate实现,并使用PagingAndSortingRepository。创建一堆虚拟MenuItems和Tag之后,它们将显示在我的本地数据库中,并且可以通过以下方式访问:localhost:8080 / api / menuItems或localhost:8080 / api / tags。另外,如以下文件中的注释所描述,这两个对象之间存在ManyToMany关系,并且还在我的数据库中创建了具有适当条目的Menu_Items_Tags表。

我遇到的问题是,每当我尝试通过以下方式访问给定菜单项的标签列表时:localhost:8080 / api / menuItems / 1 / tags,例如,我收到一个500 error并另外有这样的重复错误代码:

: HHH000100: Fail-safe cleanup (collections) : org.hibernate.engine.loading.internal.CollectionLoadContext@726b0462<rs=HikariProxyResultSet@1877641821 wrapping Result set representing update count of 6>

尝试请求MenuItem的标签时我做错了吗?还是我错误地设置了@ManyToMany关系?

MenuItem.java:

@Data
@Entity
public class MenuItem implements Serializable {

private static final long serialVersionUID = 1132661429342356177L;

public MenuItem() {
}

public MenuItem(String name, Double price, Integer inventory, String description, Set<Tag> tags) {
    this.name = name;
    this.price = price;
    this.inventory = inventory;
    this.description = description;
    this.tags = tags;
}

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

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

@Column(name = "price", nullable = false)
private Double price;

@Column(name = "inventory", nullable = false)
private Integer inventory;

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

@OneToMany(mappedBy = "menuItem")
private Set<Order> orders;

@ManyToMany
@JoinTable(
        name="menu_item_tags",
        joinColumns = @JoinColumn(name = "menu_item_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id"))
@JsonManagedReference
private Set<Tag> tags;

}

Tag.java:

@Data
@Entity
public class Tag implements Serializable {

private static final long serialVersionUID = 1132661429342356176L;

public Tag() {
}

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

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

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

//    @JsonIgnore
@ManyToMany(mappedBy = "tags")
@JsonBackReference
private Set<MenuItem> menuItems;
}

0 个答案:

没有答案