I am building a "Campground" site using Hibernate and Spring MVC. I have a "Campground" page that shows the details of a campground and user submitted comments. There is an "edit" button that can be used to edit the details of a `Comment`. This button sends you to a form (Spring form) where you can change the information and then the changes submitted are saved to the object then saved to the database using Hibernate.

Here is the issue. Somethings in the Hibernate Entity `Comment.java` are Objects retrieved from other tables in the database and Hibernate handles retrieving those objects using its Hibernate Magic. The problem is that these fields in the Entity class come back `null` when retrieved from the Spring `Model` (after submission of the form). This makes updating the object using a form a challenge. Fields like the `author` of type `User`, the `Campground` that it is associated with are all null. Is there a way to instruct Spring to not null out these fields when the results of the form are sent back? I found a way to avoid the id for the comment to not be nulled out using ` but it throws an exception when this is attempted on objects.

One solution I did come up with is to just update the `author` and `campground` field on the `Comment` object in my `@PostMapping` after the form is submitted, but that requires retrieving those objects from the database which seems redundant if there is a way to just not null them out in the first place. It feels like a band-aid when I may be overlooking a legitimate solution.

Below is the `Entity` classes in question along with the form to edit the `Comment`, and my Contoller.

`Comment Entity class`

```
@Entity
@Table(name = "comments")
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

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

    @ManyToOne
    @JoinColumn(name = "author")
    private User author;

    @ManyToOne
    @JoinColumn(name = "campground")
    private Campground campground;

    public Comment() {
    }

    public Comment(String text, User author) {
        this.text = text;
        this.author = author;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    public Campground getCampground() {
        return campground;
    }

    public void setCampground(Campground campground) {
        this.campground = campground;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", text='" + text + '\'' +
                ", author=" + author +
                ", campground=" + campground +
                '}';
    }
}

```

`@RequestMapping` from `Controller class`

```
@GetMapping("/campgrounds/{campgroundId}/comments/{commentId}/edit")
public String editComment(@PathVariable Long commentId, @PathVariable Long campgroundId, Model model, HttpSession session) {

    User user = (User) session.getAttribute("user");
    Comment comment = commentDAO.getComment(commentId);
    if (!user.equals(comment.getAuthor())) {
        return "redirect:/campgrounds/" + campgroundId;
    }
    model.addAttribute("editComment", comment);
    return "editComment";
}

@PostMapping("/campgrounds/{campgroundId}/comments/{commentId}/edit")
public String editPostComment(@ModelAttribute("editComment") Comment comment,
                              @PathVariable Long campgroundId, @PathVariable Long commentId, HttpSession session) {

    User author = (User) session.getAttribute("user");
    comment.setAuthor(author); // because it comes back null
    Campground campground = campgroundDAO.getCampgound(campgroundId);
    comment.setCampground(campground); // because it comes back null

    commentDAO.saveComment(comment);
    return "redirect:/campgrounds/" + campgroundId;
}

```

Finally the `editComment.jsp`

```
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="partials/header.jsp"%>
<div class="container mx-auto my-3">

    <spring:form modelAttribute="editComment" action="${pageContext.request.contextPath}/campgrounds/${campgroundId}/comments/${commentId}/edit" method="post">
        <div class="card border-info">
            <h3 class="h3 text-center card-header badge-info">Edit Comment</h3>
            <div class="my-3">

                <spring:hidden path="id"/>

                <div class="form-group col-5 m-auto">
                    <label for="comment_text">Comment Text</label>
                    <spring:textarea id="comment_text" path="text" cssClass="form-control"/>
                </div>

                <input class="btn btn-primary form-control mt-3" type="submit" value="Submit">
            </div>
        </div>
    </spring:form>

</div>

<%@ include file="partials/footer.jsp"%>

```