Spring Data JPA与MVC存储库

时间:2015-06-19 17:02:26

标签: spring spring-mvc jpa

我正在为一些注释JPA的实体构建一个Spring存储库。我创建了一个存储库:

public interface AppRepository extends PagingAndSortingRepository<App, String>
{
}

App类如下所示:

@Entity
public class App implements Serializable
{
    @Id
    private String appId;
    @OneToMany(mappedBy = "app")
    private List<AgentUser> agentusers;
    @OneToMany(mappedBy = "app")
    private List<AppFacet> appfacets;
    // getters and setters go here
}

其中AgentUser和AppFacet将一个名为app的引用属性保存到App对象。在AgentUser类中,我更改了RestResource rel:

@Entity
public class AgentUser
{
    ...
    @ManyToOne
    @JoinColumn(name = "AppId")
    @RestResource(rel = "agentUserToApp", exported = false)
    private App app;
    // other properties go here
}

我在查询/ apps路径时收到以下错误消息:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.hateoas.PagedResources["_embedded"]);

你知道是什么原因造成的吗?请注意,我在数据库中只有一个App对象,用于测试目的,而不是其他类型的对象。

更新

跟踪是:

com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:677)
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2240)
org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:231)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:161)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:101)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:167)

之后,很多:

org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:352)
org.springframework.data.rest.webmvc.mapping.LinkCollectingAssociationHandler.doWithAssociation(LinkCollectingAssociationHandler.java:101)

1 个答案:

答案 0 :(得分:3)

问题在于,只要有链接到某些实体,就必须为该实体实现存储库,以便生成正确的链接。