Enterprise Java with Spring Boot

One-to-Many Relationships

Josh Long
Broadcom
Enterprise Java with Spring Boot

Lesson Description

The "One-to-Many Relationships" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:

Josh highlights how quickly Spring Data picks up on many-to-many relationships. A database migration is added to insert dogs into the dog table. A new Dog record is created and displayed in the console alongside the Person data.

Preview
Close

Transcript from the "One-to-Many Relationships" Lesson

[00:00:00]
>> Josh Long: Now I mentioned earlier that we have a separate table called dog. And you know, I want to map that dog. It's a one to many relationship there. Right? So set of dog, dogs, record dog. Hey, come back. Record dog, entity, string name, string owner, string description. Okay.

[00:00:22]
And then I'm going to make a set. Okay, there's my updated code. I have no dogs though. Right. So let's go back and create yet another migration. V3 Prancer and Peanut. Okay, so insert. And by the way, insert into dog. Do we know what the ID is for person?

[00:00:49]
Surely it's one. Yeah. Okay, it's one. So we go back to this. Insert into dog. Name, person, description, values. Prancer. Okay, Prancer. bbb The person is one and the description is. Where's my guy, Prancer? He is a. Okay, so he is a neurotic man hating animal, hating children, hating dog.

[00:01:38]
That looks like a gremlin. Okay, there we go. That's Prancer. And then we'll have Peanut. Okay, Peanut, the worst. Okay, there we go. So there's my two dogs. Let's go ahead and now restart this. Okay, so now if I go over here, I've got person. Great. Two dogs, great.

[00:02:06]
No owners. Very surprising. Okay, nobody could have seen this coming. But if I go down here and I print out the person, I do the find all and I'm doing. It's now mapped 1 to n, right?. So if I look at the results there dogs says dog one and then dog two.

[00:02:22]
So it's automatically figured out that relationship for me, which I think is pretty nice. This is a very concise way. Okay, so I've got spring data, I've got 1 to n relationships, I've got schema migrations, I think we're doing pretty good.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now