Skip to content
Snippets Groups Projects
Commit 5c2909dd authored by Goik Martin's avatar Goik Martin
Browse files

Simple key example

parent 16490992
No related branches found
No related tags found
No related merge requests found
......@@ -399,7 +399,92 @@ public class Airline {
<section xml:id="persistSectAirlineSimpleKey">
<title>Advocating simple key definitions</title>
<para/>
<qandaset defaultlabel="qanda"
xml:id="persistQandaAirlineKeyAutoIncrementSimple">
<qandadiv>
<qandaentry>
<question>
<para>We might choose a simpler model to achieve non-nullable
keys:</para>
<programlisting language="java">@Entity
public class Airline {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long id;
@Column(unique=true, nullable=false)
String name;
@Column(unique=true, nullable=false)
String icaoCode;
public Airline(final String name, final String icaoCode) {
this.name = name;
this.icaoCode = icaoCode;
}
protected Airline(){}
}</programlisting>
<para>Discuss the pros and cons of this approach compared to
<xref linkend="persistQandaAirlineKeyAutoIncrement"/>.</para>
</question>
<answer>
<glosslist>
<glossentry>
<glossterm>Pros:</glossterm>
<glossdef>
<orderedlist>
<listitem>
<para>This approach is easier to understand.</para>
</listitem>
<listitem>
<para>Stable approach with respect to <xref
linkend="glo_Java"/> code refactoring: If e.g. the
property <property>icaoCode</property> gets
consistently renamed to
<property>icaoCode</property>Number <xref
linkend="glo_SqlDdl"/> code will still be properly
generated. However this problem may be overcome by
providing stable <xref linkend="glo_SqlDdl"/>
attribute names via <code>@Column(name="icaoCode"
...)</code> thereby protecting the database from
unnecessary schema evolution issues being inflicted by
<xref linkend="glo_Java"/> code refactoring.</para>
</listitem>
</orderedlist>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Cons:</glossterm>
<glossdef>
<orderedlist>
<listitem>
<para>Does not work for composed keys.</para>
</listitem>
<listitem>
<para>Does not allow for self explanatory constraint
names. We end up with e.g. :</para>
<programlisting language="sql">...
UNIQUE KEY `UK_s759kv0st7r42c85xqjjiusde` (`icaoCode`),
UNIQUE KEY `UK_swdntm8xd26ugm3t78mkkted2` (`name`)
...</programlisting>
</listitem>
</orderedlist>
</glossdef>
</glossentry>
</glosslist>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</section>
</chapter>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment