This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Since all entities share common properties, we should make sure that we add that shared functionality to an abstract parent class.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay.
0:00
So we have some shared functionality
between our entities at the moment.
0:01
Mainly, the handling of how our
system generates unique IDs.
0:04
All entities are going to
identify the same way and
0:07
require that no argument constructor.
0:10
So it sounds to me like what we want to
do is create a class that are entities
0:12
should extend.
0:16
But wait.
0:18
We probably wanna make sure that
nobody creates that superclass.
0:18
I mean, what would that even
map to in the database.
0:21
When you have functionality that you
wanna share but you don't wanna allow
0:24
object creation of that superclass using
an abstract class is the way to go.
0:27
Let's use an abstract
class to solve our woes.
0:31
Okay.
So let's create a new package.
0:34
And it's gonna handle all of
the shared items between our features.
0:36
It'll deal with features
core to our application.
0:40
So the typical name for
this sort of thing is core.
0:42
So let's go ahead and do the same.
0:45
Right?
So we'll make a new Java class.
0:47
We'll make a new package called core.
0:50
And let's name this entity, BaseEntity.
0:52
That should help make things clear.
0:55
Okay, so
now let's add the abstract keyword here.
0:58
Okay, now with this in place,
nobody can create a BaseEntity.
1:03
They must extend it if they want to use.
1:06
They'll get a compile time error.
1:09
Or they'll be prompted to create
an anonymous inner class.
1:10
If they attempt to use
it with the new keyword.
1:13
So the first thing that we need to do,
is to tell.
1:16
J P A that this is a mapped superclass.
1:18
So we'll say mapped superclass.
1:22
And now we can use it
with our other entities.
1:26
Now let's flip over to one of our entities
and grab our duplication perpetrator.
1:29
So here you go, let's grab this ID,
I am gonna go ahead and cut this app,
1:34
wanna flip over to the base entity.
1:38
I'm going to add that and
we will do these imports.
1:40
Here we go and it is complaining
about not having a constructor.
1:45
So let's initialize in the constructor.
1:48
And it automatically did,
1:51
just what we needed, made a protected
base entity, set the ID to null.
1:53
Let's flip back to course and
what we're gonna do is,
1:56
we're gonna say is extends BaseEntity.
2:04
Now unfortunately we still need to have
a no argument constructor in our entity
2:09
classes.
2:14
That's just the J.P.A rules but
what we can do is instead
2:15
of doing this again here
we can just call super().
2:20
This way in case we want
something special to happen in
2:25
all of our entities we can just change
the single constructor in base entity.
2:28
If we want to change
something specifically,
2:33
we can add it after the call
here to super, right?
2:35
We could come in here, and we can add
whatever we want specifically for courses.
2:38
Now the terms super class and
parent class are interchangeable, and
2:41
you'll hear both used.
2:45
In this case, our course entities super
and or parent class is BaseEntity.
2:47
So it's kind of like Jor-El.
2:54
You know, a super parent.
2:55
Sorry that was a dumb Superman's dad joke.
2:57
Okay.
3:00
And now let's do the same thing for
review.
3:01
We'll come in here and we will
3:05
come in here and
we will get rid of the ID, and
3:12
we will make a review called super.
3:16
And make it extend BaseEntity.
3:23
Here we go.
3:29
All right so now that we've got that
all out of the way, what do you
3:31
say we spin up our server and make sure
our entities are working as we expect.
3:34
So let's go ahead and make a new class
3:37
in the root here right
under com.teamTreeHouse.
3:41
Let's make a new class called Application.
3:45
The name doesn't matter.
3:47
So first things first, we will mark
this as a SpringBootApplication.
3:51
And we were going to add public
static void main and for
3:58
the body we're going to call
SpringApplication.run and
4:03
pass in our class, and pass in whatever
our args were passed to the main function.
4:08
This is SpringApplication.
4:15
Okay, so if we go ahead now and run,
4:18
we try to run this,
we will see everything.
4:21
Wait a second, you say,
we haven't made any controllers yet
4:28
that will respond to these actions.
4:32
Well get this,
our repositories are exposed.
4:33
Look, check this out in the log here.
4:36
Scroll this up here.
4:39
Scroll over.
4:41
See this mapped slash repository ID?
4:42
It went ahead and
built things when our app started up.
4:46
So simply by having repositories around,
routes got created and it will respond.
4:50
Pretty cool right?
4:55
Now, if you've never used a restful
AP before you might not know
4:56
how to go about discovering these routes.
5:00
One emerging standard is to have
your API respond with HAL or
5:02
Hype media Application Language.
5:07
Well by default spring date
arrest works with HAL formats.
5:09
So in order to discover our API
all you do is hit the route and so
5:12
let's see the route is
mapped here at 8080.
5:17
Okay, so, let's go ahead.
5:20
And we'll come over here and we'll go
to localhost:8080 and forward slash.
5:22
So if you look here we have these links
defined and its underscore links.
5:32
So that's that's a HAL format to
make sure that you know that this is
5:38
coming from HAL.
5:41
And there's more on HAL in
my teacher's notes, and
5:42
we'll explore it as we go through, but
5:43
they've given you links to
the resources that are available.
5:45
Hey look, there's courses and
5:48
reviews, these are representing
the repositories that we built.
5:50
By default,
it's using a plural version of the entity
5:53
that's being represented
by the repository.
5:56
That's pretty cool, right?
5:58
It automatically figured out that course
was courses and reviews was reviews or
5:59
review was reviews.
6:03
Pretty cool, isn't it?
6:04
We'll go over and
change this if it's wrong but
6:06
first let's navigate to the /courses.
6:09
Let's see what happens.
6:11
Courses is empty.
6:18
It's not very fulfilling without data,
is it?
6:20
We should do something about that.
6:22
Nice job on getting the abstract
entity all set up and
6:24
removing that code duplication.
6:26
It's pretty impressive that
the API just worked, isn't it?
6:28
Now I know we need to get some data in
there to make you even more impressed.
6:31
But one thing that you might have caught
from those mappings that we saw in the log
6:35
at boot up is that we can
actually send a POST request and
6:38
this will create a new course.
6:41
But before we dive into that work,
6:43
let me show you a pretty great way
to prepopulate your app with data.
6:44
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up