<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3128975055571333860</id><updated>2012-01-25T11:08:00.999+07:00</updated><category term='Java Language'/><title type='text'>Software development with blogger</title><subtitle type='html'>&lt;center&gt;&lt;b&gt;This blog's page is where collecting all good articles about technical of software development&lt;/b&gt;&lt;/center&gt;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ksoftvn.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ksoftvn.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Nguyễn Trung Khuyến</name><uri>http://www.blogger.com/profile/06342684561534336068</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3128975055571333860.post-8183870328469496476</id><published>2007-10-07T12:21:00.000+07:00</published><updated>2007-10-07T12:27:39.866+07:00</updated><title type='text'>Defining your object Model with JPA</title><content type='html'>&lt;img src="http://www.theserverside.com/tt/images/spacer.gif" height="1" width="6" /&gt;     &lt;img src="http://www.theserverside.com/tt/articles/content/JPAObjectModel/JPAObjectModel.gif" alt="Defining Your Object Model with JPA" align="middle" border="0" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;September 2007&lt;/b&gt;&lt;br /&gt;&lt;h2&gt;&lt;b&gt;&lt;a href="http://www.theserverside.com/news/thread.tss?thread_id=46926"&gt;Discussion&lt;/a&gt;&lt;/b&gt;&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;In the perfect world, your object model would map seamlessly to your database schema. Most organizations however, have database naming standards, requirements for how relationships are modeled and columns that all tables must have. &lt;/p&gt; &lt;p&gt;The following example will show how to use JPA in a real-world scenario. You’ve been asked to quickly mock up a simple blog application; here are your constraints: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;All database table names are uppercase, words separated by an underscore (“_”) &lt;/li&gt;&lt;li&gt;All table columns must start with the table name, or its initials &lt;/li&gt;&lt;li&gt;All mutable tables need to track who created and updated a row and when the insert and update occurred &lt;/li&gt;&lt;li&gt;Object relationships need to be managed intelligently &lt;/li&gt;&lt;li&gt;You should use as few trips as possible to the database to make sure we can handle current and future load &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The application should include the following objects: &lt;/p&gt; &lt;ul&gt;&lt;li&gt;User &lt;/li&gt;&lt;li&gt;Blog entry &lt;/li&gt;&lt;li&gt;Comments &lt;/li&gt;&lt;li&gt;The comments need to support nesting, so a user can reply to another’s comment, there is no limit to the nesting &lt;/li&gt;&lt;li&gt;You need to list all users who start comment threads, comments without responses, etc. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Here is one possible object model: &lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.theserverside.com/tt/articles/content/JPAObjectModel/clip_image002.jpg" height="317" width="570" /&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;strong&gt;Figure 1: Blog Object Model &lt;/strong&gt;&lt;/p&gt; &lt;p&gt; ModelBase is an abstract superclass of three entities: BlogPost, Comment, and User. It also defines fields for who created and modified each entity and when the changes occurred. The nested comment requirement is taken care of by Comment’s parent and child relationships. &lt;/p&gt; &lt;p&gt;The database schema for the object model is: &lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.theserverside.com/tt/articles/content/JPAObjectModel/clip_image004.jpg" height="281" width="576" /&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;strong&gt;Figure 2: Blog Entity Relationship Diagram &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The database tables can be seen here:&lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.theserverside.com/tt/articles/content/JPAObjectModel/tss-db.gif" /&gt;&lt;/p&gt; &lt;p&gt;The root object of the class hierarchy is ModeBase. The @Column annotation maps each field in ModelBase to a database table column. The problem is, ModelBase does not follow the database naming conventions listed above. This will be fixed with the concrete class definitions a little later. Here is ModelBase: &lt;/p&gt; &lt;pre&gt; 1 &lt;span style="color: rgb(0, 0, 255);"&gt;&lt;strong&gt;import javax.persistence.*;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2&lt;br /&gt;3 @&lt;span style="color: rgb(32, 64, 160);"&gt;MappedSuperclass&lt;/span&gt;&lt;br /&gt;4 @&lt;span style="color: rgb(32, 64, 160);"&gt;EntityListeners&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;ModelListener&lt;/span&gt;.&lt;strong&gt;class&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;5 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;abstract&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;6&lt;br /&gt;7     @&lt;span style="color: rgb(32, 64, 160);"&gt;Id&lt;/span&gt;&lt;br /&gt;8     @&lt;span style="color: rgb(32, 64, 160);"&gt;GeneratedValue&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;strategy&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;GenerationType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;IDENTITY&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;9     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"id"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;10     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Long&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;id&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;11&lt;br /&gt;12     @&lt;span style="color: rgb(32, 64, 160);"&gt;Version&lt;/span&gt;&lt;br /&gt;13     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"version"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;14     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Integer&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;version&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;15&lt;br /&gt;16     @&lt;span style="color: rgb(32, 64, 160);"&gt;ManyToOne&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;FetchType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;LAZY&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;ALL&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;17     @&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"created_by_user_id"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;18     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;createdByUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;19&lt;br /&gt;20     @&lt;span style="color: rgb(32, 64, 160);"&gt;Temporal&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;value&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;TemporalType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;TIMESTAMP&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;21     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"date_created"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;nullable&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;false&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;22     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;dateCreated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;23&lt;br /&gt;24     @&lt;span style="color: rgb(32, 64, 160);"&gt;ManyToOne&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;FetchType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;LAZY&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;25     @&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"updated_by_user_id"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;26     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;updatedByUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;27&lt;br /&gt;28     @&lt;span style="color: rgb(32, 64, 160);"&gt;Temporal&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;value&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;TemporalType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;TIMESTAMP&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;29     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"date_updated"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;nullable&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;false&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;30     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;dateUpdated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;31&lt;br /&gt;32     &lt;span style="color: rgb(68, 68, 68);"&gt;// methods removed for readability&lt;/span&gt;&lt;br /&gt;33 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt;  &lt;p&gt;ModelBase uses the @MappedSuperclass annotation to tell the JPA persistence provide that this object is not an entity but it’s fields will be included in each entities table (for the entities that subclass ModelBase). You can use a mapped superclass to define all common fields. In this case it defines a field for optimist locking, version, primary key, id and fields for date created and updated. The second annotation, @EntityListener defines a class to be called by the JPA persistence provider at various lifecycle events. ModelListener, sets the user who created and modified each entity and when it was created and updated. Here is ModelListener: &lt;/p&gt; &lt;pre&gt; 1 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelListener&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2&lt;br /&gt;3     @&lt;span style="color: rgb(32, 64, 160);"&gt;PrePersist&lt;/span&gt;&lt;br /&gt;4     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;setDatesAndUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;5&lt;br /&gt;6         &lt;span style="color: rgb(68, 68, 68);"&gt;// set createdBy and updatedBy User information&lt;/span&gt;&lt;br /&gt;7         &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;UserUtil&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getCurrentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;8&lt;br /&gt;9         &lt;span style="color: rgb(68, 68, 68);"&gt;// check to see if modelBase and currentUser are &lt;/span&gt;&lt;br /&gt;10         &lt;span style="color: rgb(68, 68, 68);"&gt;// the same, if so, make currentUser modelBase.&lt;/span&gt;&lt;br /&gt;11         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;equals&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;12             &lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;13         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;14&lt;br /&gt;15         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;16             &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getCreatedByUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;17                 &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setCreatedByUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;18             &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;19             &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setUpdatedByUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;20         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;21&lt;br /&gt;22         &lt;span style="color: rgb(68, 68, 68);"&gt;// set dateCreated and dateUpdated fields&lt;/span&gt;&lt;br /&gt;23         &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;24         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getDateCreated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;25             &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setDateCreated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;26         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;27         &lt;span style="color: rgb(32, 64, 160);"&gt;modelBase&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setDateUpdated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;28     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;29 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt; ModelListener has only one method, setDatesAndUser(ModelBase modelBase). This method is tied to the pre-persist entity lifecycle event and will always be called just before an entity is persisted to the database. This gives us a convenient way to set when and by whom the object is created and updated. &lt;/p&gt; &lt;p&gt;JPA requires that entity listeners be stateless and therefore we need a way to establish who the current user of our system is so we can set the createdByUser and updatedByUser fields. The UserUtil class uses a ThreadLocal to store the current user and provides an easy way for clients to establish the “current” user of the system. &lt;/p&gt; &lt;pre&gt; 1 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;UserUtil&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2&lt;br /&gt;3     &lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ThreadLocal&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;br /&gt;4         &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ThreadLocal&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;5&lt;br /&gt;6     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;setCurrentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;7         &lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;8     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;9&lt;br /&gt;10     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;static&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;getCurrentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;11         &lt;strong&gt;return&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;currentUser&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;12     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;13 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;If you want to turn off any inherited entity listeners for a particular entity, you can use @ExcludeSuperclassListeners. This annotation does not have any elements, you add it to your entity like this: &lt;/p&gt; &lt;pre&gt;1 @&lt;span style="color: rgb(32, 64, 160);"&gt;ExcludeDefaultListeners&lt;/span&gt;&lt;br /&gt;2 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Serializable&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;Now, Comment will not be passed to ModelListener when it’s persisted. &lt;/p&gt; &lt;p&gt;There are a couple of drawbacks to using entity listeners, most notably the lack of any lifecycle methods for the listener itself, it must be stateless, and it must have a public no-arg constructor. If you can live with these restrictions entity listeners are a good way to hook into the JPA entity lifecycle. &lt;/p&gt; &lt;p&gt;JPA also supports callback methods, these are methods in your object model class itself and must have the following method signature: void methodName(). Use the entity lifecycle event annotations (e.g. @PrePersist or @PostPersist) to indicate which event the callback method participates in. Here is an example of a callback method (continuing with the example of Comment turning off it’s entity listeners): &lt;/p&gt; &lt;pre&gt; 1 @&lt;span style="color: rgb(32, 64, 160);"&gt;ExcludeDefaultListeners&lt;/span&gt;&lt;br /&gt;2 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Serializable&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3&lt;br /&gt;4     &lt;span style="color: rgb(68, 68, 68);"&gt;// code removed for readability &lt;/span&gt;&lt;br /&gt;5&lt;br /&gt;6     @&lt;span style="color: rgb(32, 64, 160);"&gt;PrePersist&lt;/span&gt;&lt;br /&gt;7     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;setDates&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;8         &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Date&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;9         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;getDateCreated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;10              &lt;span style="color: rgb(32, 64, 160);"&gt;setDateCreated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;11         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;12         &lt;span style="color: rgb(32, 64, 160);"&gt;setDateUpdated&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;now&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;13     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;14 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;By default, an entities table name is the same as the entity itself. In the case of BlogPost, the default table name would be BlogPost. To explicitly set the table name an entity is mapped to, use the @Table annotation. To change the column mappings defined in ModelBase, use @AttributeOverride. If you need to override more than one field, use the @AttributeOverrides (plural) annotation. Here is how you change the mappings: &lt;/p&gt; &lt;pre&gt;1 @&lt;span style="color: rgb(32, 64, 160);"&gt;Entity&lt;/span&gt;&lt;br /&gt;2 @&lt;span style="color: rgb(32, 64, 160);"&gt;Table&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"BLOG_POST"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3 @&lt;span style="color: rgb(32, 64, 160);"&gt;AttributeOverrides&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; @&lt;span style="color: rgb(32, 64, 160);"&gt;AttributeOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"id"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;column&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"BP_ID"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;4     @&lt;span style="color: rgb(32, 64, 160);"&gt;AttributeOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"version"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;@&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_VERSION"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;5     @&lt;span style="color: rgb(32, 64, 160);"&gt;AttributeOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"dateCreated"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;@&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_DATE_CREATED"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;6     @&lt;span style="color: rgb(32, 64, 160);"&gt;AttributeOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"dateUpdated"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;@&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_DATE_UPDATED"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;7     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;8 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;BlogPost&lt;/span&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Serializable&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;The two remaining fields to remap from ModelBase are not attributes but associations, so you need to use a different set of annotations, @AssociationOverrides (plural) and @AssociationOverride. Here is how you rename the createdByUser and updatedByUser foreign key columns in the BlogPost entity: &lt;/p&gt; &lt;pre&gt;1 @&lt;span style="color: rgb(32, 64, 160);"&gt;AssociationOverrides&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2     @&lt;span style="color: rgb(32, 64, 160);"&gt;AssociationOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"createdByUser"&lt;/span&gt;,&lt;br /&gt;3                &lt;span style="color: rgb(32, 64, 160);"&gt;joinColumns&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;@&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_CREATED_BY_USER_ID"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;4&lt;br /&gt;5     @&lt;span style="color: rgb(32, 64, 160);"&gt;AssociationOverride&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"updatedByUser"&lt;/span&gt;,&lt;br /&gt;6                &lt;span style="color: rgb(32, 64, 160);"&gt;joinColumns&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;@&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_UPDATED_BY_USER_ID"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;7 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;As you can see, the @AssociationOverride annotation is a little be more complex than @AttributeOverride because it has a nested annotation, @JoinColumn. &lt;/p&gt; &lt;p&gt;As you can see in Figure 1, there are three more fields to be defined in BlogPost. Here is their definition: &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;Lob&lt;/span&gt;&lt;br /&gt;2     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"BP_CONTENT"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;String&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;content&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;4&lt;br /&gt;5     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"BP_TITLE"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;6     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;String&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;title&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;7&lt;br /&gt;8     @&lt;span style="color: rgb(32, 64, 160);"&gt;Column&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"BP_EDIT_COUNT"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;9     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Integer&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;editCount&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;The @Lob annotation tells the persistence provider that content is bound to a large object type column. The @Column annotation defines the column name the content field is mapped to. Use the @Column annotation to map an entities field to a specific column name. &lt;/p&gt; &lt;p&gt;One last thing to look at is BlogPost’s relationship to Comment. Here is how the comments field is defined: &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;OneToMany&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;PERSIST&lt;/span&gt;,&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;MERGE&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;2             &lt;span style="color: rgb(32, 64, 160);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;FetchType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;LAZY&lt;/span&gt;)&lt;br /&gt;3     @&lt;span style="color: rgb(32, 64, 160);"&gt;Cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;org&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;hibernate&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;annotations&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;DELETE_ORPHAN&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;4     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comments&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ArrayList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;The OneToMany annotation tells the persistence provider this is an association and not an attribute. The cascade element tells the persistence provider to persist or merge any comments associated with this instance when blog post is persisted or merged. Unfortunately, JPA does not provide a cascade type for managing orphaned deletes. Fortunately, the Hibernate team has created a set of Hibernate extension annotations to make managing relationships (and every other aspect of an entity) easier. The delete orphan cascade annotation lets Hibernate know it can delete a row from the comment table if a comment is removed from the comments list. Without this annotation, you would have to manually delete the removed comment. &lt;/p&gt; &lt;p&gt;There are two ways for the persistence provider to map a one-to-many relationship in the database. One is to use a join table (a unidirectional relationship) and the other is to have a foreign key column in the many side of the relationship (a bidirectional relationship). Given the BlogPost to Comment relationship, if you just defined the blog post to comment relationship as shown above, JPA should expect the following tables in the database: &lt;/p&gt; &lt;p align="center"&gt;&lt;img src="http://www.theserverside.com/tt/articles/content/JPAObjectModel/clip_image006.jpg" height="161" width="393" /&gt;&lt;/p&gt; &lt;p align="center"&gt;&lt;strong&gt;Figure 3: Blog Post to Comment join table &lt;/strong&gt;&lt;/p&gt; &lt;p&gt;You eliminate the join table, BlogPost_Comment, by defining a bidirectional relationship. To tell JPA this is a bidirectional relationship, you need to add the mappedBy element to the @OneToMany annotation like this: &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;OneToMany&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;PERSIST&lt;/span&gt;,&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;MERGE&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;2             &lt;span style="color: rgb(32, 64, 160);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;FetchType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;LAZY&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;mappedBy&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3     @&lt;span style="color: rgb(32, 64, 160);"&gt;Cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;org&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;hibernate&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;annotations&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;DELETE_ORPHAN&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;4     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comments&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ArrayList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;The mappedBy element indicates the owning-side of the relationship, or the field in Comment that will hold a reference to BlogPost. Here is the comment side of the relationship: &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;ManyToOne&lt;/span&gt;&lt;br /&gt;2     @&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"COMMENT_BLOG_POST_ID"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;BlogPost&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;blogPost&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt;  &lt;p&gt;By defining the post to comments relationship this way we eliminate the need for a join table. &lt;/p&gt; &lt;p&gt;The Comment entity has the same attribute and association overrides as BlogPost (mapping to different column names of course), however it has an interesting relationship to itself. Comments support nested comments, so a comment can contain a list of responses to itself. To create this relationship, Comment needs two fields, one for the collection of comments and another to represent the parent comment. Here is how the fields are defined: &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;ManyToOne&lt;/span&gt;&lt;br /&gt;2     @&lt;span style="color: rgb(32, 64, 160);"&gt;JoinColumn&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"COMMENT_PARENT"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;4&lt;br /&gt;5     @&lt;span style="color: rgb(32, 64, 160);"&gt;OneToMany&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;mappedBy&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"parent"&lt;/span&gt;,&lt;br /&gt;6             &lt;span style="color: rgb(32, 64, 160);"&gt;cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;MERGE&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;PERSIST&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;7     @&lt;span style="color: rgb(32, 64, 160);"&gt;OrderBy&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"dateCreated DESC"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;8     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;children&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ArrayList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;These relationships are the same as any other one-to-many bidirectional relationship between BlogPost and Comment; the only difference is both sides of the relationship are in the same class. The parent field holds the primary key of the parent comment. &lt;/p&gt; &lt;p&gt;Now that you have the fields defined, you need to add methods to manage the relationship. Both sides of the relationship need to be set in order for the Comment entity to behave properly. To ensure the relationship is set up correctly the java bean property methods for children need to be slightly modified. Since the JPA annotations used in the Comment entity are field based, the JPA persistence provider does not use property accessor methods to set its state. This means we do not need the public void setChildren(List&lt;comment&gt; children) method for the persistence provider. In addition, by removing this method clients of our model cannot directly set the collection. Next is the public List&lt;comment&gt; getChildren() method; like setChildren() this method allows clients to directly modify a comments children. To fix this, make getChildren() return an immutable list, like this: &lt;/comment&gt;&lt;/comment&gt;&lt;/p&gt; &lt;pre&gt;1     &lt;strong&gt;public&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;getChildren&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2         &lt;strong&gt;return&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Collections&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;unmodifiableList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;children&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;3     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;The final step is to define methods to add and remove child comments. Here is the method for adding child comments: &lt;/p&gt; &lt;pre&gt; 1     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;addChildComment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3             &lt;strong&gt;throw&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;IllegalArgumentException&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"child comment is null!"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;4         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;5&lt;br /&gt;6         &lt;span style="color: rgb(68, 68, 68);"&gt;// check to see if comment has a parent&lt;/span&gt;&lt;br /&gt;7         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;8             &lt;span style="color: rgb(68, 68, 68);"&gt;// if there is a parent check to see if it's already &lt;/span&gt;&lt;br /&gt;9             &lt;span style="color: rgb(68, 68, 68);"&gt;// associated to this comment&lt;/span&gt;&lt;br /&gt;10             &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;equals&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;11                 &lt;span style="color: rgb(68, 68, 68);"&gt;// if this instance is already the parent, we can just return&lt;/span&gt;&lt;br /&gt;12                 &lt;strong&gt;return&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;13             &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;14             &lt;strong&gt;else&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;15                 &lt;span style="color: rgb(68, 68, 68);"&gt;// disconnect post from it's current relationship&lt;/span&gt;&lt;br /&gt;16                 &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;children&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;remove&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;17             &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;18         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;19&lt;br /&gt;20         &lt;span style="color: rgb(68, 68, 68);"&gt;// make this instance the new parent&lt;/span&gt;&lt;br /&gt;21         &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;22         &lt;span style="color: rgb(32, 64, 160);"&gt;children&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;add&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;23     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt; If you want to add a remove comment method, it might look like this: &lt;/p&gt; &lt;pre&gt; 1     &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;removeChildComment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3             &lt;strong&gt;throw&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;IllegalArgumentException&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"child comment is null!"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;4         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;5&lt;br /&gt;6         &lt;span style="color: rgb(68, 68, 68);"&gt;// make sure we are the parent before we break the relationship&lt;/span&gt;&lt;br /&gt;7         &lt;strong&gt;if&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;!&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;null&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&amp;amp;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;equals&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;8             &lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;null&lt;/strong&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;9             &lt;span style="color: rgb(32, 64, 160);"&gt;children&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;remove&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;10         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;11         &lt;strong&gt;else&lt;/strong&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;12             &lt;strong&gt;throw&lt;/strong&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;IllegalArgumentException&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;13                     &lt;span style="color: rgb(0, 128, 0);"&gt;"child comment not associated with this instance"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;14         &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;15     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt; Not only does this method remove a comment from its parents children collection, it makes sure the parent of comment is this instance. If comment were not associated with this instance, removing it from children would have no affect. However, setting its parent to null would leave a dangling comment in the database. &lt;/p&gt; &lt;p&gt; The last two methods to deal with are getParent() and setParent() . Here is their definition: &lt;/p&gt; &lt;pre&gt;1     &lt;strong&gt;public&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;getParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2         &lt;strong&gt;return&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;3     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;4&lt;br /&gt;5     &lt;strong&gt;private&lt;/strong&gt; &lt;strong&gt;void&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;setParent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;6         &lt;strong&gt;this&lt;/strong&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;parent&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;7     &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;The addChildComment() method uses setParent() , however; clients of the object model should not be able to change a comments parent. The easiest way to ensure this is to make setParent() private. getParent() is fine the way it is. The pattern used for Comments parent/child relationship can be applied to any bidirectional association in your object model. &lt;/p&gt; &lt;p&gt;Deciding where to put the relationship management methods in a bidirectional relationship is rather arbitrary since the purpose of the methods is to ensure the relationships are established correctly. With one-to-many bidirectional relationships, I tend to put the management methods on the “one” side. In the case of Comment this isn’t obvious, but in the BlogPost to Comment relationship I placed the relationship methods in BlogPost. This seems more natural; you add comments to a blog post, not the other way around. If you have a many-to-many relationship it really doesn’t matter which side has the methods so pick one and restrict the other side. &lt;/p&gt; &lt;p&gt;JPA provides methods for fetching entities by their primary key but it might be a good idea to provide some queries to fetch blog posts and comments by User, count the number of comments and blog posts a user has made, determine which comments do not have child comments, and find all root comments (those without a parent). &lt;/p&gt; &lt;p&gt;JPA provides three different types of queries, dynamic queries, static or named queries, and native queries. Dynamic and static queries use the Java Persistence Query Language and native queries use SQL. A dynamic query is one that is processed at runtime, meaning it is parsed and SQL generated every time it’s created. Static queries are processed when the persistence provider loads your object model. This means static queries are parsed once and reused every time you run the query. &lt;/p&gt; &lt;p&gt;To declare a named or static query, you use the @NamedQuery annotation. If you have more than one named query, you need to use the @NamedQueries annotation. Both annotations can be placed on an entity or mapped superclass and are declared at the class or type level. Query names are global, that is, they are not bound to any entity. As a result, you should use some sort of naming convention. One approach is to prefix every query name with the entity name it’s associated with. &lt;/p&gt; &lt;p&gt;Here are the named queries used in Comment and BlogPost: &lt;/p&gt; &lt;pre&gt; 1 @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQueries&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;2     &lt;span style="color: rgb(68, 68, 68);"&gt;// select comments that do not have parent comments&lt;/span&gt;&lt;br /&gt;3     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"comment.rootComments"&lt;/span&gt;,&lt;br /&gt;4             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT c FROM Comment c WHERE c.parent IS NULL"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;5&lt;br /&gt;6     &lt;span style="color: rgb(68, 68, 68);"&gt;// select comments made by a User&lt;/span&gt;&lt;br /&gt;7     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"comment.userComments"&lt;/span&gt;,&lt;br /&gt;8             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT c FROM Comment c WHERE c.createdByUser = :user"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;9&lt;br /&gt;10     &lt;span style="color: rgb(68, 68, 68);"&gt;// count the number of comments made by a user&lt;/span&gt;&lt;br /&gt;11     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"comment.userCount"&lt;/span&gt;,&lt;br /&gt;12             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT COUNT(c) FROM Comment c WHERE c.createdByUser = :user"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;13&lt;br /&gt;14     &lt;span style="color: rgb(68, 68, 68);"&gt;// select the comments a user made without responses&lt;/span&gt;&lt;br /&gt;15     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"comment.noChildren"&lt;/span&gt;,&lt;br /&gt;16             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT c FROM Comment c WHERE c.children IS EMPTY AND c.parent IS NULL AND c.createdByUser = ?1"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;17 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;18 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Serializable&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; ... &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;19&lt;br /&gt;20&lt;br /&gt;21 @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQueries&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;22     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost.createdByUser"&lt;/span&gt;,&lt;br /&gt;23             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT p FROM BlogPost p WHERE p.createdByUser = ?1"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;24&lt;br /&gt;25     &lt;span style="color: rgb(68, 68, 68);"&gt;// determine the number of posts a User has made&lt;/span&gt;&lt;br /&gt;26     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost.postCount"&lt;/span&gt;,&lt;br /&gt;27             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT COUNT(p) FROM BlogPost p WHERE p.createdByUser = ?1"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;28&lt;br /&gt;29     &lt;span style="color: rgb(68, 68, 68);"&gt;// fetch a blog post and eagerly fetch its comments&lt;/span&gt;&lt;br /&gt;30     @&lt;span style="color: rgb(32, 64, 160);"&gt;NamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;name&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost.createdByUserComments"&lt;/span&gt;,&lt;br /&gt;31             &lt;span style="color: rgb(32, 64, 160);"&gt;query&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(0, 128, 0);"&gt;"SELECT p FROM BlogPost p JOIN FETCH p.comments as c WHERE p.createdByUser = ?1"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;32 &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;33 &lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;class&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;BlogPost&lt;/span&gt; &lt;strong&gt;extends&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ModelBase&lt;/span&gt; &lt;strong&gt;implements&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;Serializable&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; ... &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;Each of the above queries uses either positional or named parameters; positional parameters use the “?1” syntax and named parameters use the “:name” syntax. Here is how you would create and execute a static query which uses named parameters: &lt;/p&gt; &lt;pre&gt;1 &lt;span style="color: rgb(68, 68, 68);"&gt;// assume entityManager exists&lt;/span&gt;&lt;br /&gt;2 &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;UserUtil&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getCurrentUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;3&lt;br /&gt;4 &lt;span style="color: rgb(68, 68, 68);"&gt;// Here is the definition of comment.userComments for reference&lt;/span&gt;&lt;br /&gt;5 &lt;span style="color: rgb(68, 68, 68);"&gt;// SELECT c FROM Comment c WHERE c.createdByUser = :user &lt;/span&gt;&lt;br /&gt;6&lt;br /&gt;7 &lt;span style="color: rgb(32, 64, 160);"&gt;Query&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;entityManager&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;createNamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"comment.userComments"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;8 &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setParameter&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"user"&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;9 &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;results&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getResultList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;Another query using positional parameters (note: the count function returns a Long): &lt;/p&gt; &lt;pre&gt;1 &lt;span style="color: rgb(68, 68, 68);"&gt;// assume entityManager exists&lt;/span&gt;&lt;br /&gt;2 &lt;span style="color: rgb(32, 64, 160);"&gt;User&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;UserUtil&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getCurrenstUser&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;3&lt;br /&gt;4 &lt;span style="color: rgb(68, 68, 68);"&gt;// Here is the definition of blogPost.postCount for reference&lt;/span&gt;&lt;br /&gt;5 &lt;span style="color: rgb(68, 68, 68);"&gt;// SELECT COUNT(p) FROM BlogPost p WHERE p.createdByUser = ?1&lt;/span&gt;&lt;br /&gt;6&lt;br /&gt;7 &lt;span style="color: rgb(32, 64, 160);"&gt;Query&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;entityManager&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;createNamedQuery&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost.postCount"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;8 &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;setParameter&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;1&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;user&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;br /&gt;9 &lt;span style="color: rgb(32, 64, 160);"&gt;Long&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;result&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Long&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;q&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;getSingleResult&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt; &lt;/pre&gt; &lt;p&gt;The JPQL has many built in functions and expressions to assist you in working with your object model. If you have not checked out JPQL before, you might be surprised by how powerful it is. &lt;/p&gt; &lt;p&gt;The last query to look at is blogPost.createdByUserComments. This query uses the fetch join operator to eagerly fetch a blog posts’ comments. The BlogPost to Comment association is defined as being LAZY (here is the relationship again): &lt;/p&gt; &lt;pre&gt;1     @&lt;span style="color: rgb(32, 64, 160);"&gt;OneToMany&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;PERSIST&lt;/span&gt;,&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;MERGE&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;,&lt;br /&gt;2             &lt;span style="color: rgb(32, 64, 160);"&gt;fetch&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;FetchType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;LAZY&lt;/span&gt;, &lt;span style="color: rgb(32, 64, 160);"&gt;mappedBy&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt;&lt;span style="color: rgb(0, 128, 0);"&gt;"blogPost"&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;3     @&lt;span style="color: rgb(32, 64, 160);"&gt;Cascade&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;org&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;hibernate&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;annotations&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;CascadeType&lt;/span&gt;.&lt;span style="color: rgb(32, 64, 160);"&gt;DELETE_ORPHAN&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;4     &lt;strong&gt;private&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;List&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;comments&lt;/span&gt; &lt;span style="color: rgb(68, 68, 255);"&gt;=&lt;/span&gt; &lt;strong&gt;new&lt;/strong&gt; &lt;span style="color: rgb(32, 64, 160);"&gt;ArrayList&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(32, 64, 160);"&gt;Comment&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: rgb(68, 68, 255);"&gt;;&lt;/span&gt;&lt;/pre&gt; &lt;p&gt;Making the relationship LAZY (by setting the fetch element of the @OneToMany annotation to FetchType.LAZY) enables your application to fetch a blog post, say when a user wants to edit the post, without also fetching all the comments. When you want to fetch the blog post and its comments, you can use the named query, blogPost.createdByUserComments. Sometimes you will want to make a relationship eager, by setting the fetch element of the @OneToMany annotation to FetchType.EAGER, so when you fetch entity “A” the persistence provider will also fetch entity “B”. Having one fetch to get back several objects is more efficient than multiple trips to the database but you will need to decide the correct semantics for each collection in your object model. &lt;/p&gt; &lt;p&gt;If you haven’t looked at JPA before, I hope this short example will encourage you to give it a try. Hibernate’s latest release has excellent JPA support and provides an extensive set of JPA extension annotations. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3128975055571333860-8183870328469496476?l=ksoftvn.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ksoftvn.blogspot.com/feeds/8183870328469496476/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3128975055571333860&amp;postID=8183870328469496476' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/8183870328469496476'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/8183870328469496476'/><link rel='alternate' type='text/html' href='http://ksoftvn.blogspot.com/2007/10/september-2007-discussion-in-perfect.html' title='Defining your object Model with JPA'/><author><name>Nguyễn Trung Khuyến</name><uri>http://www.blogger.com/profile/06342684561534336068</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3128975055571333860.post-3414875810678792175</id><published>2007-09-21T20:45:00.000+07:00</published><updated>2007-09-21T20:59:15.402+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Language'/><title type='text'>Struts 2 + Spring 2 + JPA + AJAX</title><content type='html'>&lt;h3&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://cwiki.apache.org/S2WIKI/struts-2-spring-2-jpa-ajax.html"&gt;Click here to view detail of the article...&lt;/a&gt;&lt;/span&gt;&lt;/h3&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3128975055571333860-3414875810678792175?l=ksoftvn.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ksoftvn.blogspot.com/feeds/3414875810678792175/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3128975055571333860&amp;postID=3414875810678792175' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/3414875810678792175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/3414875810678792175'/><link rel='alternate' type='text/html' href='http://ksoftvn.blogspot.com/2007/09/struts-2-spring-2-jpa-ajax.html' title='Struts 2 + Spring 2 + JPA + AJAX'/><author><name>Nguyễn Trung Khuyến</name><uri>http://www.blogger.com/profile/06342684561534336068</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3128975055571333860.post-1660501552357793050</id><published>2007-09-21T20:35:00.000+07:00</published><updated>2007-09-21T20:59:35.368+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Language'/><title type='text'>Struts 2.0 Introduction and Validations using Annotations</title><content type='html'>&lt;span style="color: rgb(255, 0, 0);font-size:130%;" &gt;&lt;span style="color: rgb(51, 204, 0);font-size:100%;" &gt;This link below is a good article to understand overall about Struts 2.0&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;h3&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://struts.javabeat.net/articles/2007/05/struts-2-0-introduction-annotation-validations/"&gt;Click here to view detail of the article...&lt;/a&gt;&lt;/span&gt;&lt;/h3&gt;&lt;br /&gt;&lt;span style="color: rgb(255, 0, 0);font-size:130%;" &gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3128975055571333860-1660501552357793050?l=ksoftvn.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ksoftvn.blogspot.com/feeds/1660501552357793050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3128975055571333860&amp;postID=1660501552357793050' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/1660501552357793050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/1660501552357793050'/><link rel='alternate' type='text/html' href='http://ksoftvn.blogspot.com/2007/09/struts-20.html' title='Struts 2.0 Introduction and Validations using Annotations'/><author><name>Nguyễn Trung Khuyến</name><uri>http://www.blogger.com/profile/06342684561534336068</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3128975055571333860.post-568130985011356971</id><published>2007-09-21T19:49:00.002+07:00</published><updated>2007-09-21T20:57:52.717+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Language'/><title type='text'>Java5 Language New Features</title><content type='html'>&lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html"&gt;View detail Generics&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety.  It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=14"&gt;JSR 14&lt;/a&gt;.  &lt;a name="forloop"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html"&gt;View detail Enhanced for Loop&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays. Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=201"&gt; JSR 201 &lt;/a&gt;.  &lt;a name="boxing"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html"&gt;View detail Autoboxing/Unboxing&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This facility eliminates the drudgery of manual conversion between primitive types (such as int) and wrapper types (such as Integer). Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=201"&gt;  JSR 201 &lt;/a&gt;.  &lt;a name="enums"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html"&gt;View Detail Typesafe Enums&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This flexible object-oriented enumerated type facility allows you to create enumerated types with arbitrary methods and fields.  It provides all the benefits of the Typesafe Enum pattern ("Effective Java," Item 21) without the verbosity and the error-proneness. Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=201"&gt;JSR 201&lt;/a&gt;.  &lt;a name="varargs"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html"&gt;View detail Varargs&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists. Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=201"&gt;JSR 201&lt;/a&gt;.  &lt;a name="static_import"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html"&gt;View Detail Static Import&lt;/a&gt;&lt;/span&gt; &lt;/h3&gt; This facility lets you avoid qualifying static members with class names without the shortcomings of the "Constant Interface antipattern." Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=201"&gt;JSR 201&lt;/a&gt;.  &lt;a name="annotations"&gt;&lt;/a&gt; &lt;h3&gt; &lt;span style="font-size:100%;"&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html"&gt;View detail Metadata&lt;/a&gt;&lt;/span&gt; (Annotations)&lt;/span&gt; &lt;/h3&gt; This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it.  Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files.  Instead the information can be maintained &lt;i&gt;in&lt;/i&gt; the source file. Refer to &lt;a href="http://jcp.org/en/jsr/detail?id=175"&gt;JSR 175&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3128975055571333860-568130985011356971?l=ksoftvn.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ksoftvn.blogspot.com/feeds/568130985011356971/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3128975055571333860&amp;postID=568130985011356971' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/568130985011356971'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3128975055571333860/posts/default/568130985011356971'/><link rel='alternate' type='text/html' href='http://ksoftvn.blogspot.com/2007/09/java5-language-features.html' title='Java5 Language New Features'/><author><name>Nguyễn Trung Khuyến</name><uri>http://www.blogger.com/profile/06342684561534336068</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
