[MUSIC] In Java, there's a tool that we can use to help us go from objects down to the database, search the database and then return that data back as objects to our code. In this tool is a framework called JPA, or the Java Persistence API. And so, the idea behind JPA is that we go and annotate our objects using Java annotations and tell JPA about how to store our objects, in a database. So we go and we add these annotations to our objects. And then JPA can automatically look at our objects and look at those annotations, and figure out the appropriate way to turn our objects automatically into rows and columns and tables within a database. So JPA uses this information to marshal or map our objects down into the underlying database. And then whenever we want objects back, it uses this same information to help us get objects back out of the database. So JPA is an incredibly useful tool and will allow us to write less code and know that our mapping will hopefully be done 100% correctly all the time. So we don't have to worry about making errors and how we can convert these objects. And in fact, what you'll see is with a very small amount of code, we can begin utilizing the database in our cloud services. So there's not a huge amount of work or things that we have to learn in order to be able to leverage JPA. With some very simple annotations, we can start immediately taking our objects and storing them in a database. Let's take a look at JPA and how it can help us to store information. So, let's think about our, our video service. So far, we have a cloud service where users using their mobile clients can upload videos to the service and then it stores objects, video objects representing those uploaded videos in a list. Now, this is a very simple approach because as long as that service is in memory, as long as it's running, as long as the service, the server that it's on hasn't been turned off, we have access to all of those video objects and we can see what has been uploaded so far. But the moment somebody stops our cloud service for accepting these videos or the moment that the server that it's running on gets turned off, all of the information was in memory, that list of video objects, is suddenly gone. And the next time we start up our cloud service, we don't have access to those video objects that we previously created. So we need some way of creating instances of an object and then telling JPA to go and save those objects for us so that in the future, if the service gets turned off or the server gets shut down, when we start back up, they will have been persisted in the database and still accessible to us. So what we're going to do is create a simple video object and show you how easy it is to actually store instances of an object in a database. Now, the way that we do this is we have to create a class and provide annotations to JPA explaining how instances of that class should be stored in a database. So let's take a look at a simple example. So we're going to create a class to represent the videos that we are storing. So we'll have a simple video class. And what we'd like to do is store some information in the member variables in this class. So we might have private. And we'll just fix that. Long because we're going to store a whole bunch of these things. We're hoping our service will be like YouTube. ID. So we're going to have an ID for each of our videos. That's going to be a numeric ID. And then we'll store, let's just say to be simple for the moment, a string, which is the name of the video. Now we could go and add a whole bunch of other member variables here, but this is just a simple example, so let's start with this. Now, what we need to do is we need to tell JPA that we want it to store instances of this class in the database for us. And the first thing we have to do is we have to declare that this is a type of object or a type of class that is something that can be stored. And the way that we do that is by simply adding the @Entity annotation from JPA to the class. And what this Entity annotation does is it says this is something that can be persistent. Now the second thing we need to do is since we want to be able to search for these objects after we've stored them and we need a way of accessing them and referencing them, we need to give them an identity. So normally, when we go and create objects, they get stored at some memory address and we don't have to worry so much about each one of them having a unique in, you know, ID. We just know that underneath the hood, Java provides a unique memory address where each one of them is stored. But we need to do something equivalent so that we can know and address each of these video objects once they've been stored individually and that we need to do that with a unique ID. So to do that, we simply pick one of our properties that we know is going to be unique or we would like to be filled in with a unique value, and we say @Id. So this is telling JPA that this ID right here is going to be our ID property. Now, we could've had any member variable with any naming provided for the ID so long as it's of a type like a string or a long that JPA knows how to use as a unique identifier. And it doesn't have to be named ID. I just happened to name mine ID in this purpose, in this example, but we could have named it, you know, video number or anything else as long as we have this @Id annotation. The @Id annotation tells JPA that this is a property or a member variable that we want to use to uniquely identify this object. But we still have the problems, we have to make sure that every time this member variable is set, that it gets provided a unique ID. Now, we could go and try to manually figure out how to assign these IDs to make sure they're unique. But it would be easier if we could just ask JPA to create unique IDs for us on the fly so that we don't have to worry about it. And so, that's what we're going to do. We can add a second annotation, and I'm just going to move over here to the left, because this is going to be a long annotation. And don't worry if you can't remember all of these longer annotations. They're easy to Google or to look up in your [INAUDIBLE] previous code, sorry about that, to figure out what you should add. So we're going to add @GeneratedValue and then we're going to say strategy equals and then, I apologize if this runs off the screen and hopefully I'll be able to get it in the screen. Generation type.auto and if I got really lucky, all of that stayed on the screen and hopefully I did. But what this extra annotation that I just added says is basically, @GeneratedValue, meaning, this is something I'm expecting JPA to fill in a value for. I'm expecting it to generate a value for this. And then the strategy is I want it to automatically generate the value that should go in here. And if you're having trouble reading this annotation because of my handwriting, the code samples that are provided along with this lecture will make this much clearer about what this looks like and how it works. But at this point, this is all we actually have to provide in order for JPA to be able to persist instances of this video class. All we have to do is construct a video class and very importantly, just assign the name. Because we don't want to assign the ID because we're asking the JPA infrastructure to go and assign that ID for us. We just want to assign the name. So we create a video object, maybe we have some getters and setters. We assign a value for name and then we ask JPA to go and store this object. And two things are going to happen. One is JPA is automatically going to take this object and convert it into the tables and the rows and columns of our database, or depending on the actual database implementation, maybe it does something completely different and we don't have to worry about that. And two, after it's stored it and it's figured out its unique identifier in the database, it's going to generate a unique ID for us and assign it to the ID so that we don't have to do that. So, it's not only going to from these three simple annotations, these are actually two related annotations, but from these three simple annotations, it's not only going to store the objects in a database for us, but it's going to give us a way of uniquely identifying them and referencing them in our code. Which we'll see is very helpful when we want to query and look up data that's been previously stored.