<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>NHibernate</title>
        <link>http://bchavez.bitarmory.com/category/4.aspx</link>
        <description>Posts related to NHibernate</description>
        <language>en-US</language>
        <copyright>Brian Chavez</copyright>
        <managingEditor>bchavez@bitarmory.com</managingEditor>
        <generator>Subtext Version 1.9.5.176</generator>
        <item>
            <title>NHibernate 2.0 GA released!</title>
            <link>http://bchavez.bitarmory.com/archive/2008/08/23/nhibernate-2.0-ga-released.aspx</link>
            <description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;Per Fabio's Post, NHibernate 2.0 has been released:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;NHibernate &lt;a href="http://2.0.0.GA"&gt;2.0.0.GA&lt;/a&gt; is released today      &lt;br /&gt;&lt;a href="https://sourceforge.net/project/showfiles.php?group_id=73818&amp;amp;package_id=73969"&gt;https://sourceforge.net/project/showfiles.php?group_id=73818&amp;amp;package_id=73969&lt;/a&gt;      &lt;br /&gt;Enjoy it!      &lt;br /&gt;We start the work for NH2.0.0SP1       &lt;br /&gt;~20 days to:      &lt;br /&gt;- support parameters in HQLFunctions      &lt;br /&gt;- minors change in tests      &lt;br /&gt;- improv for some dialects      &lt;br /&gt;- some other minors      &lt;br /&gt;And we hope nothing tremendous happen ;)      &lt;br /&gt;Bye.      &lt;br /&gt;Fabio Maulo      &lt;br /&gt;The human knowledge belong to the world&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Go grab the download and upgrade today! :)&lt;/p&gt;  &lt;p&gt;-Brian Chavez&lt;/p&gt;&lt;img src="http://bchavez.bitarmory.com/aggbug/65.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Brian Chavez</dc:creator>
            <guid>http://bchavez.bitarmory.com/archive/2008/08/23/nhibernate-2.0-ga-released.aspx</guid>
            <pubDate>Sat, 23 Aug 2008 17:01:09 GMT</pubDate>
            <wfw:comment>http://bchavez.bitarmory.com/comments/65.aspx</wfw:comment>
            <comments>http://bchavez.bitarmory.com/archive/2008/08/23/nhibernate-2.0-ga-released.aspx#feedback</comments>
            <wfw:commentRss>http://bchavez.bitarmory.com/comments/commentRss/65.aspx</wfw:commentRss>
            <trackback:ping>http://bchavez.bitarmory.com/services/trackbacks/65.aspx</trackback:ping>
        </item>
        <item>
            <title>Differences between NHibernate FetchMode.Eager and FetchMode.Join</title>
            <link>http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx</link>
            <description>&lt;p&gt;What are the differences between &lt;strong&gt;FetchMode.Eager&lt;/strong&gt; and &lt;strong&gt;FetchMode.Join&lt;/strong&gt;?  There are none!  According to the Java &lt;a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/FetchMode.html"&gt;Hibernate docs&lt;/a&gt;, &lt;strong&gt;&lt;em&gt;FetchMode.Eager&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;FetchMode.Lazy&lt;/em&gt;&lt;/strong&gt; have been deprecated.&lt;/p&gt;  &lt;p&gt;In NH 1.2.1 GA source code, this is how FetchMode is defined:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: #313132; color: #dfdfc0; font-family: courier new"&gt;   &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   15&lt;/span&gt;     [&lt;span style="color: #9ea6da"&gt;Serializable&lt;/span&gt;]&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   16&lt;/span&gt;     &lt;span style="font-weight: bold; color: #5b8a86"&gt;public&lt;/span&gt; &lt;span style="font-weight: bold; color: #5b8a86"&gt;enum&lt;/span&gt; &lt;span style="color: #7acbcf"&gt;FetchMode&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   17&lt;/span&gt;     {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   18&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   19&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; Default to the setting configured in the mapping file.&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   20&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   21&lt;/span&gt;         Default &lt;span style="color: #c9b68f"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold; color: #8fced1"&gt;0&lt;/span&gt;,&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   22&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   23&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; Fetch eagerly, using a separate select. Equivalent to&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   24&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;fetch="select"&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; (and &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;outer-join="false"&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;)&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   25&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   26&lt;/span&gt;         Select &lt;span style="color: #c9b68f"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold; color: #8fced1"&gt;1&lt;/span&gt;,&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   27&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   28&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; Fetch using an outer join.  Equivalent to&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   29&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;fetch="join"&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; (and &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;outer-join="true"&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/c&amp;gt;&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt;)&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   30&lt;/span&gt;         &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: #a4a4a4"&gt; &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   31&lt;/span&gt;         Join &lt;span style="color: #c9b68f"&gt;=&lt;/span&gt; &lt;span style="font-weight: bold; color: #8fced1"&gt;2&lt;/span&gt;,&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   32&lt;/span&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   33&lt;/span&gt;         Lazy &lt;span style="color: #c9b68f"&gt;=&lt;/span&gt; Select,&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   34&lt;/span&gt;         Eager &lt;span style="color: #c9b68f"&gt;=&lt;/span&gt; Join&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #eaaf00"&gt;   35&lt;/span&gt;     }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Well, now that clears up some of my confusions... &lt;img alt="confused0078.gif" src="http://bchavez.bitarmory.com/Images/emotions/confused0078.gif" /&gt;&lt;/p&gt;&lt;img src="http://bchavez.bitarmory.com/aggbug/51.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Brian Chavez</dc:creator>
            <guid>http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx</guid>
            <pubDate>Sat, 05 Apr 2008 02:25:30 GMT</pubDate>
            <wfw:comment>http://bchavez.bitarmory.com/comments/51.aspx</wfw:comment>
            <comments>http://bchavez.bitarmory.com/archive/2008/04/04/differences-between-nhibernate-fetchmode.eager-and-fetchmode.join.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://bchavez.bitarmory.com/comments/commentRss/51.aspx</wfw:commentRss>
            <trackback:ping>http://bchavez.bitarmory.com/services/trackbacks/51.aspx</trackback:ping>
        </item>
        <item>
            <title>Visitor Pattern for User Interface and Display</title>
            <link>http://bchavez.bitarmory.com/archive/2007/11/26/visitor-pattern-for-user-interface-and-display.aspx</link>
            <description>&lt;p&gt;I was browsing the NHibernate forums and came across a &lt;a href="http://forum.hibernate.org/viewtopic.php?t=978511&amp;amp;highlight=interface" target="_blank"&gt;post by "thatmikewilliams"&lt;/a&gt; who posted the following code:&lt;/p&gt; &lt;p&gt;&lt;span class="postbody"&gt;--------------------------------------------------------&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;span class="postbody"&gt;Because this functionality is closely tied to the user &lt;span style="color: rgb(255,0,0)"&gt;&lt;b&gt;interface&lt;/b&gt;&lt;/span&gt; you should probably use a visitor for this (rather than an overridden method in your class hierarchy). &lt;br /&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;/p&gt;&lt;table cellspacing="1" cellpadding="3" width="100%" align="center" border="0"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td class="code"&gt;package test.visitor; &lt;br /&gt;public abstract class Payment { &lt;br /&gt;   &lt;span style="color: rgb(255,0,0)"&gt;&lt;b&gt;interface&lt;/b&gt;&lt;/span&gt; Visitor { &lt;br /&gt;      Object visit(CardPayment payment); &lt;br /&gt;      Object visit(CashPayment payment); &lt;br /&gt;   } &lt;br /&gt;   public abstract Object accept(Visitor visitor); &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;package test.visitor; &lt;br /&gt;public class CardPayment extends Payment { &lt;br /&gt;   @Override &lt;br /&gt;   public Object accept(Visitor visitor) { &lt;br /&gt;      return visitor.visit(this); &lt;br /&gt;   } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;package test.visitor; &lt;br /&gt;public class CashPayment extends Payment { &lt;br /&gt;   @Override &lt;br /&gt;   public Object accept(Visitor visitor) { &lt;br /&gt;      return visitor.visit(this); &lt;br /&gt;   } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;package test.visitor; &lt;br /&gt;public class PaymentDisplayStringVisitor implements Payment.Visitor { &lt;br /&gt;   public Object visit(CardPayment payment) { &lt;br /&gt;      return "CC"; &lt;br /&gt;   } &lt;br /&gt;   public Object visit(CashPayment payment) { &lt;br /&gt;      return "CS"; &lt;br /&gt;   } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;package test.visitor; &lt;br /&gt;import java.util.ArrayList; &lt;br /&gt;import java.util.List; &lt;br /&gt;public class Test { &lt;br /&gt;&lt;br /&gt;   public static void main(String[] args) { &lt;br /&gt;      List&amp;lt;Payment&amp;gt; payments = new ArrayList&amp;lt;Payment&amp;gt;(); &lt;br /&gt;      payments.add(new CashPayment()); &lt;br /&gt;      payments.add(new CardPayment()); &lt;br /&gt;       &lt;br /&gt;      PaymentDisplayStringVisitor visitor = new PaymentDisplayStringVisitor(); &lt;br /&gt;      for (Payment payment : payments) { &lt;br /&gt;         System.out.println(payment.accept(visitor)); &lt;br /&gt;      } &lt;br /&gt;   } &lt;br /&gt;} &lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt; &lt;p&gt;----------------------------------------------&lt;/p&gt; &lt;p&gt;Using the Visitor Pattern to display domain specific types on a user interface is a nice approach instead of overriding specific methods on your domain object, on top of that, cluttering your domain objects with useless "UI" specific methods like ("get object name").  Pretty interesting approach. :)  Notice the &lt;strong&gt;PaymentDisplayStringVisitor&lt;/strong&gt; returns specific string type based on the type.  Very Cool.  I'll try extending this approach using .NET and Generics and post some code later.&lt;/p&gt;&lt;img src="http://bchavez.bitarmory.com/aggbug/39.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Brian Chavez</dc:creator>
            <guid>http://bchavez.bitarmory.com/archive/2007/11/26/visitor-pattern-for-user-interface-and-display.aspx</guid>
            <pubDate>Mon, 26 Nov 2007 20:19:02 GMT</pubDate>
            <wfw:comment>http://bchavez.bitarmory.com/comments/39.aspx</wfw:comment>
            <comments>http://bchavez.bitarmory.com/archive/2007/11/26/visitor-pattern-for-user-interface-and-display.aspx#feedback</comments>
            <wfw:commentRss>http://bchavez.bitarmory.com/comments/commentRss/39.aspx</wfw:commentRss>
            <trackback:ping>http://bchavez.bitarmory.com/services/trackbacks/39.aspx</trackback:ping>
        </item>
        <item>
            <title>NHibernate - null id in entry (don't flush the Session after an exception occurs)</title>
            <link>http://bchavez.bitarmory.com/archive/2007/11/14/nhibernate---null-id-in-entry-dont-flush-the-session.aspx</link>
            <description>&lt;p&gt;&lt;img style="margin: 10px" height="52" alt="image" src="http://bchavez.bitarmory.com/images/bchavez_bitarmory_com/WindowsLiveWriter/NHibernatenullidinentrydontflushtheSessi_BC87/image_308de7db-c740-4525-b003-2ded000b06d0.png" width="230" align="left" border="0" /&gt;I ran into this issue today when trying to persist one of my objects.  The cause of the problem was interesting.  I was trying to save an object when a property/columns in the table had a unique constraint.  As a result, the object that I was trying to persist would not persist simply because the object's property it failed to meet the unique constraint.&lt;/p&gt; &lt;p&gt;As a result, a call to Save() on the object failed and the ID on the object I was trying to save was not set, but NHibernate still processed the object and associated it with its persistence mechanism leaving it in a "semi-persistent" state with the NHibernate persistence manager (ie: NHibernate now knows about the object you tried to save and it SHOULD have fully evicted the object from its persistence manager because the save failed, but it didn't).&lt;/p&gt; &lt;p&gt;When an HTTP request finishes on my ASP.NET application, I flush and close all NHibernate session objects at the time the request is done.  And as a result, when the HTTP request finished, NHibernate attempted to flush the jacked up "semi-persistent" object (an object who's ID was null) and ultimately generating the error above.&lt;/p&gt; &lt;p&gt;So, the solution that I implemented was to wrap the Save() in a try{} catch{} statement, and if the save failed, immediately close and shutdown the session, handle the error/exception.  Then, check if Session.IsOpen when the HTTP request finishes.&lt;/p&gt; &lt;p&gt;Hope that helps, &lt;img alt="confused0081.gif" src="http://bchavez.bitarmory.com/Images/emotions/confused0081.gif" /&gt;&lt;/p&gt; &lt;p&gt;Brian Chavez&lt;/p&gt;&lt;img src="http://bchavez.bitarmory.com/aggbug/37.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Brian Chavez</dc:creator>
            <guid>http://bchavez.bitarmory.com/archive/2007/11/14/nhibernate---null-id-in-entry-dont-flush-the-session.aspx</guid>
            <pubDate>Wed, 14 Nov 2007 21:41:11 GMT</pubDate>
            <wfw:comment>http://bchavez.bitarmory.com/comments/37.aspx</wfw:comment>
            <comments>http://bchavez.bitarmory.com/archive/2007/11/14/nhibernate---null-id-in-entry-dont-flush-the-session.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://bchavez.bitarmory.com/comments/commentRss/37.aspx</wfw:commentRss>
            <trackback:ping>http://bchavez.bitarmory.com/services/trackbacks/37.aspx</trackback:ping>
        </item>
        <item>
            <title>NHibernate and Inverse=True|False Attribute</title>
            <link>http://bchavez.bitarmory.com/archive/2007/10/06/nhibernate-and-inversetruefalse-attribute.aspx</link>
            <description>&lt;p&gt;In order to truly understand the &lt;strong&gt;inverse&lt;/strong&gt; attribute in NHibernate, you need to first look at your relationship from the database table point of view and how your domain objects maps into their respective tables.  It comes down to determining who is the owner of the relationship association between the parent and child objects.&lt;/p&gt; &lt;p&gt;Suppose you have &lt;strong&gt;ParentTable (parentId PK, parentcol1, parentcol2)&lt;/strong&gt; and &lt;strong&gt;ChildTable(childid PK, parentid FK, childcol1, childcol2)&lt;/strong&gt;.  In a one-to-many relationship, you will have one parent and many children.  The &lt;strong&gt;ChildTable&lt;/strong&gt; has a primary key and foreign key.   The foreign key is a reference to the primary key in the &lt;strong&gt;ParentTable&lt;/strong&gt;.  If you look at the &lt;strong&gt;ChildTable&lt;/strong&gt;, you will notice that the &lt;strong&gt;ChildTable&lt;/strong&gt; stores extra information about a row in the &lt;strong&gt;ParentTable&lt;/strong&gt; via the &lt;strong&gt;ChildTable&lt;/strong&gt;'s parentId foreign key.  Using this perspective, the "child" owns (knows about) the relationship it has with its parent row.  So, the child will be the owner of the relationship in this one-to-many relationship.&lt;/p&gt; &lt;p&gt;Where does inverse come in?  Well, from the Parent's perspective, since the parent doesn't own the relationship it is considered the "inverse" of the relationship.  Below is some additional information I pulled down an edited from IBM's websphere pages:&lt;/p&gt; &lt;p&gt;&lt;a title="http://www.ibm.com/developerworks/websphere/techjournal/0708_vines/0708_vines.html" href="http://www.ibm.com/developerworks/websphere/techjournal/0708_vines/0708_vines.html"&gt;http://www.ibm.com/developerworks/websphere/techjournal/0708_vines/0708_vines.html&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;a&gt;&lt;b&gt;Many-to-one relationship&lt;/b&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The entity declaring the many-to-one relationship is the &lt;strong&gt;child object&lt;/strong&gt; (or owner of the relationship), &lt;strong&gt;as its table has the foreign key&lt;/strong&gt;, while the object that is referenced by the entity declaring the many-to-one relationship is the parent object. Since its table doesn't have the foreign key, &lt;strong&gt;it is the non-owner&lt;/strong&gt;, or &lt;strong&gt;inverse&lt;/strong&gt; of the relationship.&lt;/p&gt; &lt;p&gt;&lt;b&gt;Hibernate conventions&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;In Hibernate, many-to-one relationship maps as follows:  &lt;/p&gt;&lt;ul&gt; &lt;li&gt;Use many-to-one element in the child class.  &lt;/li&gt;&lt;li&gt;Define the primary key in the parent class. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;&lt;a&gt;&lt;b&gt;One-to-many relationship&lt;/b&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A one-to-many relationship defines a reference to a collection of objects. It is the most common kind of relationship that you will find in object models due to the fact that use cases typically require traversal from the parent object to its children, but may or may not require traversal from the child back to its parent; which means a unidirectional one-to-many relationship will suffice in most cases.&lt;/p&gt; &lt;p&gt;The entity declaring the one-to-many relationship is the &lt;strong&gt;parent object&lt;/strong&gt; (and is the non-owner). The table for this entity &lt;strong&gt;defines the primary key&lt;/strong&gt;, but it &lt;strong&gt;does not have the foreign key&lt;/strong&gt; -- that is in the &lt;strong&gt;child&lt;/strong&gt;.&lt;/p&gt; &lt;p&gt;In Hibernate, the mapping of one-to-many relationships is generally done by adding a column to the child table for the foreign key, but the details of the mapping differs depending on whether it is a unidirectional or a bidirectional one-to-many relationship.  &lt;/p&gt;&lt;p&gt;In the &lt;strong&gt;unidirectional case&lt;/strong&gt;, the &lt;strong&gt;foreign key column&lt;/strong&gt; in the &lt;strong&gt;child table doesn't map to a property&lt;/strong&gt; in the &lt;strong&gt;child object&lt;/strong&gt;; it is in the data model, but not the object model. Since it is &lt;strong&gt;unidirectional&lt;/strong&gt; there is just a &lt;strong&gt;property&lt;/strong&gt; in the parent object [that contains a collection of children]; &lt;strike&gt;not the child&lt;/strike&gt; [the child object does not contain any information back to the parent]. In addition, the&lt;strong&gt; foreign key column&lt;/strong&gt; [in the child table] has to be defined as &lt;strong&gt;nullable&lt;/strong&gt; because Hibernate will &lt;strong&gt;first insert the child row&lt;/strong&gt; (with a &lt;strong&gt;NULL foreign key&lt;/strong&gt;) and then &lt;strong&gt;update&lt;/strong&gt; &lt;strike&gt;it&lt;/strike&gt; [the inserted row] later [with the parent's primary key].  &lt;/p&gt;&lt;p&gt;In the &lt;strong&gt;bidirectional case&lt;/strong&gt;, the object-relational mapping is better because &lt;strong&gt;there is a property in the child object for the foreign key column&lt;/strong&gt;, and that column doesn't have to be nullable in the database. But the resulting object model has cyclic dependencies and tighter coupling between the objects, and requires additional programming to set both sides of the relationship.  &lt;/p&gt;&lt;p&gt;As you can see, there are several tradeoffs to consider with regard to the definition of one-to-many relationships, but using unidirectional relationships is generally recommended unless there are use cases that indicate the need for navigation in both directions.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;Hibernate conventions&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;In Hibernate, one-to-many (unidirectional) relationships map as follows:  &lt;/p&gt;&lt;ul&gt; &lt;li&gt;Use the set, bag, or list with one-to-many sub-element in parent class.  &lt;/li&gt;&lt;li&gt;Create a foreign key in the table representing the child class if the relationship is unidirectional; otherwise, use the &lt;strong&gt;many-to-one relationship&lt;/strong&gt; for a bidirectional relationship. &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;There are some additional features that are often used in the definition of one-to-many relationships[...]:&lt;/p&gt; &lt;ul&gt; &lt;li&gt; &lt;p&gt;&lt;b&gt;Hibernate&lt;/b&gt;  &lt;/p&gt;&lt;ul&gt; &lt;li&gt; &lt;p&gt;&lt;b&gt;inverse="true"&lt;/b&gt;&lt;br /&gt;In Hibernate, you might encounter the inverse="true" attribute being used in the definition of a bidirectional relationship. If so, don't worry, because this feature is equivalent to ... the non-owner of the relationship whose table doesn't have the foreign key. Similarly, the Hibernate inverse="false" attribute is equivalent to the ... owner of the relationship whose table has the foreign key.  &lt;/p&gt;&lt;p&gt;In general, these notions align, except for the case where someone defines a Hibernate mapping with inverse="true" set on the many-to-one side of a bidirectional relationship. If you find such a mapping, you should change it ..., as it is not a best practice in Hibernate and does not generate optimal SQL. For instance, if the many-to-one side is set to inverse="true" then every time you create a child, Hibernate will execute two SQL statements, one to create the child and one to update the child with the foreign key of the parent. Changing it to inverse="false" on the many-to-one side and setting inverse="true" on the one-to-many side will fix that oversight ...&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;&lt;a&gt;&lt;b&gt;Many-to-many relationship&lt;/b&gt; &lt;/a&gt; &lt;/p&gt;&lt;p&gt;A many-to-many relationship defines a reference to a collection of objects through a &lt;strong&gt;mapping table&lt;/strong&gt;. Many-to-many relationships are not all that common in an object model, but they will generally be bidirectional.  &lt;/p&gt;&lt;p&gt;Like the other &lt;strong&gt;bidirectional&lt;/strong&gt; relationships, there is an &lt;strong&gt;owning&lt;/strong&gt; and a &lt;strong&gt;non-owning&lt;/strong&gt; side. In this case, &lt;strong&gt;the owning side has the mapping table&lt;/strong&gt;, instead of the foreign key. &lt;strong&gt;Either side can be designated as the owning side&lt;/strong&gt;; it &lt;strong&gt;doesn't matter&lt;/strong&gt; which side you pick.  &lt;/p&gt;&lt;p&gt;&lt;b&gt;Hibernate conventions&lt;/b&gt;  &lt;/p&gt;&lt;p&gt;In Hibernate, many-to-many relationship maps as follows:  &lt;/p&gt;&lt;ul&gt; &lt;li&gt;The &lt;strong&gt;non-owner&lt;/strong&gt; uses the collections (set, bag, or list) element with the &lt;strong&gt;inverse="true"&lt;/strong&gt; attribute, the &lt;strong&gt;table attribute&lt;/strong&gt;, the &lt;strong&gt;key sub-element&lt;/strong&gt;, and the&lt;strong&gt; many-to-many sub-element&lt;/strong&gt;.  &lt;/li&gt;&lt;li&gt;The owner of the relationship uses the collections (set, bag, or list) element with the &lt;strong&gt;table attribute&lt;/strong&gt;, the &lt;strong&gt;key sub-element&lt;/strong&gt;, and the &lt;strong&gt;many-to-many sub-element&lt;/strong&gt;. &lt;/li&gt;&lt;/ul&gt;&lt;img src="http://bchavez.bitarmory.com/aggbug/28.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Brian Chavez</dc:creator>
            <guid>http://bchavez.bitarmory.com/archive/2007/10/06/nhibernate-and-inversetruefalse-attribute.aspx</guid>
            <pubDate>Sun, 07 Oct 2007 02:00:48 GMT</pubDate>
            <wfw:comment>http://bchavez.bitarmory.com/comments/28.aspx</wfw:comment>
            <comments>http://bchavez.bitarmory.com/archive/2007/10/06/nhibernate-and-inversetruefalse-attribute.aspx#feedback</comments>
            <slash:comments>19</slash:comments>
            <wfw:commentRss>http://bchavez.bitarmory.com/comments/commentRss/28.aspx</wfw:commentRss>
            <trackback:ping>http://bchavez.bitarmory.com/services/trackbacks/28.aspx</trackback:ping>
        </item>
    </channel>
</rss>