Spring Boot Diary: Query-less Queries – Part 1

No Comments

So database! We need a database almost in every application, it’s the place where we store our data in order to read them later on. Spring Boot and Spring Data JPA make a perfect combination for database operations, which means, they do everything on your behalf.

So let’s start by creating a project, or asking Spring Boot to do it for you, head over to http://start.spring.io and generate a project with the dependency JPA. JPA means, Java Persistence API, it is a specification that handles relational database stuff in Java. JPA relies on ORM which is object-relational mapping, it’s like, you represent database tables as Java classes, and database records as instances of these classes, and instead of using queries to access the database, you use an API that does all the dirty work for you.

 

 

Import your project into your favorite IDE, and you check that beloved single class that Spring Boot created for you, Application.

You are probably wondering now, what about the database? shouldn’t I create the tables first?

Nah nah nah, forget all that shit.

Here’s how you do it, instead of creating a table, just create the class representing that table, and Spring Boot (Hibernate actually) will create the table for you, see how beautiful it is?

So let’s create our first class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.alibassam.springbootdiaries.part2;
 
import java.util.Date;
 
import javax.persistence.Entity;
 
import org.springframework.data.jpa.domain.AbstractPersistable;
 
@Entity
public class Developer extends AbstractPersistable<Integer>{
 
    private String name;
    private String email;
    private Date birthDate;
    @OneToMany
    private List<ProgrammingLanguage> usedLanguage = new ArrayList<ProgrammingLanguage>();
 
    public Developer() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public Date getBirthDate() {
        return birthDate;
    }
 
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
 
    public List<ProgrammingLanguage> getUsedLanguage() {
        return usedLanguage;
    }
 
    public void setUsedLanguage(List<ProgrammingLanguage> usedLanguage) {
        this.usedLanguage = usedLanguage;
    }
}

It’s nothing but a simple plain old java object, oh that’s catchy, let’s call it POJO! We add that little @Entity annotation just so we can say that this class represents a table. If someone is wondering what AbstractPersistable is, it’s just a beautiful class created by:

Oliver Gierke and Thomas Darimont and Mark Paluch from Spring Team. It simply contains the @Id column which is auto generated in case you’re too lazy like to me to mention it in every class where you need it.

Ok now let’s create another class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.alibassam.springbootdiaries.part2;
 
import javax.persistence.Entity;
 
import org.springframework.data.jpa.domain.AbstractPersistable;
 
@Entity
public class ProgrammingLanguage extends AbstractPersistable<Integer>{
 
    private String name;
 
    public ProgrammingLanguage() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}

We will have some developers who write code in different programming languages.

Ok so what now? What about the database? Where is it? How to connect to it? How to tell this thing to create the tables for us?

Nah, nah and nah, forget all that.

Spring Boot does eveeeeeeeeeeeeeerything for you.

You only need to tell Spring Boot which database you wanna use, of course that decision is kept for you, so how do we tell Spring Boot?

Well all you need to do is to add a dependency for the driver of that database of yours and just specify where it is on the network. So we have MySQL, Oracle, IBM DB2, Microsoft whatever, and lots of other choices. Do you really feel like installing a database now? No? Good boy/girl, neither do I. So let’s use an embedded database, which is an in-memory database that is created when the application is started and gets destroyed when the application is terminated, it’s good for testing and for lazy people like me.

Let’s use H2 database so just add the dependency in your POM:

1
2
3
4
5
<dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 <scope>runtime</scope>
</dependency>

Oh and no need to define any database related configurations for H2, Spring Boot does everything for you.

Before running the application, let’s add these lines in application.properties to tell Spring Boot to be a little bit generous concerning database activities inside the log.

1
2
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Now start the app, and let’s check the log together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
HikariPool-1 - Starting...
HikariPool-1 - Start completed.
Building JPA container EntityManagerFactory for persistence unit 'default'
...
Using dialect: org.hibernate.dialect.H2Dialect
drop table developer if exists
drop table developer_used_language if exists
drop table programming_language if exists
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start with 1 increment by 1
create table developer (id integer not null, birth_date timestamp, email varchar(255), name varchar(255), primary key (id))
create table developer_used_language (developer_id integer not null, used_language_id integer not null)
create table programming_language (id integer not null, name varchar(255), primary key (id))
....

As you can see, the application created 3 tables, developer, programming_language, and developer_used_language as well as a sequence for the Id column. How cool is that?

 

Categories: Java, Spring

Leave a Reply