‫In this video we're going to do is set up a 1-to-1 relationship.
‫And you don't see the one ones use that much actually out there in the wild.
‫But they are good for certain situations where you are going to have one entity related to another entity
‫and there's only going to be literally a 1 to 1 relationship.
‫And typically you could use case here, I'm doing an order approval.
‫So the use cases that someone is going to go in and approve the order.
‫So they're good for tracking like events and things like that where you don't want to necessarily add
‫in those properties to that entity, but you want some type of tracking entity for like a one time event
‫that it's one typical use case for that.
‫But again, you don't see the one one's that much.
‫That's kind of a little bit of an odd use case.
‫But there there are places for them.
‫So in this case, we are going to go ahead and set up order approval entity.
‫So you can see on screen there, I do have the scale and bring in migration script to update the database.
‫So we are going to create a table called Order Approval that is going to have my D value approved by
‫just a string value, therefore approved by create date and last modified date.
‫And then we are going to alter the order header table to have the order approval ID.
‫And likewise we are going to set up a foreign key constraint there.
‫So we keep the database tidy.
‫So I'm going to go ahead and start creating the java components for this.
‫So I'm going to come into the domain and create a new Java class.
‫It's going to be order approval.
‫Go ahead.
‫Add that.
‫That extends the base entity.
‫And of course, we need to annotate this as an entity as well.
‫So I'm just going to add an account called approvedBy like so, then we'll go ahead and use IntelliJ to generate
‫the getters and setters.
‫So pretty simple entity class.
‫Now I want to come into order header so, I can come in here and add and
‫So I can see that IntelliJ saying it's not happy about that.
‫Here we want to say.
‫OneToOne.
‫And of course, we're going to need to add in the getters and setters for that as well.
‫So that generates the necessary getters and setters that sets that up.
‫Let's go ahead add and a repository for it as well.
‫Let's going to extend out the JPA repository.
‫And that sets up our repository as well.
‫Now let's go ahead and we'll generate a test for this.
‫So I'm going to come in here to the order header repository test and the save order here.
‫We can go ahead and work with that.
‫So let's come up here.
‫Add in the order approval repository.
‫So use the full name just its for consistency.
‫We'll go ahead and set up the @Autowired down that and let's come down here at either one of these tests
‫should work.
‫I want to set up an approval just before we work with the order repository.
‫New Order approval.
‫Approval setApprovedBy me.
‫So I'm going to go ahead and approve the order.
‫ApprovalRepository save.
‫And then on the, OrderHeadder.
‫Like so
‫So we're going to go through there.
‫We'll save it to the database and we actually use saved approval.
‫So that's a very easy mistake to make.
‫And sometimes you'll get away with it.
‫Sometimes you will not.
‫So let's go ahead and run the test here, make sure that it does, in fact, run properly.
‫Again, I'm running this for the first time, and when spring runs, it will run the fly away migration
‫and we'll see that in fact run.
‫So it did execute properly.
‫So let's see right here the hibernate insert into order approval.
‫So we do know that did in fact work for us.
‫So let me minimize this.
‫Just to recap what we changed here.
‫So we have the SQL creating the new table for order approval and then we alter the order header
‫table adding in the new column as well as a new foreign key.
‫We create the order approval entity again extending our base entity like so and then updating the order
‫header.
‫And in this case with the order header, we are doing a 1 to 1 relationship which sets up that relationship
‫between the order header and order approval.
‫And we provided a JPA repository for that.
‫And then finally we update our test to go ahead, create in our pre-approval, save it, assign it to
‫the order, and then save the order with it.