1

I have following mapping:

1

public class Content {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "content_id", unique = true, nullable = false)
    private Long contentId;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "content", cascade = CascadeType.ALL)
    UserContent userContent;
     ....
}

@Entity
@Table(name="user_content")
public class UserContent {
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "content"))
    @Id
    @GeneratedValue(generator = "generator")
    @Column(name = "content_id", unique = true, nullable = false)
    private Long contentId;

    @OneToOne(fetch = FetchType.EAGER)
    @PrimaryKeyJoinColumn
    Content content;
}

and following dao:

 public Set<UserContent> getAllContent() {
        Session session = sessionFactory.getCurrentSession();
        return new HashSet<UserContent>(session.createCriteria(UserContent.class).list());
    }

when return result of this dao in controller method I see infinitie recursive json:

[{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":....

2

I tryed to change mapping like this:

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Content {
    ...
    @OneToOne(fetch = FetchType.LAZY, mappedBy = "content", cascade = CascadeType.ALL)
    @JsonIgnore        //added this annotation
    UserContent userContent;
    ....

result:

HTTP Status 500 - Could not write JSON: failed to lazily initialize a collection of role: com.terminal.domain.TerminalUser.companys, no session or session was closed (through reference chain: java.util.HashSet[0]->com.terminal.domain.UserContent["user"]->com.terminal.domain.TerminalUser["companys"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.terminal.domain.TerminalUser.companys, no session or session was closed (through reference chain: java.util.HashSet[0]->com.terminal.domain.UserContent["user"]->com.terminal.domain.TerminalUser["companys"])

P.S.

I have read followig popular answer but I cannot struggle my problem - please help.

Infinite Recursion with Jackson JSON and Hibernate JPA issue

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

2

Since Jackson 2.0 you can use @JsonIdentityInfo. Check documentation

Updated

...
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Content {
    ...
}

...
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class UserContent {
    ...
}
Pavel Parshin
  • 507
  • 4
  • 15
0

Update : About Infinite Recursion.

You have Bidirectional Relationship right ? imagine json converts your Content entity class then it has field of userContent and in UserContent Entity class it has a content field also then it will result infinite Recursion thats why you put @JsonIgnore in your UserContent Field to ignore that field to avoid infinite recursion. - Secondo

I bet you have private List<TerminalUser> companys; inside of your Content entity class.

I lost on my bet. XD :))

Based in this line.

HTTP Status 500 - Could not write JSON: failed to lazily initialize a collection of role: com.terminal.domain.TerminalUser.companys, 

just set the fetchType to Eager in private List<TerminalUser> companys; or just initialized it using Hibernate.initialized();

for more explanation refer to this link. lazyinitializationexception when session is not closed

Community
  • 1
  • 1
Secondo
  • 451
  • 4
  • 9
  • UserContent have TerminalUser field and terminalUser have List but I don't understand how does it related with JsonIgnore – gstackoverflow Dec 01 '14 at 08:14
  • thats why you are getting lazyinitialization exception you haven't initialize the list of company on your TerminalUser object btw. @jsonIgnore is use to ignore some field in your entity class this is also use to avoid `infinite recursive`. You got lazyinitialization exception because json tries to convert all of the related field on your object/entity class and json found out that List is not yet initialize. – Secondo Dec 01 '14 at 08:56
  • if you dont need the `List` just put @JsonIgnore also – Secondo Dec 01 '14 at 09:01
  • I just encounter same problem weeks ago and thats what i found out. – Secondo Dec 01 '14 at 09:10
  • Yes, know this problem but it appeared only after adding annotation @JsonIgnore – gstackoverflow Dec 01 '14 at 09:41
  • when you put `@JsonIgnore` in a certain field it means that when Json converts your object that field will be ignored and not include in convertion process. lazyinitialization exception appears because @jsonIgnore solves your first problem about infinite recursion thing. – Secondo Dec 01 '14 at 09:51
  • Hmm... looks like truth – gstackoverflow Dec 01 '14 at 09:59
  • You have Bidirectional Relationship right ? imagine json converts your Content entity class then it has field of `userContent` and in UserContent Entity class it has a `content` field also then it will result infinite Recursion thats why you put @JsonIgnore in your UserContent Field to ignore that field to avoid infinite recursion. – Secondo Dec 01 '14 at 09:59
  • so your current problem right now is to fix the lazyinitialization exception by changing its fetchtype to eager or use the Hibernate.initialize(yourCollection); in dao layer . – Secondo Dec 01 '14 at 10:01
  • I just added @JsonIgnore for this field) – gstackoverflow Dec 01 '14 at 10:07
  • whats the result let me know :)) – Secondo Dec 01 '14 at 10:09
  • If I was able to help you Im happy to receive my first Accepted answer :)) XD TIA – Secondo Dec 02 '14 at 00:43