This course will be retired on July 14, 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
In this video we'll finish up creating our Entities with the PizzaTopping Entity!
Related Links
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
We're almost done creating our entities.
0:00
All we've got left is one
combining pizzas with toppings.
0:02
The first step to creating
an entity is to create a class.
0:05
To get started, let's create a new pizza
topping class inside of our data package.
0:09
And make sure to pick Class.
0:15
Then, just like our other entities,
let's make this a data class.
0:18
And declare our columns,
a parameters to the constructor.
0:23
So instead of brackets,
we want parentheses.
0:28
And inside the parentheses,
let's use a val to declare
0:33
pizzaId as the first parameter,
and make it an Int.
0:38
Then let's use another val,
to declare toppingId
0:43
as the second parameter, and
let's make it an Int as well.
0:49
Great, now let's add the @Entity
annotation above the class.
0:53
And all that's left is to
declare the relationships.
0:59
We'll need to specify the primary key,
as well as both foreign keys.
1:03
Let's start with the primary key.
1:07
In the previous video,
1:10
we declared a primary key by
using the @primarykey annotation.
1:11
However, this doesn't work if your
table has more than one column in its
1:16
primary key.
1:20
For situations like this
you have to declare
1:22
the primary key up with the entity.
1:25
Let's add parentheses
after the word entity, and
1:27
let me just quickly hide the side pane.
1:30
And then let's look at some of
the options we can declare.
1:36
By default,
1:39
each table in the database will be named
exactly like its corresponding entity.
1:40
However, if you'd like the table
to have a different name,
1:46
you can do that by specifying
the table name property.
1:49
In addition to the table name, it looks
like there's also entries to let us
1:52
specify multiple primaryKeys,
and multiple foreignKeys.
1:56
Let's set up the primaryKeys by typing,
2:02
primaryKeys = arrayOf.
2:09
And inside the array, we just need
to add the names of the columns.
2:13
PizzaId.
2:18
And toppingId.
2:22
Also inside annotations, and
only inside of annotations,
2:25
we're allowed to declare
a raise using brackets.
2:31
Let's use alt+enter on
arrayOf to replace this call
2:36
with an array literal,
which are the brackets, awesome.
2:41
Now, to add a comma at
the end of our array.
2:47
And then add a new line for
setting up our foreign keys.
2:51
Let's type foreignKeys=, and remember it
needs an array of foreign key objects.
2:55
Let's add the brackets for the array, and
then create a new foreign key by typing,
3:01
[(ForeignKey)], and
hitting enter to accept auto complete.
3:08
Which for some reason,
gives us an extra couple of parentheses.
3:13
Let's get rid of those.
3:18
And then look at which
parameters we need to provide.
3:23
We’ll need to specify the first three.
3:30
The entity the foreign key represents,
the name of the column and
3:32
the parent table, and
the name of the column and this table.
3:36
Referred to as the child columns.
3:40
Also, even though in this case we're only
matching one column with one other column,
3:42
it's possible to have a foreign
key involving multiple columns.
3:48
So instead of just using a string,
3:51
we'll need to specify each of these
columns as an array containing a string.
3:54
Okay, let's start with
the pizza foreignKey.
3:59
Its entity is the pizza class.
4:02
So, let's set entity = pizza:: class,
4:04
parentColumns = to an empty array.
4:13
The parent column is just
the name of the column that
4:22
the foreign key is referencing.
4:24
Since the pizzaId column corresponds
to the id column in the Pizza table,
4:26
that's the one we're looking for.
4:31
Let's add id to our parentColumns array,
and then move on to child columns.
4:33
Which is just our pizzaId column.
4:40
So let's add a comma, and
then set child columns
4:42
equal to an array containing
pizzaId as a string.
4:47
All right, all we've got left
is to specify our foreignKey
4:53
to the topping table.
4:57
I'm gonna add a bit of spacing to
make it easier to see everything.
4:58
Then let's copy our pizza foreignKey,
5:08
add a comma to the end of the line,
and then paste it on the next line.
5:12
Now we've just gotta
change pizza to topping,
5:20
and a pizzaId, to toppingId,
and there we go.
5:25
The pizza topping table is finished.
5:30
Pretty cool, right?
5:34
We've created a bunch of tables, but
we haven't had to right any SQL.
5:35
Thinking back to the last video,
once we've created the end of these,
5:39
it's time to create the dows.
5:43
Coming up, we'll take a look
at how to query our tables
5:44
by using data access objects.
5:47
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