Welcome back.
We have the to do been.
Let's say we have a to do 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 Add Entity.
If you say the To-Do is an entity, it means that this bean is mapped to a database table.
Let's just add in entity control one input.
Jakarta persistence.
So you'd see that Jakarta persistence not entities imported in.
Whenever you have an entity, you would also need to have ID.
So you need to define primary key.
I'll add in at ID and say control one.
Import it.
Jakarta Persistence.
Let's do that.
And we would also want to generate this using a sequence.
So I'll say generated value.
So think about what we are doing in here.
We are mapping to do been this been to a table in the database.
That's what entities allow you to do.
It allows you to map a bean to a database table.
One of the magic of Springboard auto configuration is that if it sees any entities, then it would automatically
start creating tables in H2.
If you look at our Android XML, we have two database in here.
So let's see where it is.
If you look at Palm that XML, we have a database in here.
And as soon as Springboard sees history 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 Edge to console.
Let's refresh.
Let's re login by connecting.
Make sure that you are using the right JDBC URL and say connect.
Now you can see that there is a To-Do table in here.
So what's happening in here is that Springboard auto configuration.
Whenever it sees H2 or any in-memory database in the class path, it will directly create tables for
all the entities.
When you are defining your entity, you have a lot of flexibility.
Let's say the name of the table.
Let's say I would want to map two is not to do, but let's say I would want to map to something else.
So I would say name is equal to to do ABC and save.
Let's refresh this.
Continue.
Connect.
You can see that the name of the table is not to do ABC.
So we are mapping to a table called to do ABC, to do ABC.
By default it will 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 user name, let's say I would want to give it a different name.
I can say column and give it a name.
The name is equal to a name.
Right now, if you look at this, it's called user name, but if I save this and come back.
To which to cancel, refresh it and connect it back.
You would see that the name is enough name.
So this field is now mapped to this.
JP also supports mapping complex relationships.
One, two, 112.
Many, many to one, many too 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 true and save it out.
And when I actually refresh this, go back and say connect.
I should see to do table in here with the usual column names id description done targeted user name.
So these look good.
One thing you would have observed is target date is target underscore 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 underscore date.
Let's organize, import 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 dot SQL.
So Search Main Resources.
New SQL file.
Data dot SQL.
In the data dot SQL, I can create queries and these queries will be executed at startup.
So I'll say insert into tattoos.
Actually insert into To-Do values.
And you can specify the values in here.
The default generated values 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.
A certified.
And.
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 1001 user username is in 28 minutes or ranga you can insert for any of those
users and use that specific user get certified the target date.
Let's use a hedge to function called current date.
And is it done?
It's false.
And say this.
If you go to console right enough, you'd see an error.
It's saying table two do not found by default Data dot SQL is executed before the entities are processed.
Tables are created when the entities are processed.
So data dot 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.
What properties?
That's something we can configure in application of properties.
So let's open up application of properties.
And type in spring dot JPA dot defer hyphen data source.
Any shirt light zation, make sure that you get the spelling right it springboard JPA defer hyphen data
source hyphen initialization is equal to true and say save.
When I stopped and started the application again, I saw an syntax exception.
It says column in 28 minutes not found.
The reason why it's throwing error is because in data or SQL you should use single quotes.
So let me replace all this with single quotes.
So instead of double quotes, let's use single quotes.
And take this.
Let's see it in action.
Let's start it again.
Enough.
Everything looks hunky dory.
The application has started up fine.
Let's go to page two console refresh and connect again to the table.
Do you have the data?
Yep, We have the data in here.
Let's add a few more tattoos to data skull.
Control a.
So let's add a few more ID should be increased.
You cannot have duplicate IDs.
So 1000.
Three 1004.
I led all the tutus for in 28 minutes itself will be make use of intimidating its ID.
If you want, you can make use of the wrong idea as well.
So let's say get your certified GCP certified and let's say learn full stack or DevOps, whatever you
want to do or learn to dance.
All right, so what do you want to do?
You can put them in here, make sure that the IDs are unique and save them down.
And go back and refresh connect and to do 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 this 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 to DOS connecting
to the H2 database.
We want to use JPA, connect to the database and show the to dos 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.