How to send back to an AJAX post an object with foreign entities

193
April 30, 2018, at 10:35 PM

I am developing a simple blogging website in Sping MVC. Currently I am trying to implement a form that allows to post a comment via AJAX post to a Spring MVC controller. The problem is that the success function defined in jquery is not invoked when I add an entity that has foreign entities defined in its model to the JSON response sent to the AJAX call. In this case I have Comment entity that has two foreign entities: User and Post.

I need to send back to the AJAX call the Comment entity created because I would like to insert the created object to the DOM through jquery.

These are my Comment, Post and User entities:

@Entity
@Table(name = "COMMENTS")
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int commentid;
    @ManyToOne
    @JoinColumn(name="USERID")
    private User author;
    @ManyToOne
    @JoinColumn(name="POSTID")
    private Post post;
    @OneToMany(mappedBy="replyto")
    private List<Comment> replies;
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="REPLYTOID")
    private Comment replyto;
    @NotEmpty(message="* Enter message for this comment.")
    @Column(name = "BODY")
    private String body;
    @Column(name = "PUBDATE")
    private LocalDateTime pubdate;
    // constructors
    // getters and setters
}
@Entity
@Table(name = "POSTS")
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int postid;
    @NotEmpty(message="* Enter a title for this post.")
    @Column(name = "TITLE")
    private String title;
    @Column(name = "SMALLTITLE")
    private String smalltitle;
    @NotEmpty(message="* Enter your post.")
    @Column(name = "POST")
    private String post;
    @Column(name = "SMALLPOST")
    private String smallpost;
    @Column(name = "PUBDATE")
    private LocalDateTime pubdate;
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="BLOGID")
    private Blog ownerblog;
    @OneToMany(mappedBy="post")
    private List<Comment> comments;
    @ManyToMany
    @JoinTable(name = "POSTS_TAGS",
        joinColumns = @JoinColumn(name = "POSTID"),
        inverseJoinColumns = @JoinColumn(name = "TAGID")
    )
    private List<Tag> tagslist;
   // constructors
   // getters and setters
}
@Entity
@Table(name = "USERS", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"USERNAME"}),
        @UniqueConstraint(columnNames = {"EMAIL"})
})
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userid;
    @OneToOne(mappedBy="owner")
    private Blog blog;
    @NotEmpty(message="* Enter an original username.")
    @Column(name = "USERNAME")
    private String username;
    @Size(min = 6, message="* Enter a password with at least 6 characters.")
    @Column(name = "PASSWORD")
    private String password;
    @NotEmpty(message="* Enter a valid email address.")
    @Column(name = "EMAIL")
    private String email;
    @Column(name = "AVATAR")
    private String avatar;
    @NotEmpty(message="* Enter your first name.")
    @Column(name = "NAME")
    private String name;
    @NotEmpty(message="* Enter your last name.")
    @Column(name = "SURNAME")
    private String surname;
    @Column(name = "DESCRIPTION")
    private String description;
    @Column(name = "CREATION")
    private LocalDate creation;
    @Column(name = "ENABLED")
    private boolean enabled;
    @OneToMany(mappedBy="post")
    private List<Comment> comments;
    @Transient
    private BCryptPasswordEncoder passwordEncoder;
    // constructors
    // getters and setters
}

This is my AJAX function:

$(document).ready(function() {
    /*  Submit form using Ajax */
    $('input[name=commentBtn]').click(function(e) {
        //Prevent default submission of form
        e.preventDefault();
        //Remove all errors
        $('.error').remove();
        $('textarea[class=comment]').css('border', '1px solid #ddd');
        var path = $(this).attr("path");
        var postbody = $('textarea[class=comment]').val();
        $.ajax({
            type: 'POST',
            url : path,
            data : {
                body: postbody
            },
            success : function(res) {
                if(res.validated){
                    $('textarea[class=comment]').val('');
                }
                else{
                    $.each(res.errorMessages, function(key, value){
                        $('textarea[name='+key+']').after('<span class="error">'+value+'</span>');
                        $('textarea[name='+key+']').css('border', '1px solid red');
                    });
                }
            },
        });
    }); 
});

This is the controller that handle the AJAX POST:

@Controller
public class CommentController {
    @Autowired
    CommentRepository commentRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    PostRepository postRepository;
    @ResponseBody
    @RequestMapping(path     = { "/user/{userid}/blog/post/{postid}" }, 
                    method   = RequestMethod.POST, 
                    produces = { MediaType.APPLICATION_JSON_VALUE }
    )
    public CommentJsonResponse commentPost(HttpServletRequest request,
            @PathVariable int postid,
            @RequestParam("body") String body) {
        CommentJsonResponse resp = new CommentJsonResponse();
        System.out.println(body);
        if(body.length() < 1) {
            resp.setValidated(false);
            resp.setErrorMessage("body", "* Enter a body for your comment.");
        }
        else {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("userSession");
            Post post = postRepository.findByPostid(postid);
            Comment comment = new Comment(body);
            comment.setAuthor(user);
            comment.setPost(post);
            resp.setComment(comment);
            resp.setValidated(true);
            commentRepository.save(comment);
        }       
        return resp;
    } 
}

And finally this is the fragment of the JSP in which I have defined the form:

<c:set var = "path" value = "${pageContext.request.contextPath}/user/${post.ownerblog.owner.userid}/blog/post/${post.postid}"/>
<form method="POST" name="commentForm">
   <textarea class="comment" name="body"></textarea>
   <br><br>
   <input name="commentBtn" type="submit" value="Submit Comment" path='<c:out value = "${path}"/>'>
</form>
Rent Charter Buses Company
READ ALSO
How do I make an image move, but disappear in java?

How do I make an image move, but disappear in java?

just to preface, I am very new to javaSo expect dumb mistakes

208
What are the best benchmarking frameworks for parallel Java programs? [on hold]

What are the best benchmarking frameworks for parallel Java programs? [on hold]

I am particularly focusing on measuring speedup of a simple applicationIt will be very helpful if you can provide some references to good resources about benchmarking of parallel applications in general

183