|The Insider's Guide to the Java Web Developer Nanodegree - 4|
|Written by Nikos Vaggalis|
|Monday, 21 September 2020|
Page 1 of 2
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:
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:
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
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 :
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:
|Last Updated ( Monday, 21 September 2020 )|