<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Annotation Mapping for Ordered Lists in Hibernate</title>
	<atom:link href="http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/</link>
	<description>Technology, code, and thoughts by Stevi Deter</description>
	<lastBuildDate>Thu, 15 Dec 2011 15:20:14 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: stevi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-10819</link>
		<dc:creator>stevi</dc:creator>
		<pubDate>Fri, 16 Apr 2010 16:47:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-10819</guid>
		<description>Prem,
I see what you&#039;re saying now. I think I also suffered from a case of looking at the code, and similar code, so often that I wasn&#039;t really reading what was there. 

My mapping is unidirectional, not bidirectional, and would probably cause problems because neither end is marked as insertable/updatable false. 

Makes me think I should write an entry about unidirectional/bidirectional and all the gotchas. 

Thanks for pointing this out!</description>
		<content:encoded><![CDATA[<p>Prem,<br />
I see what you&#8217;re saying now. I think I also suffered from a case of looking at the code, and similar code, so often that I wasn&#8217;t really reading what was there. </p>
<p>My mapping is unidirectional, not bidirectional, and would probably cause problems because neither end is marked as insertable/updatable false. </p>
<p>Makes me think I should write an entry about unidirectional/bidirectional and all the gotchas. </p>
<p>Thanks for pointing this out!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prem</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-10814</link>
		<dc:creator>prem</dc:creator>
		<pubDate>Fri, 16 Apr 2010 10:51:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-10814</guid>
		<description>Hi Stevi,
I am not saying that mappedBy shoudl be comming on both the ends but 

@OneToMany(
mappedBy = “item”, cascade=CascadeType.ALL)
@OrderBy(“userId,date,amount”)
public List getBids() {
return bids;
}
and at the Bids class

@ManyToOne( targetEntity = com.stevideter.domain.Item.class )
  @JoinColumn(name = &quot;itemid&quot;, nullable = false)
  public Item getItem() {
    return item;
  }

your comment:
If we annotate the Bids in Item with mappedBy, we’re also telling Item how it relates to Bids, but with an important caveat — we’re telling Item it is not responsible for managing that relationship.

I really dont think this is what we are telling the hibernate when we use the mappedBy attribute as 

the mappedBy=&quot;item&quot; is pointing to the item and does it not mean that it is the item at the Bids class responsible for the relationship.

By using mappedBy attribute we are making the Item and the Bids class to be bidirectional and if we don&#039;t have the mappedBy attribute would it not be treated as unidirectional.

In your ManyToOne mapping in the Bids class, You have defined that  itemid is the foreign key and what is the need to say the same thing in your OneToMany mapping aswell.
Please do clarify and if i am missing some important point you were trying to highlight to me, do accept my ignorance.</description>
		<content:encoded><![CDATA[<p>Hi Stevi,<br />
I am not saying that mappedBy shoudl be comming on both the ends but </p>
<p>@OneToMany(<br />
mappedBy = “item”, cascade=CascadeType.ALL)<br />
@OrderBy(“userId,date,amount”)<br />
public List getBids() {<br />
return bids;<br />
}<br />
and at the Bids class</p>
<p>@ManyToOne( targetEntity = com.stevideter.domain.Item.class )<br />
  @JoinColumn(name = &#8220;itemid&#8221;, nullable = false)<br />
  public Item getItem() {<br />
    return item;<br />
  }</p>
<p>your comment:<br />
If we annotate the Bids in Item with mappedBy, we’re also telling Item how it relates to Bids, but with an important caveat — we’re telling Item it is not responsible for managing that relationship.</p>
<p>I really dont think this is what we are telling the hibernate when we use the mappedBy attribute as </p>
<p>the mappedBy=&#8221;item&#8221; is pointing to the item and does it not mean that it is the item at the Bids class responsible for the relationship.</p>
<p>By using mappedBy attribute we are making the Item and the Bids class to be bidirectional and if we don&#8217;t have the mappedBy attribute would it not be treated as unidirectional.</p>
<p>In your ManyToOne mapping in the Bids class, You have defined that  itemid is the foreign key and what is the need to say the same thing in your OneToMany mapping aswell.<br />
Please do clarify and if i am missing some important point you were trying to highlight to me, do accept my ignorance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stevi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-10793</link>
		<dc:creator>stevi</dc:creator>
		<pubDate>Thu, 15 Apr 2010 16:09:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-10793</guid>
		<description>Prem,

The JoinColumn referred to in the mapping on Bids in Item refers to the Foreign Key in the Bid table; we are explicitly telling Hibernate &quot;the Foreign Key to Item in Bids is a column named itemid&quot; 

Another way to phrase that is - we are telling Item &quot;find all rows in Bid where bid.itemid = item.id&quot;

If we annotate the Bids in Item with mappedBy, we&#039;re also telling Item how it relates to Bids, but with an important caveat -- we&#039;re telling Item it is not responsible for managing that relationship. 

The decision then comes to what behavior you want. Usually, when I&#039;m mapping child collections, I want the parent to be responsible for the collection. The reason is I want to be able to save the parent item, and have Hibernate automatically recognize I want it to also save any changes to the children. 

If this is not the behavior you want (which is quite possible), then mappedBy would be the more appropriate choice. 

In this example, though, you&#039;d have mappedBy on both ends of the relationship, which sort of defeats the purpose.</description>
		<content:encoded><![CDATA[<p>Prem,</p>
<p>The JoinColumn referred to in the mapping on Bids in Item refers to the Foreign Key in the Bid table; we are explicitly telling Hibernate &#8220;the Foreign Key to Item in Bids is a column named itemid&#8221; </p>
<p>Another way to phrase that is &#8211; we are telling Item &#8220;find all rows in Bid where bid.itemid = item.id&#8221;</p>
<p>If we annotate the Bids in Item with mappedBy, we&#8217;re also telling Item how it relates to Bids, but with an important caveat &#8212; we&#8217;re telling Item it is not responsible for managing that relationship. </p>
<p>The decision then comes to what behavior you want. Usually, when I&#8217;m mapping child collections, I want the parent to be responsible for the collection. The reason is I want to be able to save the parent item, and have Hibernate automatically recognize I want it to also save any changes to the children. </p>
<p>If this is not the behavior you want (which is quite possible), then mappedBy would be the more appropriate choice. </p>
<p>In this example, though, you&#8217;d have mappedBy on both ends of the relationship, which sort of defeats the purpose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prem</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-10789</link>
		<dc:creator>prem</dc:creator>
		<pubDate>Thu, 15 Apr 2010 13:14:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-10789</guid>
		<description>Hi ,
Instead of the join column in the OneToMany mapping it should be 


    @OneToMany(
mappedBy = &quot;item&quot;, cascade=CascadeType.ALL)
    @OrderBy(&quot;userId,date,amount&quot;)
    public List getBids() {
    return bids;
   }
Correct me if i am wrong.
Prem</description>
		<content:encoded><![CDATA[<p>Hi ,<br />
Instead of the join column in the OneToMany mapping it should be </p>
<p>    @OneToMany(<br />
mappedBy = &#8220;item&#8221;, cascade=CascadeType.ALL)<br />
    @OrderBy(&#8220;userId,date,amount&#8221;)<br />
    public List getBids() {<br />
    return bids;<br />
   }<br />
Correct me if i am wrong.<br />
Prem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: prem</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-10788</link>
		<dc:creator>prem</dc:creator>
		<pubDate>Thu, 15 Apr 2010 12:48:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-10788</guid>
		<description>Hi i was just wondering about the OneToMany mapping you have created in your class Item.
@OneToMany(cascade=CascadeType.ALL)
  @JoinColumn(name=&quot;itemid&quot;)
  @OrderBy(&quot;userId,date,amount&quot;)
  public List getBids() {
    return bids;
  }
Is this the right way of doing this as my question is how would the ITEM table would have multiple foreign keys for a particular Item.
I think the join column should come on the ManyToOne side of the table and not the oneToMany side and if you dont want that to be a bidirectional relationship then probably you got to use a join table to make it unidirectional. 
Correct me if i am wrong.

Does it not look like the itemid is going to be in the ITEM table?

Thanks
Prem</description>
		<content:encoded><![CDATA[<p>Hi i was just wondering about the OneToMany mapping you have created in your class Item.<br />
@OneToMany(cascade=CascadeType.ALL)<br />
  @JoinColumn(name=&#8221;itemid&#8221;)<br />
  @OrderBy(&#8220;userId,date,amount&#8221;)<br />
  public List getBids() {<br />
    return bids;<br />
  }<br />
Is this the right way of doing this as my question is how would the ITEM table would have multiple foreign keys for a particular Item.<br />
I think the join column should come on the ManyToOne side of the table and not the oneToMany side and if you dont want that to be a bidirectional relationship then probably you got to use a join table to make it unidirectional.<br />
Correct me if i am wrong.</p>
<p>Does it not look like the itemid is going to be in the ITEM table?</p>
<p>Thanks<br />
Prem</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: praveen</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-1001</link>
		<dc:creator>praveen</dc:creator>
		<pubDate>Sun, 22 Mar 2009 04:11:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-1001</guid>
		<description>thanks for giving the difference between merge and saveOrUpdate() in hibernate.  This is very good explination.</description>
		<content:encoded><![CDATA[<p>thanks for giving the difference between merge and saveOrUpdate() in hibernate.  This is very good explination.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-559</link>
		<dc:creator>Abhi</dc:creator>
		<pubDate>Tue, 02 Dec 2008 11:20:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-559</guid>
		<description>I resolved it. Thanks.</description>
		<content:encoded><![CDATA[<p>I resolved it. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-557</link>
		<dc:creator>Abhi</dc:creator>
		<pubDate>Mon, 01 Dec 2008 04:32:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-557</guid>
		<description>What I meant was the itemid column in bids table. I apologize for the typo.</description>
		<content:encoded><![CDATA[<p>What I meant was the itemid column in bids table. I apologize for the typo.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stevi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-556</link>
		<dc:creator>stevi</dc:creator>
		<pubDate>Fri, 28 Nov 2008 18:44:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-556</guid>
		<description>Without seeing your code, my first question is have you added an ID generating strategy? 

That is one element I omitted from the example. 

For example, if you&#039;re using MySQL or SQL Server, you can use the built in identity generation: 
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  public Integer getId() {
    return id;
  }

When using Oracle, I usually use the sequence strategy: 

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&quot;ITEMID_SEQ&quot;)
  public Integer getId() {
    return id;
  }</description>
		<content:encoded><![CDATA[<p>Without seeing your code, my first question is have you added an ID generating strategy? </p>
<p>That is one element I omitted from the example. </p>
<p>For example, if you&#8217;re using MySQL or SQL Server, you can use the built in identity generation:<br />
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)<br />
  public Integer getId() {<br />
    return id;<br />
  }</p>
<p>When using Oracle, I usually use the sequence strategy: </p>
<p>@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator=&#8221;ITEMID_SEQ&#8221;)<br />
  public Integer getId() {<br />
    return id;<br />
  }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhi</title>
		<link>http://www.stevideter.com/2008/10/20/annotation-mapping-for-ordered-lists-in-hibernate/comment-page-1/#comment-555</link>
		<dc:creator>Abhi</dc:creator>
		<pubDate>Fri, 28 Nov 2008 13:29:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.stevideter.com/?p=41#comment-555</guid>
		<description>Thanks for this. Saved me a lot of time. 

But the mapping does not work if I add a database constraint saying that the itemid column in item table is not null. Then when I try to save an item class with associated bids I get an error saying &quot;not-null property references a null or transient value&quot;.

Any pointers?</description>
		<content:encoded><![CDATA[<p>Thanks for this. Saved me a lot of time. </p>
<p>But the mapping does not work if I add a database constraint saying that the itemid column in item table is not null. Then when I try to save an item class with associated bids I get an error saying &#8220;not-null property references a null or transient value&#8221;.</p>
<p>Any pointers?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

