Professional Documents
Culture Documents
Week 4 - Server-Side
Week 4 - Server-Side
External data
sources
Gerralt Gottemaker
2020 - 2021
<h1>Today’s subjects:</h1>
Web Applications <ul>
week 4 <li>Revision of MVC</li>
Adding a (remote) data <li>Introduction to Hibernate</li>
source to our APIs.
<li class=“diy”>Adding data source</li>
<li>Entities and Repositories</li>
<li class=“diy”>CRUD data source</li>
<li>Custom queries</li>
<li class=“diy”>Deeper dive</li>
</ul>
Controller sends Controller requests
MVC model data back to the view Controller information from
a service
Model
Controller sends Controller requests
MVC model data back to the view Controller information from
a service
Model
Hibernate is an ORM framework that helps with abstracting
your data from your code.
Introduction to
Hibernate ORM: Object/Relational Mapping
What is Hibernate? - Makes sure (Java) Objects are mapped to a Relational
database
- Sounds easier than it is!
- It uses, just as Spring and Jackson, Java reflection
Model
Services
Introduction to
Hibernate
Let’s zoom in a bit…
Hibernate
Repositories
Config Entities
Model
Services
Introduction to
Hibernate
Let’s zoom in a bit…
Hibernate
Repositories
Config Entities
!
Which database you want to
use, doesn’t matter for Model
Hibernate. In this course, we are
using PostgreSQL.
Hibernate can be added to your project in two ways.
Config
We need to configure Hibernate (and Spring) to make sure it
knows what database to use.
Introduction to
Hibernate Let’s say there is a database demo_db with the owner demo
Configuring Hibernate (without password).
Services
Hibernate
Repositories
Config Entities
Model
We start off with a (simple) Java class.
Entities @Entity
public class Cat {
Simple example
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Column
private String gender;
@Column
private int age;
Entities @Entity
Table (cat) public class Cat {
@Id
ID, will be automatically @GeneratedValue(strategy = GenerationType.IDENTITY)
generated private Long id;
@Column
Column (name)
private String name;
@Column
Column (gender) private String gender;
@Column
Column (age) private int age;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "custom_name")
private String name;
...
}
!
@Column annotations are optional if
default names are used. It is good
practice however to add them anyway
(readability, showing intentions)
Let’s add a class
Entities @Entity
public class Person {
One-to-many relationships ...
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<Cat> cats;
...
}
@Entity
public class Cat {
...
}
Do It Yourself time! Creating your Entities
Services
Hibernate
Repositories
Config Entities
Model
A few steps.
Repositories You’ve seen Java generics before. For example, when creating an
ArrayList and you tell it what type it holds. Or, when you created a
Simple example Comparable and you told it what to compare itself to.
// Checks
Standardiffunctionality
a row with id exists in the cat table
boolean exists = this.catRepository.existsById(id);
// Will find one row. Returns an Optional object, so it either holds 1 or 0 object(s).
// use cat.isPresent() and cat.isEmpty() to check if it's there or not.
// If it's there, use cat.get() to actually grab it.
Optional<Cat> cat = this.catRepository.findById(id);
// Saves a cat.
// If no ID is given, this will create a new row
// If an ID is given, it will update the existing row
// Will return the newly saved cat, including (new) id.
Cat cat = this.catRepository.save(cat);
Repositories We can do that the same way we added our Service to our
Controller.
Usage
1. Create a (private final) instance variable of the type
CatRepository
2. Create a CatService constructor that takes the CatRepository
as a parameter and assign it to the instance variable.
3. You are now ready to use the CatRepository in your Service!
@Service
public class CatService {
Custom methods for There are two ways of adding custom queries:
custom queries 1. Using the @Query annotation above your custom method
@Query("select c from Cat c where c.gender = :gender")
List<Cat> yourOwnName(@Param("gender") String gender);
!
The query isn’t really SQL, because they try to make it database
independent. The language follows a mix of SQL and object-
oriented notations.
2. Using the Hibernate method naming conventions
List<Cat> findAllByGender(String gender);
Repositories
!
Usually, it’s good to override the default findAll() method so it
returns a List instead of an Iterator. Iterators are harder to work
with than Lists, so this one line of code saves you from a lot of
trouble.
@Repository
public interface CatRepository extends CrudRepository<Cat, Long> {
...
}
Do It Yourself time! Creating your Entities
Config Entities
Model
That’s all folks!
What’s next?
This week:
• Get familiar with the usage of Repositories and its custom
methods
• Expand the TODO API with a database (homework assignment)
Next week:
• Working on our own project (final assignment)