Learn how to create and update entities.
- [Instructor] So let's get in to some real code. In the exercise files, there is a Spring Boot Maven project that I created with Spring Initializr. If you are not familiar with Spring Boot or Spring Initializr, I encourage you to watch the first chapter of Learning Spring with Spring Boot. The project is called University. It incorporates an H2 in-memory database, so you can download and run the project without any setup, other than Java, Maven, and an IDE. The base package is com.example.university.
There are three java entities, student, course, and department, in the domain package. Student is an entity. It has a student ID that is automatically generated upon construction. There's a full-time status attribute, and age attribute, and attendee, which is embeddable as the person class that has the first and last name of the student.
So person is embeddable. It's not an entity in itself. It's part of the student entity, and there's a first and last name attributes. Back to Student. There is a one-to-many relationship between student and courses. So this is the list of courses that the student is enrolled in. Let's open Course, courses and entity. It has also has a generated ID, and name, and a department that the course is in.
So let's open Department. Department is an entity. It has a generated ID as well, and name, and then a list of courses that are in the department. So there's a one-to-many relationship back to courses, and we have a FetchType eager and cascade all. Now in the repo package, let's open StudentRepository. The StudentRepository interface extends from the Spring Data CrudRepository where the bounded type parameters are student and integer.
Integer is the type of the student ID attribute. StudentRepository inherits the create, read, and update and delete methods from CrudRepository. I'm going to use JUnit to exercise the repository. It is in the source/test folder, and the file is called CrudRepositoryDemo. This test leverages the JUnit Spring Runner to autowire an instance of the StudentRepository, and Spring Boot test, which starts up the Spring Boot University application within the JUnit JVM.
So in this example, I'm going to run this method, this JUnit test, and I'm going to create some students and for systems to the repository, which is an in-memory database. I'm gonna look them up and print them out. I'm going to iterate through them and age up each one of the students, add a year to each of their ages. Look them up again and print them out again, and then remove them. I'm leveraging Java eight streams and lambdas to print these students all on one line of code.
If this is new to you, you should check out Learning Java Lambda Expressions. So I'm going to debug this method, and run through these. "But wait," you might be asking, "Mary Ellen, you forgot something. "Where did you implement the repository methods?" Well, you're right, I didn't, and what's better, I don't have to. At run time during bootstrap, Spring Data scans the code for any interface that extends the repository interface.
The implementation is inferred by the declaration. So we see in the debugger, this object that's referenced of studentRepository that actually references an object of the class SimplejpaRepository. So let's step through these. I have my status as fullTime, and let's create student Jane Doe who's fullTime and 20 years old. We have student John Doe who's fullTime and 22. Mike Smith is fullTime and 18.
And Ally Kim is not fullTime and 19. And show the console, and we'll print out all those students, and so they're in our persistent store. And now we're going to iterate through all those, and age them up, and print them out. Now they're definitely a year older. And now we're gonna delete them all in one method and verify that they're gone.
Much cleaner and better code than regular JPA.
In this course, learn how to easily implement JPA-based repositories using Spring Data JPA. Mary Ellen Bowman describes the Spring Data umbrella project, and helps you understand JPA for object-relational mapping. She also covers querying, and dives into other Spring Data Commons features such as QueryDSL and auditing.
- Spring Data Commons
- Using JPA for object-relational mapping
- Declaring Spring Data Repositories
- Creating query methods with property expressions and @Query
- Query by example
- QueryDSL Spring Data Extension
- Spring Data REST
- Introduction to Spring Data Mongo
- Common pitfalls