diff --git a/Doc/Persist/topics.xml b/Doc/Persist/topics.xml index e065fa5f36c448d2006419cc5c912e5dddd476fa..33d343c2264f7867ef7debeaddd96d774853a4cd 100644 --- a/Doc/Persist/topics.xml +++ b/Doc/Persist/topics.xml @@ -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>