Instructor: Welcome back.
We have the Todo bean.
Let's say we have a Todo table in the database.
JPA allows us to map your bean to the database.
The way you can do that
is by adding a simple annotation called @Entity.
If you say the Todo is an entity
it means that this bean is mapped to a database table.
Let's just add in entity, Ctrl + 1,
input jakarta.persistence.
So you'd see that jakarta.persistence.Entity is imported in.
Whenever you have a entity
you would also need to have a ID.
So you need to define primary key.
I'll add in @Id and say Ctrl + 1,
import ID jakarta.persistence.
Let's do that.
And we'd also want to generate this using a sequence.
So I'll say @GeneratedValue.
So think about what we are doing in here.
We are mapping Todo bean, this bean,
to a table in the database.
That's what entities allow you Todo.
It allows you to map a bean to a database table.
One of the magic of Spring Boot auto-configuration
is that if it sees any entities
then it would automatically start creating tables in H2.
If you look at our pom.xml, we have H2 database in here.
So let's see where it is.
If you look at pom.xml, we have H2 database in here.
And as soon as Spring Boot sees H2 database
what it does is it would start pre-configuring it.
One of the pre-configuring steps is if it sees any entities,
if it sees any entities like this
it would directly start creating tables for them.
Let's see that in action. Let's go to H2 console.
Let's refresh. Let's re-log in by connecting.
Make sure that you're using the right JDBC URL
and say connect.
Now you can see that there is a Todo table in here.
So what's happening in here
is that Spring Boot auto-configuration, whenever it sees H2
or any in-memory database in the class path
it'll directly create tables for all the entities.
When you're defining your entity
you have a lot of flexibility.
Let's say the name of the table I would want to map to
is not Todo, but let's say I would want to map
to something else.
So I would say name is equal to TodoABC and save.
Let's refresh this.
Continue. Connect.
You can see that the name of the table is now TodoABC.
So we are mapping to a table called TodoABC.
By default it'll use the class name itself.
I'll remove this.
You also have flexibility
in terms of customizing the column names as well.
Instead of calling this USERNAME,
let's say I would want to give it a different name.
I can say column and give it a name.
A name is equal to name.
Right now if you look at this, it's called USERNAME.
But if I save this and come back to H2 console,
refresh it and connect it back,
you would see that the name is now name.
So this field is now mapped to this.
JPA also supports mapping complex relationships.
One-to-one, one-to-many, many-to-one,
many-to-many and things like that.
For now, let's remove the column.
So I'll remove this column name is equal to name.
I would want to continue using the default values
which are generated.
Make sure that the entity name is also Todo and save it out.
And when I actually refresh this, go back and say connect,
I should see Todo table in here with the usual column names.
ID, DESCRIPTION, DONE, TARGET_DATE, USERNAME.
So these look good.
One thing you would observe is TARGET_DATE.
The reason is because there is a init cap in here.
So this is in caps.
So what happens is in a database typically
we separate words using underscore.
We capitalized D in here,
that's why the column name is created as TARGET_DATE.
Let's organize imports and save this.
Now I would want to populate some data to start off.
So how can I populate some data to start off?
I can create a file called data.sql.
So src/main/resources, new, SQL file, data.sql.
In the data.sql I can create queries
and these queries will be executed at startup.
So I'll say insert into todos.
Actually, insert into todo.
Values, and you can specify the values in here.
The default GeneratedValues start with one,
so I'll use 10,001 as the ID.
The second one is DESCRIPTION.
I'll say learn, or you can say Get AWS Certified.
The next one is DONE, which is false.
The order of these does not look good.
So what I do is I would define my own columns in here.
So I would want to insert in the right order.
So I would want to define my own thing in here.
So I would say ID, first ID, then DESCRIPTION,
then I would want to insert...
Actually, before DESCRIPTION we need to have the USERNAME.
ID, USERNAME, DESCRIPTION,
after that would be the TARGET_DATE.
And the last one is DONE.
So let's insert values in the same way.
So let's maximize this.
And 10,001, username is in28minutes or Ranga.
You can insert for any of those users
and use that specific user.
Get AWS certified.
The target date, let's use a H2 function
called current date.
And is it done? It's false.
And save this.
If you go to console right now, you'd see an error.
It's saying table TODO not found.
By default data.sql is executed
before the entities are processed.
Tables are created when the entities are processed.
So data.sql is executed before the table is created
and we would want to change that.
How do we change that?
That's where we can make a change
in the application.properties.
That's something we can configure in application.properties.
So let's open up application.properties
and type in spring.jpa.defer-datasource-initialization.
Make sure that you get the spelling right.
It's spring.jpa.defer-datasource-initialization
is equal to true, and say save.
When I stopped and started the application again
I saw a syntax exception.
It says column in28minutes not found.
The reason why it's thrown an error
is because in data.sql you should use single quotes.
So let me replace all these with single quotes.
So instead of double quotes, let's use single quotes.
And save this.
Let's see it in action now, let's start it again.
And now, everything looks hunky dory.
The application has started up fine.
Let's go to H2 console, refresh and connect again.
TODO table, do you have the data?
Yep, we have the data in here.
Let's add a few more todos to data.sql.
Ctrl + A. So let's add a few more ID.
Should be increased. You cannot have duplicate IDs.
So 10,003, 10,004.
I'll add all the todos for in28minutes itself.
We'll be making use of in28minutes ID.
If you want, you can make use of the Ranga ID as well.
So let's say get Azure certified, GCP certified,
and let's say learn full stack or DevOps,
whatever you'd want to do.
Or learn to dance, right?
So whatever you'd want to do, you can put them in here.
Make sure that the ideas are unique and save them.
And go back and refresh, connect, and TODO, run.
One thing you need to remember
is that even H2 is an in-memory database.
When you restart the application,
all the data in H2 will be lost.
In the step, we got the H2 database ready.
We have the table ready, we have the data ready
and now we would want to display these todos
connecting to the H2 database.
We'd want to use JPA, connect to the database,
and show the todos in here.
And we would want to use JPA
to perform all the operations as well.
Delete, update, add, and a wide variety of them.
Let's get started with that starting the next step.