O'Reilly logo

Harnessing Hibernate by James Elliott, Ryan Fowler, Timothy M. O'Brien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dealing with Associations

Because our example doesn’t deal with any associations, we avoided some complexity that goes into typical applications. As it turns out, calling merge() as the DAO objects do in their persist() methods actually persists whatever you send it. If the object you are persisting doesn’t also include all the objects associated with it, the previously persisted version gets overwritten and loses those associations. For example, if an Album had two comments, but was saved with an empty set of comments, the pre-existing comments would be lost.

Let’s implement an event handler and view for adding and editing Album comments. That way you’ll be able to see the problem we’re trying to solve before we actually go about solving it. First, we’ll add an editComments.jsp file with the contents of Example 14-18 that will provide a form to add comments to the Albums.

Example 14-18. A comment editor, webapp/albums/edit_comment.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%> <stripes:useActionBean beanclass="com.oreilly.hh.web.AlbumActionBean" var="act ionBean" event="edit" /> <h1>Add a comment for the album <span style="font-style: italic">${actionBean. album.title }</span></h1> <stripes:form action="/Album.action" > <stripes:hidden name="album.id" /> Comment: <stripes:text name="comment" /> <br /> <stripes:submit name="saveComment" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required