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

Modified exercise

parent 2a536d14
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -361,63 +361,15 @@ CREATE TABLE `Lecture` (
<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>'s 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>
<para>Since <classname>Description</classname> instances are
composites with respect to <classname>Product</classname>
you might want to use @ElementCollection in favour of
@ManyToOne / @OneToMany.</para>
</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>
<para>Test your implementation by uncommenting the test
method's body in
<classname>rdbms2catalog.TestSchema</classname>.</para>
</listitem>
<listitem>
......
......@@ -27,16 +27,13 @@ public class TestSchema {
// accordingly the following commented code should work.
// final Product monkeyPickedTea = new Product(1, "Monkey Picked Tea", null);
// new Description(monkeyPickedTea, 0, "Picked only by specially trained monkeys");
// new Description(monkeyPickedTea, 1, "Rare wild Chinese tea");
// monkeyPickedTea.getDescriptions().add(new Description("Picked by specially trained monkeys"));
// monkeyPickedTea.getDescriptions().add(new Description("Rare wild Chinese tea"));
//
// final Product instantTent = new Product(2, "4-Person Instant Tent", 15);
// new Description(instantTent, 0, "Exclusive WeatherTec system.");
// new Description(instantTent, 1, "4-person, 1-compartment tent");
// new Description(instantTent, 2, "Pre-attached tent poles");
//
// em.persist(monkeyPickedTea);
// em.persist(instantTent);
// instantTent.getDescriptions().add(new Description("Exclusive WeatherTec system."));
// instantTent.getDescriptions().add(new Description("4-person, 1-compartment tent"));
// instantTent.getDescriptions().add(new Description("Pre-attached tent poles"));
transaction.commit();
}
......
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