I'm tryng to print the object that I get from the database but everytime it give me the StackOverFlowError:
SLF4J: Failed toString() invocation on an object of type [java.util.ArrayList]
Reported exception:
java.lang.StackOverflowError
at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:102)
at com.example.NetflixProve.model.Tracker.toString(Tracker.java:116)
at java.base/java.lang.String.valueOf(String.java:4215)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:169)
at com.example.NetflixProve.model.Channel.toString(Channel.java:33)
at java.base/java.lang.String.valueOf(String.java:4215)
at java.base/java.lang.StringBuilder.append(StringBuilder.java:169)
at java.base/java.util.AbstractCollection.toString(AbstractCollection.java:457)
at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:622)
at java.base/java.lang.String.valueOf(String.java:4215)
....
same even if I use System.out.println();
I deleted @Data and added @JsonBackedReference but it didn't work. My code in entities are:
@Entity
@Table(name= "TBL_CHANNEL")
@Getter
@Setter
public class Channel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long id;
@ManyToOne
@JoinColumn(name = "channels")
@JsonBackReference
private Tracker tracker;
@Column(name = "Channel_Name")
private String channelName;
@Override
public String toString() {
return "Channel{"
"id=" id
", tracker=" tracker
", channelName='" channelName '\''
'}';
}
}
and:
@Entity
@Table(name = "TBL_TRACKER")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tracker {
@Id
@Column(name = "ARCHIVE_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Generated(GenerationTime.ALWAYS)
private Long archiveId;
@Column(name = "NOTIFICATION_ID")
private String notificationId;
@Column(name = "NOTIFICATION_ID_INDEX")
private String notificationIdIndex;
@Column(name = "DELIVERY_CHANNEL")
@Enumerated(EnumType.STRING)
private DeliveryChannel deliveryChannel;
@Column(name = "SENDING_DATE")
@DateTimeFormat
private Date sendingDate;
@Column(name = "SERVICE_ID")
private String serviceId;
@Column(name = "IDENTIFIER_TYPE")
@Enumerated(EnumType.STRING)
private IdentifierType identifierType;
@Column(name = "IDENTIFIER")
private String identifier;//fiscalCode
@Column(name = "PAN")
private String pan;
@Column(name = "FLAG")
@Enumerated(EnumType.STRING)
private FlagEnum flag;
@Column(name = "NOTIFICATION_CATEGORY")
@Enumerated(EnumType.STRING)
private NotificationCategory notificationCategory;
@Column(name = "EXPIRING_DATE")
@DateTimeFormat
private Date expiringDate;
@Column(name = "CREATION_DATE")
@DateTimeFormat
@CreationTimestamp
private Date creationDate;
@Column(name = "VISUALIZATION_DATE")
@DateTimeFormat
private Date visualizationDate;
@Column(name = "EXPIRE_VISUALIZATION_DATE")
@DateTimeFormat
private Date expireVisualizationDate;
@Column(name = "portal")
private PortalEnum portalEnum;
@Column(name = "campaign_name")
private String campaignName;
@Column(name = "business_priority")
private int businessPriority;
@OneToMany(cascade = {CascadeType.ALL},mappedBy = "tracker")
@JsonManagedReference
private List<Channel> channelsId;
@Override
public String toString() {
return "Tracker{"
"archiveId=" archiveId
", notificationId='" notificationId '\''
", notificationIdIndex='" notificationIdIndex '\''
", deliveryChannel=" deliveryChannel
", sendingDate=" sendingDate
", serviceId='" serviceId '\''
", identifierType=" identifierType
", identifier='" identifier '\''
", pan='" pan '\''
", flag=" flag
", notificationCategory=" notificationCategory
", expiringDate=" expiringDate
", creationDate=" creationDate
", visualizationDate=" visualizationDate
", expireVisualizationDate=" expireVisualizationDate
", portalEnum=" portalEnum
", campaignName='" campaignName '\''
", businessPriority=" businessPriority
", channelsId=" channelsId
'}';
}
The error happen here, when I'm trying to get the code, (later I will use the DTO to print)
public List<Tracker> findAll(){
List<Tracker> trackerList= trackerRepo.findAll();
log.info("tracker: {}",trackerList);
return trackerList;
}
CodePudding user response:
You can use @Data, just add @ToString.Exclude to the tracker member of Channel. The point is to not have any circular paths while invoking toString.