Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
05724781
Commit
05724781
authored
12 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
next exercise
parent
41c75a34
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/course.xml
+95
-1
95 additions, 1 deletion
Doc/course.xml
with
95 additions
and
1 deletion
Doc/course.xml
+
95
−
1
View file @
05724781
...
...
@@ -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>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment