Я пытаюсь установить связь Many-to-Many
между двумя объектами:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.perso.ez.debate.tag.TagEntity;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
@Entity
@Indexed
@Table(name = "data")
public class DataLightEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Field
@Column(name = "title")
private String title;
@Field
@Column(name = "subtitle")
private String subtitle;
@Field
@Column(name = "text", columnDefinition = "TEXT")
private String text;
@IndexedEmbedded
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "data_tags", joinColumns = @JoinColumn(name = "data_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id"))
private List<TagEntity> tags;
@Column(name = "icon")
private String icon;
@Column(name = "date")
private LocalDateTime date = LocalDateTime.now();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public LocalDateTime getDate() {
return date;
}
public void setDate(LocalDateTime date) {
this.date = date;
}
public List<TagEntity> getTags() {
return tags;
}
public void setTags(List<TagEntity> tags) {
this.tags = tags;
}
@Override
public String toString() {
return "DataLightEntity{" +
"id=" + id +
", title='" + title + '\'' +
", subtitle='" + subtitle + '\'' +
", text='" + text + '\'' +
", tags=" + tags +
", icon='" + icon + '\'' +
", date=" + date +
'}';
}
}
package com.perso.ez.debate.tag;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.perso.ez.debate.data.DataLightEntity;
import com.perso.ez.debate.tag.type.TagTypeEntity;
import org.hibernate.search.annotations.Field;
import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "tag")
public class TagEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Field
@Column(name = "tag")
private String tag;
@ManyToOne
@JoinColumn(name = "type_id", nullable = false)
private TagTypeEntity type;
@ManyToMany(mappedBy = "tags")
@JsonIgnore
private List<DataLightEntity> datas = new ArrayList<>();
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Long getId() {
return id;
}
public TagTypeEntity getType() {
return type;
}
public void setType(TagTypeEntity type) {
this.type = type;
}
public List<DataLightEntity> getDatas() {
return datas;
}
public void setDatas(List<DataLightEntity> datas) {
this.datas = datas;
}
}
Я использую аннотации @Field
и @IndexedEmbedded
для своих функций исследования и @JsonIgnore
чтобы избежать бесконечного цикла.
Но я все еще получаю эту ошибку, когда пытаюсь провести исследование:
Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.perso.ez.debate.data.DataLightEntity.tags, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.perso.ez.debate.data.DataLightEntity.tags, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.perso.ez.debate.data.DataLightEntity["tags"])]
Я не знаю, что делать. Я пробовал много вещей, но у меня нет идей. Если у кого-то есть идея, это мне очень поможет. Спасибо!