当我们在REST存储库上启用except投影时,我们正面临StackoverflowError
。实体问题有两个关联,一个@ManyToOne
有一个Venue
实体,必须包含所有回复的内联,一个@OneToMany
带有Trainee
实体我们总是想要隐藏。实体(相关)片段
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Workshop implements Serializable {
private static final long serialVersionUID = -5516160437873476233L;
private Long id;
// omitted properties
private Venue venue;
private Set<Trainee> trainees;
@Id
@GeneratedValue
public Long getId() {
return id;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "workshop_venue", joinColumns = { @JoinColumn(name = "workshop_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "venue_id", referencedColumnName = "id") })
public Venue getVenue() {
return venue;
}
@OneToMany
@JoinTable(name = "workshop_trainees", joinColumns = { @JoinColumn(name = "workshiop_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "trainee_id", referencedColumnName = "email") })
@RestResource(exported = false)
@JsonIgnore
public Set<Trainee> getTrainees() {
return trainees;
}
// omitted getters/setters
}
当我添加此投影时
@Projection(name = "default", types = { Workshop.class })
public interface InlineVenueProjection {
String getName();
Integer getSeatsAvailable();
WorkshopType getWorkshopType();
Date getDate();
Venue getVenue();
}
并在存储库中启用它
@RepositoryRestResource(collectionResourceRel = "workshop", path = "workshops", excerptProjection = InlineVenueProjection.class)
public interface WorkshopRepository extends JpaRepository<Workshop, Long> {
// omitted methods
}
我在POST请求中收到堆栈溢出错误
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
并进一步在堆栈跟踪中
Caused by: java.lang.StackOverflowError: null
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync$ThreadLocalHoldCounter.initialValue(ReentrantReadWriteLock.java:286)
at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
at java.lang.ThreadLocal.get(ThreadLocal.java:170)
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryReleaseShared(ReentrantReadWriteLock.java:423)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.releaseShared(AbstractQueuedSynchronizer.java:1341)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.unlock(ReentrantReadWriteLock.java:881)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:169)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)
at org.springframework.data.mapping.context.PersistentEntities.getPersistentEntity(PersistentEntities.java:63)
at org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:100)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:352)
答案 0 :(得分:5)
您获得堆栈溢出异常的原因是因为我猜在class Reference<T> {
var value: T
init(_ value: T) { self.value = value }
}
(1-Many)和Venue
(Many-1)之间定义了双向关系。你可以证实我的假设。
当您尝试直接序列化场地时,由于关系工作室已加载,并且它具有对场地的引用,因此无限递归。
<强>解决方案强>
要解决此问题,杰克逊有Workshop
和@JsonManagedReference
。
工作坊课程
@JsonBackReference
地点等级
public class Workshop implements Serializable {
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "workshop_venue", joinColumns = { @JoinColumn(name = "workshop_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "venue_id", referencedColumnName = "id") })
@JsonManagedReference
public Venue getVenue() {
return venue;
}
...
}
希望这会有所帮助。如果有效,请告诉我。