打印一对多关系时出现问题
Problem while print OnetoMany Reletionship
我正在尝试打印从数据库中获取的对象,但每次它都会给我 WhosebugError:
SLF4J: Failed toString() invocation on an object of type [java.util.ArrayList]
Reported exception:
java.lang.WhosebugError
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)
....
即使我使用 System.out.println();
也一样
我删除了@Data 并添加了@JsonBackedReference,但没有用。
我在实体中的代码是:
@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 + '\'' +
'}';
}
}
和:
@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 +
'}';
}
错误发生在这里,当我试图获取代码时,(稍后我将使用DTO打印)
public List<Tracker> findAll(){
List<Tracker> trackerList= trackerRepo.findAll();
log.info("tracker: {}",trackerList);
return trackerList;
}
可以使用@Data,只需在Channel的tracker成员中添加@ToString.Exclude即可。关键是在调用 toString 时不要有任何循环路径。
我正在尝试打印从数据库中获取的对象,但每次它都会给我 WhosebugError:
SLF4J: Failed toString() invocation on an object of type [java.util.ArrayList]
Reported exception:
java.lang.WhosebugError
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)
....
即使我使用 System.out.println();
也一样我删除了@Data 并添加了@JsonBackedReference,但没有用。 我在实体中的代码是:
@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 + '\'' +
'}';
}
}
和:
@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 +
'}';
}
错误发生在这里,当我试图获取代码时,(稍后我将使用DTO打印)
public List<Tracker> findAll(){
List<Tracker> trackerList= trackerRepo.findAll();
log.info("tracker: {}",trackerList);
return trackerList;
}
可以使用@Data,只需在Channel的tracker成员中添加@ToString.Exclude即可。关键是在调用 toString 时不要有任何循环路径。