<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>demetrio.it blog &#187; auto generated key</title>
	<atom:link href="http://blog.demetrio.it/tag/auto-generated-key/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.demetrio.it</link>
	<description>nessun limite all'immaginazione</description>
	<lastBuildDate>Thu, 15 Jul 2010 15:42:55 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>One-to-one relationship with auto-generated key using JPA/Hibernate using annotations</title>
		<link>http://blog.demetrio.it/2009/04/03/one-to-one-relationship-with-auto-generated-key-using-jpahibernate-using-annotations/</link>
		<comments>http://blog.demetrio.it/2009/04/03/one-to-one-relationship-with-auto-generated-key-using-jpahibernate-using-annotations/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 01:18:52 +0000</pubDate>
		<dc:creator>Demetrio Filocamo</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[auto generated key]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[one-to-one]]></category>
		<category><![CDATA[one2one]]></category>
		<category><![CDATA[seam]]></category>

		<guid isPermaLink="false">http://blog.demetrio.it/?p=115</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p> </p>
<p>Adding a one-to-one relationship with shared primary key can become a difficult task if the main table key is an auto-generated one.</p>
<p>Even the Hibernate Annotations documentation example only works with not auto-generated id, if you try to use an auto incremental id, for example, </p>
<p>it&#8217;s impossible to persist the 2 tables at the same time.</p>
<p>Surfing on the net I&#8217;ve seen some ppl with this kind of problem (I suppose one-to-one relationships are not so common, don&#8217;t know why) and most of ppl use the foreign-key approach that works ok but it&#8217;s not clean from my point of view, so I wanted to find a solution for my case.</p>
<p>The original example from the Hibernate Annotations Documentation is:</p>
<p>@Entity<br />
public class Body {</p>
<p>    @Id<br />
    public Long getId() { return id; }</p>
<p>    @OneToOne(cascade = CascadeType.ALL)<br />
    @PrimaryKeyJoinColumn<br />
    public Heart getHeart() {<br />
        return heart;<br />
    }</p>
<p>    &#8230;</p>
<p>}</p>
<p> </p>
<p>@Entity<br />
public class Heart {</p>
<p>    @Id<br />
    public Long getId() { &#8230; }</p>
<p>}</p>
<p>As you can see this as not auto generated id and will not work just putting the <em>@GeneratedValue</em> annotation.</p>
<p>The final solution is:</p>
<p>@Entity<br />
public class Body {</p>
<p>    @Id<br />
    <strong>@GeneratedValue(strategy = IDENTITY)</strong><br />
    public Long getId() { return id; }</p>
<p>    @OneToOne(cascade = CascadeType.ALL)<br />
    @PrimaryKeyJoinColumn<br />
    public Heart getHeart() {<br />
        return heart;<br />
    }</p>
<p>    &#8230;</p>
<p>}</p>
<p>@Entity<br />
public class Heart {</p>
<p>    @Id<br />
<strong>    @GeneratedValue(generator=&#8221;foreign&#8221;) <br />
    @GenericGenerator(name=&#8221;foreign&#8221;, strategy = &#8220;foreign&#8221;, parameters = {@Parameter(name=&#8221;property&#8221;, value=&#8221;body&#8221;)})<br />
<span style="font-weight: normal; ">    public Long getId() { &#8230;}</span></strong></p>
<p><strong>    @OneToOne(mappedBy=&#8221;heart&#8221;)<br />
    public Body getBody() { &#8230; }</strong></p>
<p>}</p>
<div>Now all works fine for me.</div>
<div>Thanks</div>
<div>Demetrio</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.demetrio.it/2009/04/03/one-to-one-relationship-with-auto-generated-key-using-jpahibernate-using-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
