Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (ForumAnswerReplay)

Forum Answer saving time facing issue.before i saved successfully using @JsonIgnore but now i removed @JsonIgnore annonation.and added @JsonBackReference and @JsonManagedReference.

Stack Trace

java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.List) not compatible with managed type (com.tta.abcd.model.ForumAnswerReplay)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase._resolveManagedReferenceProperty(BeanDeserializerBase.java:790)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:516)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:296)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:443)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:196)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:26)
at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:681)
at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:445)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:967)

Forum.java

    @Entity
    @Table(name="Forum")
    public class Forum {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="forumId")
    private Long forumId;

    @Column(name="question")
    private String question;

    @Column(columnDefinition="varchar(1000)",name="discription")
    private String discription;

    @Column(name="postedDate")
    private Date postedDate;

    @Fetch(value = FetchMode.SELECT)
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "forumId")
    @JsonIgnore
    private List<ForumAnswer> forumList;
    }

ForumAnswer.java

    @Entity
    @Table(name="ForumAnswer")
    public class ForumAnswer {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="forumAnswerId")
    private Long forumAnswerId;

    @ManyToOne
        @JoinColumn(name = "forumId",insertable=true, updatable=true,nullable=true)
    private Forum forum;

    @Column(name="answer")
    private String answer;

    @Column(name="answerDate")
    private Date answerDate;

    @Fetch(value = FetchMode.SELECT)
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "forumAnswerId")
    @JsonBackReference
    private List<ForumAnswerReplay> forumAnswerReplayList;

    @Transient
    private List<ForumAnswerReplay> faqReplayList;
}

ForumAnswerReplay.java

@Entity
@Table(name="ForumAnswerReplay")
public class ForumAnswerReplay {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="replayId")
    private Long replayId;

    @Column(name="replayToAnswer")
    private String replayToAnswer;

    @Column(name="replayToAnswerDate")
    private Date replayToAnswerDate;


    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="forumAnswerId",insertable=true,updatable=true,nullable=true)
        @JsonManagedReference
    private ForumAnswer forumAnswer;

    @Transient
    private String message;
}

Controller Code:

public ForumAnswer saveForumAns(@RequestBody ForumAnswer forumAns, BindingResult bindingResult, HttpSession session,
        HttpServletRequest request, HttpServletResponse response) {
    ForumAnswer forumDetails = forumService.saveForumAns(forumAns, bindingResult, session);
    if (forumDetails != null) {
        forumDetails.setMessage("success");
    }
    else {
        forumDetails.setMessage("failed");
    }
    return forumDetails;
}

DAO:

public ForumAnswer saveForumAns(ForumAnswer forumAns, BindingResult bindingResult) {

    final Session session = getSession();
    try {
        session.beginTransaction();
        Query query = session.createQuery("UPDATE ForumAnswer set answer =:answer,"
                + "forumAnswerId =:forumAnswerId,farmerUuid =:farmerUuid, answerDate =:answerDate");
        query.setParameter("answer", forumAns.getAnswer());
        query.setParameter("forumAnswerId", forumAns.getForumAnswerId());
        query.setParameter("farmerUuid", forumAns.getFarmer());
        query.setParameter("answerDate", forumAns.getAnswerDate());
        session.merge(forumAns);
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("error while saving forum details" + e);
    } finally {
        session.close();
    }

    return forumAns;
}
like image 222
Umapathi Avatar asked Jun 23 '17 05:06

Umapathi


1 Answers

Use @JsonBackReference for @OneToMany and @JsonManagedReference for @ManyToOne relation to allow Jackson to better handle the relation.

like image 91
shawon Avatar answered Oct 28 '22 09:10

shawon