The Insider's Guide to the Java Web Developer Nanodegree - 4
Written by Nikos Vaggalis   
Monday, 21 September 2020
Article Index
The Insider's Guide to the Java Web Developer Nanodegree - 4
Persistence and Connection

Another installment of our ongoing in-depth coverage of the new Udacity Nanodegree program that will enhance your career prospects as a Java web developer. At last we get to play in the big league with the Java Persistence API and Hibernate.

Because there's always a confusion around JPA vs Hibernate, the first thing we learn in Data Stores and Persistence is that the Java Persistence API (JPA) is the specification and that Hibernate is an implementer of it; just like in OOP where a concrete class implements an interface.

SQL and database design are, however, not part of this lesson and we'll be looking at databases purely from a Java perspective.That is. the lesson adopts a Code First approach to database design, where you create your entities and map their relationships in code and have Hibernate use it to generate the database schema. It is an approach that is easier to get started with,and doesn't require much knowledge of database design and drawing Entity Relation Diagrams, ERDs.

So what is JPA in the first place - it is Java's ORM specification for storing and accessing object data to and from a database; mapping Java classes to database tables, performing queries between objects and propagating these actions to the database entities through mapped relationships.The key here is the concept of the Entity. Entities are used to negotiate between the object representation of data in Java and the table representation of data in a database.


In Lesson 2: Data in Multitier Architecture, we start to learn about the basics of the Entities and their Identifiers (primary keys in database terminology) and how to map relationships between them through the use of annotations like @OneToOne, @OneToMany,,etc.

While database design is out of the scope of the lesson, you really can't skip the basic concepts. Therefore the instructor makes a decent attempt to go through them by skimming the broad details of an otherwise tough topic, which to be fair would requires a full course to be dedicated to it.

When decorating a relationship as e.g. @OneToMany the corresponding mapping is taking place in the database in joining the tables with foreign keys. But the Object Relational Model and Hibernate go one step beyond by also allowing one- and two- way relationships: 

  • Unidirectional - Association specified on one side of the relationship only: 

    Doesn't retrieve data you won’t use.
    Should use Set collection type for most efficient SQL.

  • Bidirectional - Association specified on both sides of the relationship.:

    Use mappedBy on the containing Entity side.
    Access both sides of relationship with a single query.


Other ORM induced intricacies not found in pure ERDs are the @ElementCollection annotation with which you denote an association between a single Entity and a list of values that are not themselves Entities. For example:

public class Person {
 private Long id;

 private List<Outfit> outfits;

/* rest of class */

These embeddables will be stored in a separate table, along with the ID of the Entity in which they are contained.

And Inheritance, a way to share data and associations across multiple related classes

public abstract class Humanoid {
 Long id;

 @OneToMany(mappedBy = "humanoid")
 List<Outfit> outfits;

/* getters and setters */

public class Person extends Humanoid {

 private String name;

 private int age;

 @Column(name="composer", length=512)
 private String favoriteComposer;

/* getters and setters */

Wrapping up the Entities topic, we move on to the way we incorporate them in our applications' design. Since Entities translate our data from the application to the database, we must position them in between the Application Layer and the Database.


This is the incentive that gets us into the mindset of Multitier design and its advantages : 

  • Maintenance
    Centralizes access to your data source
    Reduces time needed to make changes to Entity interactions
    Reduces amount of code each developer needs to understand
  • Performance
    Allows application layers to easily be separated into modules
    Reduces application size
    Enables scaling of independent components
    Supports future architecture deployment
  • Security
    Able to secure each tier with different permissions
    Reduces redundant authentication in other tiers 

The lesson closes by looking at two related concepts: JSONView Annotations and Data Transfer Objects (DTOs). Both take the responsibility for extracting the data our presentation layer needs out of the Entity and pass them through our Controller to the client.

The scheme goes something like this:

From client to server:


From server to client:


To sum up lesson 2, it had a lot going on. We've learned about: 

  • Multitier Architecture
  • Java Persistence API (JPA)
  • Hibernate
  • POJO or "Plain Old Java Object"
  • Entity Types
  • Value Types
  • Basic Types
  • Serialization
  • Embeddables
  • Entities
  • Unidirectional
  • Bidirectional
  • OneToOne
  • OneToMany and ManyToOne
  • ManyToMany
  • Inheritance
  • Single Table Inheritance
  • Polymorphic Query
  • Joined Inheritance
  • Table Per Class Inheritance
  • Mapped Superclass
  • Data Transfer Objects (DTOs)
  • JSONView Annotations

Last Updated ( Monday, 21 September 2020 )