Dragos Dumitrescu
Dragos Dumitrescu

Reputation: 101

Spring Data JPA with MVC repository

I am building a Spring repository for some JPA-annotated entities. I have created a repository:

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

The App class looks as follows:

@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
}

where the AgentUser and the AppFacet hold a reference property called app towards an App object. In the AgentUser class, I have changed the RestResource rel:

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

I am getting the following error message while querying the /apps path:

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

Do you know what could be causing it? Please note that I only have one App object in a database, for testing purposes and no other kind of object.

Update

The trace is:

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)

And after that, a lot of:

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

Upvotes: 0

Views: 372

Answers (1)

Dragos Dumitrescu
Dragos Dumitrescu

Reputation: 101

The problem resides in that, whenever you have links to some entities, you must implement a repository for that entity too, in order to generate the proper links.

Upvotes: 3

Related Questions