Ana*_*sra 2 spring spring-data spring-data-jpa spring-data-rest
StackoverflowError当我们在REST存储库上启用except投影时,我们正面临着.该实体是问题有两个社团,一个@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
}
Run Code Online (Sandbox Code Playgroud)
当我添加此投影
@Projection(name = "default", types = { Workshop.class })
public interface InlineVenueProjection {
String getName();
Integer getSeatsAvailable();
WorkshopType getWorkshopType();
Date getDate();
Venue getVenue();
}
Run Code Online (Sandbox Code Playgroud)
并在存储库中启用它
@RepositoryRestResource(collectionResourceRel = "workshop", path = "workshops", excerptProjection = InlineVenueProjection.class)
public interface WorkshopRepository extends JpaRepository<Workshop, Long> {
// omitted methods
}
Run Code Online (Sandbox Code Playgroud)
我在POST请求中遇到堆栈溢出错误
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
Run Code Online (Sandbox Code Playgroud)
并进一步在堆栈跟踪中
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)
Run Code Online (Sandbox Code Playgroud)
你得到堆栈溢出异常的原因是因为我猜在Venue(1-Many)和Workshop(Many-1)之间定义了双向关系.你可以确认我的假设.
当你试图直接序列化场地时,由于关系工作室被加载,它有一个参考场地,因此无休止的递归.
解
要解决这个问题,杰克逊已经@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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
场地类
public class Venue implements Serializable {
...
@OneToMany
@JsonBackReference
public List<Workshop> getWorkshops() {
return workshops;
}
...
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.让我知道这个是否奏效.
| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |