<?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>id &#8211; Pauls Blog</title>
	<atom:link href="https://sterl.org/tag/id/feed/" rel="self" type="application/rss+xml" />
	<link>https://sterl.org</link>
	<description></description>
	<lastBuildDate>Mon, 26 Feb 2024 19:34:03 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Strong IDs</title>
		<link>https://sterl.org/2015/06/pattern-strong-id/</link>
					<comments>https://sterl.org/2015/06/pattern-strong-id/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Sat, 13 Jun 2015 11:01:24 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Pattern & Best Practice]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[Strong ID]]></category>
		<category><![CDATA[StrongID]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=36</guid>

					<description><![CDATA[In some projects you may encounter the so called &#8222;strong id pattern&#8220;. What is it? Strong IDs are simply wrapped primitive or simple types into classes. Just remember what we did in Java before the Enum come around. We had just Strings or Numbers in the code which we used to assign well known values&#8230;]]></description>
										<content:encoded><![CDATA[<p>In some projects you may encounter the so called &#8222;strong id pattern&#8220;.</p>
<h2>What is it?</h2>
<p>Strong IDs are simply wrapped primitive or simple types into classes. Just remember what we did in Java before the <code>Enum</code> come around. We had just Strings or Numbers in the code which we used to assign well known values to specific objects. In some projects these Strings has already been wrapped into classes. Now with <code>Enums</code> this is mostly obsolete. But for Ids we can still apply this pattern.</p>
<p>Imagine the following classes:</p>
<pre class="lang:java decode:true" title="Simple Class">class Office {
  String id;
  String name;
}

class Person {
  String id;
  String name;
}
// we could use any string we want, even the ID from the office
interface PersonService {
  Person getById(String id);
}</pre>
<p>Both classes have an <span style="color: #ffffff; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;"><span style="font-size: 14px; line-height: 21px; background-color: #333333;">id</span></span>, simply just as a <code>String</code>. Keeping just these ids in hand you cannot tell if it is a <code>Person Id</code> or the <code>Office Id</code>. To make it more obvious we could change the code too:</p>
<pre class="lang:default decode:true" title="Simple class with Strong ID">final class OfficeId {
  String value;
}
class Office {
  OfficeId id;
  String name;
}

final class PersonId {
  String value;
}
class Person {
  PersonId id;
  String name;
}
// we can now use the real type, compile time save
interface PersonService {
   Person getById(PersonId id);
}</pre>
<p>If you now have either the <code>OfficeId</code> or <code>PersonId</code> in hand, you cannot accidentally mix them up. You exactly know what you have and what it identifies.</p>
<p>Usually the ID classes are not mutable too.</p>
<h2>Advantages</h2>
<p>In larger projects you have many classes, it is easier to understand and keep different IDs in method signatures. That we use the right type in generic lists or method calls can be ensured now during compile time.</p>
<p>Also nice we can easyer change the type of ID, e.g. from <code>String</code> to <code>Long</code> or to a composite type.</p>
<p>If we move this sample to REST where we usually want to return a URI as an identifier to an object this pattern even gets more attractive. If we assume our IDs have been initially just simple Longs wrapped into a Strong ID we could change the value it a String and create URI / String which we return to the clients.</p>
<h2>Disadvantages</h2>
<p>Well no pro without a contra. Having strong ids in the system makes the handling sometimes awkward. E.g. if you serialize this to JSON you get:</p>
<pre class="lang:js decode:true ">// with strong ID
{ id: {value: "abc"}, name: "Main Office"}

// without strong id, or what you usually want
{ id: "abc", name: "Main Office"}</pre>
<p>Furthermore Hibernate nor simple bean serializer/ deserializer can handle well immutable classes. Overall we would need to add additional code to handle that. e.g. Hibernate custom types etc.</p>
<p><a href="https://github.com/sterlp/training/tree/master/jpa-hibernate/src/main/java/org/sterl/jpa/strong_id">Github JPA example</a></p>


<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Kompakt: JPA | Hibernate | eigene @Id Datentypen |  Strong ID Pattern" width="640" height="360" src="https://www.youtube.com/embed/wPiE4hF6Si0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2015/06/pattern-strong-id/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
