diff --git a/Doc/Sda1/Ref/src/rdbms2catalog.jpa.sksleton.zip b/Doc/Sda1/Ref/src/rdbms2catalog.jpa.sksleton.zip new file mode 100644 index 0000000000000000000000000000000000000000..b5d18f792c525ad17a67d58a289beb0be7205051 Binary files /dev/null and b/Doc/Sda1/Ref/src/rdbms2catalog.jpa.sksleton.zip differ diff --git a/Doc/Sda1/jdbc.xml b/Doc/Sda1/jdbc.xml index d4e00ead7cdf1c12b5daae117469c0073bfc2de0..518c4587fd1ecb947518d9d952ae60aaca7d5a0d 100644 --- a/Doc/Sda1/jdbc.xml +++ b/Doc/Sda1/jdbc.xml @@ -3574,7 +3574,7 @@ PersistenceHandler.password=<emphasis role="bold">XYZ</emphasis> <co </qandaset> </section> - <section xml:id="sectRelationadatal2Xml"> + <section xml:id="sda1SectRelationadatal2Xml"> <title>Reversing <xref linkend="glo_XML"/> to Rdbms</title> <qandaset defaultlabel="qanda" xml:base="qandaRelationaldata2Xml" diff --git a/Doc/Sda1/jpaintro.xml b/Doc/Sda1/jpaintro.xml index 9c7c38d4eb40c7245efef1f6a09bda930b6d6abe..d62b78208844311ebf0ac8a6ab3d50cc8cddaa91 100644 --- a/Doc/Sda1/jpaintro.xml +++ b/Doc/Sda1/jpaintro.xml @@ -330,4 +330,119 @@ CREATE TABLE `Lecture` ( </qandadiv> </qandaset> </section> + + <section xml:id="sda1SectRdbms2catalogJpa"> + <title>Exporting <xref linkend="glo_XML"/> catalog data using <xref + linkend="glo_JPA"/>.</title> + + <qandaset defaultlabel="qanda" xml:id="sda1QandaRdbms2catalogJpa"> + <qandadiv> + <qandaentry> + <question> + <para>In <xref linkend="sda1SectRelationadatal2Xml"/> you exported + RDBMS data to <xref linkend="glo_XML"/> by means of <xref + linkend="glo_JDBC"/>.</para> + + <para>In this exercise we want to facilitate the database reading + part by using <xref linkend="glo_JPA"/>. You may follow the + subsequently described steps:</para> + + <orderedlist> + <listitem> + <para>Import the Maven skeleton project <link + xlink:href="Ref/src/rdbms2catalog.jpa.sksleton.zip">rdbms2catalog.jpa.sksleton.zip</link>.</para> + </listitem> + + <listitem> + <para>Add required properties among with <xref + linkend="glo_JPA"/> annotations to + <classname>de.hdm_stuttgart.mi.sda1.sql2catalog.model.Product</classname> + and + <classname>de.hdm_stuttgart.mi.sda1.sql2catalog.model.Description</classname>.</para> + + <tip> + <orderedlist> + <listitem> + <para>Since <xref linkend="glo_JPA"/> requires an + <code>@id</code> you must provide it. Though composite + <code>@id</code> are possible you better define an + additional (business) unique constraint by:</para> + + <programlisting language="none">@Table( + uniqueConstraints= + @UniqueConstraint(columnNames={"product", "orderIndex"}) + ) +@Entity +public class Description { ...</programlisting> + </listitem> + + <listitem> + <para>The required <code>@ManyToOne</code> annotation + will result in a SQL schema property + <property>product_id</property>. This may be renamed to + <property>product</property> by adding + <code>@JoinColumn(name="product")</code>.</para> + </listitem> + + <listitem> + <para>Adding @OneToMany(mappedBy = "product", <emphasis + role="bold">cascade=CascadeType.ALL</emphasis>) allows + for transitive persistence of dependent + <classname>de.hdm_stuttgart.mi.sda1.sql2catalog.model.Description</classname> + entities.</para> + </listitem> + </orderedlist> + </tip> + + <para>A correct implementation should allow for enabling + <classname>rdbms2catalog.TestSchema</classname> by + uncommenting the test method's body. </para> + </listitem> + + <listitem> + <para>Write a second <productname>Junit</productname> test + class checking for uniqueness of + (<property>product</property>, + <property>itemOrder</property>). Using duplicates should yield + an exception which may be accounted for by using:</para> + + <programlisting language="none">public class TestSchemaConflict { +... + @Test(expected= ...Exception.class) + public void insertData() { + { + final EntityTransaction transaction = em.getTransaction(); + transaction.begin(); + + final Product monkeyPickedTea = new Product(1, "Monkey Picked Tea", null); + new Description(monkeyPickedTea, 0, "Picked only by specially trained monkeys"); + new Description(monkeyPickedTea, 0, "Rare wild Chinese tea"); // oops! Unique key violation +...</programlisting> + </listitem> + + <listitem> + <para>Implement the actual RDBMS to <xref linkend="glo_XML"/> + export application.</para> + + <tip> + <para><filename>META-INF/persistence.xml</filename> does + contain a second persistence unit definition + <property>name="none"</property> which does not alter the + database schema. Since your tests already inserted required + test data you may use this alternate configuration for using + your RDBMS as a data source.</para> + </tip> + </listitem> + </orderedlist> + </question> + + <answer> + <annotation role="make"> + <para role="eclipse">Sda1/Jpa/rdbms2catalog/solution</para> + </annotation> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> </chapter>