<?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: JPA implementation patterns: Using UUIDs as primary keys</title>
	<atom:link href="http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/</link>
	<description></description>
	<lastBuildDate>Thu, 18 Mar 2010 13:27:36 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: cdiesse</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-93467</link>
		<dc:creator>cdiesse</dc:creator>
		<pubDate>Fri, 11 Dec 2009 16:47:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-93467</guid>
		<description>My main concern about defining Ids in MappedSuperClass, is to define the id but have opportunities to override the GeneratedValue strategy in derived classes.

This is a missing feature in JPA 1.0 and JPA 2.0.  Maybe this can be overcome with EntityListeners ... 

Anythought ?</description>
		<content:encoded><![CDATA[<p>My main concern about defining Ids in MappedSuperClass, is to define the id but have opportunities to override the GeneratedValue strategy in derived classes.</p>
<p>This is a missing feature in JPA 1.0 and JPA 2.0.  Maybe this can be overcome with EntityListeners &#8230; </p>
<p>Anythought ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ralph</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92399</link>
		<dc:creator>Ralph</dc:creator>
		<pubDate>Wed, 02 Sep 2009 12:44:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92399</guid>
		<description>Hi

Instead of assign the uid inside the constructor you could use an EntityListener. This way the uid is only generated before persist or when somebody calls hashCode() or equals().

@MappedSuperclass
@EntityListeners({AbstractEntity.AbstractEntityListener.class})
public abstract class AbstractEntity implements Serializable {

  @Id
  @Column(length=36)
  private String uid;
  
  @Override
  public boolean equals(Object o) {
    return (o == this &#124;&#124; (o instanceof AbstractEntity &amp;&amp; uid().equals(((AbstractEntity)o).uid())));
  }
  
  @Override
  public int hashCode() {
    return uid().hashCode();
  }
  
  String uid() {
    if (uid == null) {
      uid = new UUID().toString();
    }
    return uid;
  }

  public static class AbstractEntityListener {      
    @PrePersist
    public void onPrePersist(AbstractEntity abstractEntity) {
      abstractEntity.uid();
    }
  }
}</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Instead of assign the uid inside the constructor you could use an EntityListener. This way the uid is only generated before persist or when somebody calls hashCode() or equals().</p>
<p>@MappedSuperclass<br />
@EntityListeners({AbstractEntity.AbstractEntityListener.class})<br />
public abstract class AbstractEntity implements Serializable {</p>
<p>  @Id<br />
  @Column(length=36)<br />
  private String uid;</p>
<p>  @Override<br />
  public boolean equals(Object o) {<br />
    return (o == this || (o instanceof AbstractEntity &amp;&amp; uid().equals(((AbstractEntity)o).uid())));<br />
  }</p>
<p>  @Override<br />
  public int hashCode() {<br />
    return uid().hashCode();<br />
  }</p>
<p>  String uid() {<br />
    if (uid == null) {<br />
      uid = new UUID().toString();<br />
    }<br />
    return uid;<br />
  }</p>
<p>  public static class AbstractEntityListener {<br />
    @PrePersist<br />
    public void onPrePersist(AbstractEntity abstractEntity) {<br />
      abstractEntity.uid();<br />
    }<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Albert Sikkema</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92246</link>
		<dc:creator>Albert Sikkema</dc:creator>
		<pubDate>Tue, 04 Aug 2009 20:28:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92246</guid>
		<description>All,

Really nice to see so many responses here. One of the things which triggered me to write this blog was that I always went for the &#039;easy&#039; way in terms of selecting a primary key strategy without giving it much thought. I&#039;m no UUID expert in any way although I found it very useful and I&#039;ll use it again more often.

@Micheal: Do you have an example of your pool implementation?

@Jens: We felt comfortable to use the java.util.UUID implementation for our specific use-case, but I agree with you that for true universal uniqueness you should take a different implementation. Thanks for the links!</description>
		<content:encoded><![CDATA[<p>All,</p>
<p>Really nice to see so many responses here. One of the things which triggered me to write this blog was that I always went for the &#8216;easy&#8217; way in terms of selecting a primary key strategy without giving it much thought. I&#8217;m no UUID expert in any way although I found it very useful and I&#8217;ll use it again more often.</p>
<p>@Micheal: Do you have an example of your pool implementation?</p>
<p>@Jens: We felt comfortable to use the java.util.UUID implementation for our specific use-case, but I agree with you that for true universal uniqueness you should take a different implementation. Thanks for the links!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Burnley</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92148</link>
		<dc:creator>Chris Burnley</dc:creator>
		<pubDate>Thu, 23 Jul 2009 05:15:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92148</guid>
		<description>Should you put this in the constructor ? Isn&#039;t the default constructor going to get executed indirectly via the JPA implementation when the entity is loaded from the database ?</description>
		<content:encoded><![CDATA[<p>Should you put this in the constructor ? Isn&#8217;t the default constructor going to get executed indirectly via the JPA implementation when the entity is loaded from the database ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Response to: using UUIDs as primary keys &#124; Michael Dowling&#8217;s Whispers</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92143</link>
		<dc:creator>Response to: using UUIDs as primary keys &#124; Michael Dowling&#8217;s Whispers</dc:creator>
		<pubDate>Wed, 22 Jul 2009 05:17:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92143</guid>
		<description>[...] using UUIDs as primary keys Tuesday, July 21st, 2009 &#124; Java &#124; michael    So, I read someone&#8217;s blog post about the pros and cons of using UUIDs as primary keys.  I am a big believer in using UUIDs as [...]</description>
		<content:encoded><![CDATA[<p>[...] using UUIDs as primary keys Tuesday, July 21st, 2009 | Java | michael    So, I read someone&#8217;s blog post about the pros and cons of using UUIDs as primary keys.  I am a big believer in using UUIDs as [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Dowling</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92142</link>
		<dc:creator>Michael Dowling</dc:creator>
		<pubDate>Wed, 22 Jul 2009 05:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92142</guid>
		<description>Thanks for the article.  I am a big believer in using UUIDs as primary keys for domain objects in the DB.  Having used them quite a bit in my projects, I wanted to debate a couple of the cons:

1.  Performance can be an issue:  Yes, it can, but grabbing a sequence from the DB creates a bigger issue (2 calls per new insert, depending on the RDBMS platform).  This can be easily remedied by writing a simple UUID pool object which generates the UUIDs in batch, and doles them out on request.  When the pool runs low/out, regenerate.

2.  Ordering/Sorting.  Indeed, you really cannot order by the primary key in the case of a UUID.  But then again, why does one usually order by primary key?  Sequential primary keys tend to indicate the order of entity creation.  Good DB design principles include having a created_date and a modify_date column as part of the entity (depending on your needs and requirements, of course, but I&#039;d say this is mostly true).  With this in mind, you CAN order by the create date.

3.  cannot test if a record has already been persisted by checking if the Id field has been set.  Uhm, you can write a @GenericGenerator to generate the uuid if you&#039;re using a JPA provider other than hibernate, and it wouldn&#039;t be hard at all.  The UUID is only set upon insert call to the DB, so you can do this check.  But the OP is right, I question a design that checks for the existence of a PK being set.

My $0.02.  Reposting to my blog

-Michael</description>
		<content:encoded><![CDATA[<p>Thanks for the article.  I am a big believer in using UUIDs as primary keys for domain objects in the DB.  Having used them quite a bit in my projects, I wanted to debate a couple of the cons:</p>
<p>1.  Performance can be an issue:  Yes, it can, but grabbing a sequence from the DB creates a bigger issue (2 calls per new insert, depending on the RDBMS platform).  This can be easily remedied by writing a simple UUID pool object which generates the UUIDs in batch, and doles them out on request.  When the pool runs low/out, regenerate.</p>
<p>2.  Ordering/Sorting.  Indeed, you really cannot order by the primary key in the case of a UUID.  But then again, why does one usually order by primary key?  Sequential primary keys tend to indicate the order of entity creation.  Good DB design principles include having a created_date and a modify_date column as part of the entity (depending on your needs and requirements, of course, but I&#8217;d say this is mostly true).  With this in mind, you CAN order by the create date.</p>
<p>3.  cannot test if a record has already been persisted by checking if the Id field has been set.  Uhm, you can write a @GenericGenerator to generate the uuid if you&#8217;re using a JPA provider other than hibernate, and it wouldn&#8217;t be hard at all.  The UUID is only set upon insert call to the DB, so you can do this check.  But the OP is right, I question a design that checks for the existence of a PK being set.</p>
<p>My $0.02.  Reposting to my blog</p>
<p>-Michael</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fernando Machado</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92136</link>
		<dc:creator>Fernando Machado</dc:creator>
		<pubDate>Tue, 21 Jul 2009 03:21:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92136</guid>
		<description>You can use UUIDs with EclipseLink as well.

http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing</description>
		<content:encoded><![CDATA[<p>You can use UUIDs with EclipseLink as well.</p>
<p><a href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing" rel="nofollow">http://wiki.eclipse.org/EclipseLink/Examples/JPA/CustomSequencing</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: JPA Implementation Patterns &#171; Fernando Franzini Java Blog</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92096</link>
		<dc:creator>JPA Implementation Patterns &#171; Fernando Franzini Java Blog</dc:creator>
		<pubDate>Thu, 16 Jul 2009 12:11:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92096</guid>
		<description>[...] Using UUIDs as primary keys (guest blog by Albert Sikkema) [...]</description>
		<content:encoded><![CDATA[<p>[...] Using UUIDs as primary keys (guest blog by Albert Sikkema) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Schuler</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92088</link>
		<dc:creator>Peter Schuler</dc:creator>
		<pubDate>Wed, 15 Jul 2009 18:58:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92088</guid>
		<description>Ik think UUID&#039;s certainly have their function. There are however big disadvantages in using them. A big disadvantage is database readabillity. Debugging your database is quit hard when you have to follow primary/foreign key&#039;s based on UUID.

For an interesting discussing about the opinion of the Hibernate developers see this post on the hibernate forum: https://forum.hibernate.org/viewtopic.php?t=967211.</description>
		<content:encoded><![CDATA[<p>Ik think UUID&#8217;s certainly have their function. There are however big disadvantages in using them. A big disadvantage is database readabillity. Debugging your database is quit hard when you have to follow primary/foreign key&#8217;s based on UUID.</p>
<p>For an interesting discussing about the opinion of the Hibernate developers see this post on the hibernate forum: <a href="https://forum.hibernate.org/viewtopic.php?t=967211." rel="nofollow">https://forum.hibernate.org/viewtopic.php?t=967211.</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Göring</title>
		<link>http://blog.xebia.com/2009/06/03/jpa-implementation-patterns-using-uuids-as-primary-keys/comment-page-1/#comment-92087</link>
		<dc:creator>Jens Göring</dc:creator>
		<pubDate>Wed, 15 Jul 2009 13:41:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.xebia.com/?p=1856#comment-92087</guid>
		<description>UUID.randomUUID() is not Universal Unique because it is based solely on random numbers (http://en.wikipedia.org/wiki/Uuid#Version_4_.28random.29). It might be improbable to get the same number twice locally, but you should not depend on *universal* uniqueness.

If you need universal uniqueness, you have to rely on a third party UUID library like http://johannburkard.de/software/uuid/ or http://jug.safehaus.org/, which evaluate the network adapter&#039;s MAC address to build a Version 1 UUID using platform dependend code.

&gt;UUIDs are not guessable

If you use UUID Version 1 as mentioned above, this might not hold, see http://en.wikipedia.org/wiki/Uuid#Version_1_.28MAC_address.29

I&#039;ll like to add another Con:

By using UUID, you introduce another dependency to your Domain class - this might be a problem if you for example want to transmit instances of these classes via Gilead to GWT client side code.

I had this problem once and solved it by setting the UUID in the DAO&#039;s insert/update method if it is null.</description>
		<content:encoded><![CDATA[<p>UUID.randomUUID() is not Universal Unique because it is based solely on random numbers (<a href="http://en.wikipedia.org/wiki/Uuid#Version_4_.28random.29)" rel="nofollow">http://en.wikipedia.org/wiki/Uuid#Version_4_.28random.29)</a>. It might be improbable to get the same number twice locally, but you should not depend on *universal* uniqueness.</p>
<p>If you need universal uniqueness, you have to rely on a third party UUID library like <a href="http://johannburkard.de/software/uuid/" rel="nofollow">http://johannburkard.de/software/uuid/</a> or <a href="http://jug.safehaus.org/" rel="nofollow">http://jug.safehaus.org/</a>, which evaluate the network adapter&#8217;s MAC address to build a Version 1 UUID using platform dependend code.</p>
<p>&gt;UUIDs are not guessable</p>
<p>If you use UUID Version 1 as mentioned above, this might not hold, see <a href="http://en.wikipedia.org/wiki/Uuid#Version_1_.28MAC_address.29" rel="nofollow">http://en.wikipedia.org/wiki/Uuid#Version_1_.28MAC_address.29</a></p>
<p>I&#8217;ll like to add another Con:</p>
<p>By using UUID, you introduce another dependency to your Domain class &#8211; this might be a problem if you for example want to transmit instances of these classes via Gilead to GWT client side code.</p>
<p>I had this problem once and solved it by setting the UUID in the DAO&#8217;s insert/update method if it is null.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
