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

next exercise

parent 41c75a34
No related branches found
No related tags found
No related merge requests found
......@@ -18136,7 +18136,101 @@ public class User {
<section xml:id="sect_MappingComponents">
<title>Mapping components</title>
 
<para/>
<para>We consider a simple example. We may add an email property to
<classname>session3.User</classname>:</para>
<programlisting>...
public class User {
...
private Email address; ...</programlisting>
<para>Why do we use a separate class Email rather than a simple
<emphasis role="bold"><code>private String email</code></emphasis>
declaration? The answer is quite simple: We want Email instances to
be extensible and allow for method definitions like
<code>sendEmail(...)</code>:</para>
<programlisting>public class Email {
private String emailAddress;
...
void sendEmail(final String subject, final String content) {}
}</programlisting>
<para>Our <code>Email</code> class may of course have more than just
one property. We don't want to email addresses to be database
entities themselves. Instead they are meant to be components of User
instances. This is achieved by:</para>
<glosslist>
<glossentry>
<glossterm>Annotate class Email to be embeddable:</glossterm>
<glossdef>
<programlisting>package component.email;
<emphasis role="bold"> @Embeddable</emphasis> public class Email {
private String emailAddress;
...
}</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Annotate <code>emailAddress</code> to become an
embedded property:</glossterm>
<glossdef>
<programlisting>package component.email;
...
public class User {
private Email address;
<emphasis role="bold">@Embedded</emphasis>
public Email getEmailAddress() { return address;}
...</programlisting>
</glossdef>
</glossentry>
</glosslist>
<para>We may now persist <classname>component.email.User</classname>
instances:</para>
<programlisting>package component.email;
...
public class PersistUser {
...
{
final Transaction transaction = session.beginTransaction();
final User u = new User(123, "goik", "Martin Goik");
u.setEmailAddress(<emphasis role="bold">new Email("goik@hdm-stuttgart.de")</emphasis>);
session.save(u);
transaction.commit();
} ...</programlisting>
<qandaset role="exercise">
<qandadiv>
<qandaentry>
<question>
<para>Consider the following sketch of an address
class:</para>
<programlisting>public class Address {
private String street;
private String city;
private String zipcode;
...
}</programlisting>
<para>Extend class <classname>session3.User</classname> to
allow for two properties <code>homeAddress</code> and
<code>workAddress</code> of type Address.</para>
</question>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</section>
 
......
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