Summary Create a functional Spring Boot system connected to H2 through JPA, then tool it up...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Summary Create a functional Spring Boot system connected to H2 through JPA, then tool it up with an Entity Relationship! Today, we'll look at Kids who have Pets. Note: mostly explicit step-by-step instructions given this time because this can get confusing quickly! Follow through, step by step, line by line, and make it work. You've got this! Step 1- Beans Create a brand-new project called ex32_oneToOne. Use the H2 Database, Spring Data JPA, Thymeleaf, Spring Web, Spring Devtools, and Lombok repos when asked. Create a bean called Kid in a ca.sheridancollege. .beans package. Inside, store a Long id, a String name, and an Integer age. Make sure all bean properties use private visibility. Lombok as needed. Create a PetType Enum, also in the beans package. Inside PetType, list a few friendly domestic animals that make good pets. Thinking basically DOG, CAT, FISH, BIRD .. and maybe one or two more if you wish. Now create a second full bean called Pet in the beans package, and inside, create private properties for a Long id, String name, Boolean furry, and PetType petType. Add @Entity, @ld, @GeneratedValue(strategy-Generation Type.IDENTITY), and @Enumerated (EnumType.STRING) in the appropriate places. Add an instance of Pet in Kid, and make the relationship an @OneToOne. Use an @JoinTable on the id properties as we did in the lecture. @JoinTable (name="KID_PET", joinColumns inverseJoinColumns=@JoinColumn (name="PET ID")) = @JoinColumn (name="KID_ID"), Friendly note, Entities often refer to a "has a" relationship, which is super intuitive in this case (a kid has a pet), but is not always so obvious. Again, if you're ever in doubt about which side to add the relationship to, add it to the more independent side. For example, an Artist can have a Song, but they don't have to. On the other hand, looking at it from the Song side, if the artist vanishes from existence and history, their songs also vanish. Artists can exist without Songs, but Songs cannot easily exist without Artists. Not necessarily a perfect rule, but simple at least - put the relationship inside the (more) independent entity. Step 2 Repositories Create an interface called KidRepository in a ca.sheridancollege. .repositories package which extends JpaRepository. Modify Kid Repository so it works with (reflects) a Kid with a Long type ID. Create a second interface in the repositories package called PetRepository which extends Jpa Repository and which reflects a Pet and the Pet Long ID type. Add your own queries, to the appropriate repository, using JPA keywords: Find all kids with an age greater than some Integer value Find all kids who own a pet (meaning where Pet is not null) Find all pets who are furry (meaning where furry is true) Find all pets matching a PetType (meaning where a retrieved PetType value matches) Find all kids who own a particular PetType (meaning, from the Kid Repository, findByPet_PetTypeEquals(PetType value)) Find all kids who own a furry pet (meaning, again from the Kid Repository, findByPet_FurrylsTrue) Step 3 - POJO Controllers and Thymeleaf Create a class in a ca.sheridancollege. .controllers package called KidController. Annotate the class with @Controller and @AllArgsConstructor. Add a private Kid Repository instance for constructor injection. Create a second class in the controllers package called PetController. Annotate with @Controller and @AllArgsConstructor again, and add a private PetRepository instance for constructor injection. Create a Get Mapped method for root ("/") in KidController that returns to index. Create an index.html page in your templates directory. Inside index.html, create a hyperlink to "/addPet", and a second hyperlink to "/addkid". In PetController, create a Get Mapped method for "/addPet" where you add a blank Pet instance to the Model as an attribute, add all values from PetType as a second attribute named petType, and return (dispatch) to "addPet". Create an addPet.html HTML page in your src/main/resources templates directory. Inside create a form that is bound to your blank Pet instance in the model, that ultimately posts back to /addPet. Use appropriate form fields so your users can enter their Pet info. Here's something to help: Name: Furry: Pet Type: /form> Back at the PetController, create a Post Mapped method for "/add Pet" that retrieves your Pet instance as an @ModelAttribute. Use the petRepository instance to .save your pet, and remember that saving an instance to the db will return the same instance *plus* the auto-generated id value inside. Create a new Kid instance, set your returned and now fully populated pet into it, and add the kid to the model. Return to "addKid"; petRepository.save (pet); Pet savedPet = Kid kid = new Kid (); kid.setPet (savedPet); model.addAttribute ("kid", kid); return "addKid"; Create an addKid.html page in your templates directory. Add the following form: Name: Age: Now create a Post Mapped method to "/addKid" in the KidController that retrieves your Kid @ModelAttribute, uses the kidRepository to save your kid to the database, and returns to "index" as before. Finally, create a Get Mapped method for "/addKid" in the KidController that adds a blank (new) Kid to the Model (ignoring the fact that Pet inside it will be null), and which also returns to "addkid". Run it and add in a new Pet followed by a Kid. Try just adding a Kid. Make sure everything works as expected in your H2 Console! Friendly note, you likely need to enable the console in your application.properties, but that's all easy now! Step 4 - Queries At the index.html page, create a form for each query possibility created earlier. Remember, multiple forms per page is no problem, but be sure they're never nested - close them at the appropriate place! Happy to try to help with a bit of HTML here, but please style this up when/if you can! Name Age Pet Name Pet Is Furry Summary Create a functional Spring Boot system connected to H2 through JPA, then tool it up with an Entity Relationship! Today, we'll look at Kids who have Pets. Note: mostly explicit step-by-step instructions given this time because this can get confusing quickly! Follow through, step by step, line by line, and make it work. You've got this! Step 1- Beans Create a brand-new project called ex32_oneToOne. Use the H2 Database, Spring Data JPA, Thymeleaf, Spring Web, Spring Devtools, and Lombok repos when asked. Create a bean called Kid in a ca.sheridancollege. .beans package. Inside, store a Long id, a String name, and an Integer age. Make sure all bean properties use private visibility. Lombok as needed. Create a PetType Enum, also in the beans package. Inside PetType, list a few friendly domestic animals that make good pets. Thinking basically DOG, CAT, FISH, BIRD .. and maybe one or two more if you wish. Now create a second full bean called Pet in the beans package, and inside, create private properties for a Long id, String name, Boolean furry, and PetType petType. Add @Entity, @ld, @GeneratedValue(strategy-Generation Type.IDENTITY), and @Enumerated (EnumType.STRING) in the appropriate places. Add an instance of Pet in Kid, and make the relationship an @OneToOne. Use an @JoinTable on the id properties as we did in the lecture. @JoinTable (name="KID_PET", joinColumns inverseJoinColumns=@JoinColumn (name="PET ID")) = @JoinColumn (name="KID_ID"), Friendly note, Entities often refer to a "has a" relationship, which is super intuitive in this case (a kid has a pet), but is not always so obvious. Again, if you're ever in doubt about which side to add the relationship to, add it to the more independent side. For example, an Artist can have a Song, but they don't have to. On the other hand, looking at it from the Song side, if the artist vanishes from existence and history, their songs also vanish. Artists can exist without Songs, but Songs cannot easily exist without Artists. Not necessarily a perfect rule, but simple at least - put the relationship inside the (more) independent entity. Step 2 Repositories Create an interface called KidRepository in a ca.sheridancollege. .repositories package which extends JpaRepository. Modify Kid Repository so it works with (reflects) a Kid with a Long type ID. Create a second interface in the repositories package called PetRepository which extends Jpa Repository and which reflects a Pet and the Pet Long ID type. Add your own queries, to the appropriate repository, using JPA keywords: Find all kids with an age greater than some Integer value Find all kids who own a pet (meaning where Pet is not null) Find all pets who are furry (meaning where furry is true) Find all pets matching a PetType (meaning where a retrieved PetType value matches) Find all kids who own a particular PetType (meaning, from the Kid Repository, findByPet_PetTypeEquals(PetType value)) Find all kids who own a furry pet (meaning, again from the Kid Repository, findByPet_FurrylsTrue) Step 3 - POJO Controllers and Thymeleaf Create a class in a ca.sheridancollege. .controllers package called KidController. Annotate the class with @Controller and @AllArgsConstructor. Add a private Kid Repository instance for constructor injection. Create a second class in the controllers package called PetController. Annotate with @Controller and @AllArgsConstructor again, and add a private PetRepository instance for constructor injection. Create a Get Mapped method for root ("/") in KidController that returns to index. Create an index.html page in your templates directory. Inside index.html, create a hyperlink to "/addPet", and a second hyperlink to "/addkid". In PetController, create a Get Mapped method for "/addPet" where you add a blank Pet instance to the Model as an attribute, add all values from PetType as a second attribute named petType, and return (dispatch) to "addPet". Create an addPet.html HTML page in your src/main/resources templates directory. Inside create a form that is bound to your blank Pet instance in the model, that ultimately posts back to /addPet. Use appropriate form fields so your users can enter their Pet info. Here's something to help: Name: Furry: Pet Type: /form> Back at the PetController, create a Post Mapped method for "/add Pet" that retrieves your Pet instance as an @ModelAttribute. Use the petRepository instance to .save your pet, and remember that saving an instance to the db will return the same instance *plus* the auto-generated id value inside. Create a new Kid instance, set your returned and now fully populated pet into it, and add the kid to the model. Return to "addKid"; petRepository.save (pet); Pet savedPet = Kid kid = new Kid (); kid.setPet (savedPet); model.addAttribute ("kid", kid); return "addKid"; Create an addKid.html page in your templates directory. Add the following form: Name: Age: Now create a Post Mapped method to "/addKid" in the KidController that retrieves your Kid @ModelAttribute, uses the kidRepository to save your kid to the database, and returns to "index" as before. Finally, create a Get Mapped method for "/addKid" in the KidController that adds a blank (new) Kid to the Model (ignoring the fact that Pet inside it will be null), and which also returns to "addkid". Run it and add in a new Pet followed by a Kid. Try just adding a Kid. Make sure everything works as expected in your H2 Console! Friendly note, you likely need to enable the console in your application.properties, but that's all easy now! Step 4 - Queries At the index.html page, create a form for each query possibility created earlier. Remember, multiple forms per page is no problem, but be sure they're never nested - close them at the appropriate place! Happy to try to help with a bit of HTML here, but please style this up when/if you can! Name Age Pet Name Pet Is Furry
Expert Answer:
Related Book For
Smith and Roberson Business Law
ISBN: 978-0538473637
15th Edition
Authors: Richard A. Mann, Barry S. Roberts
Posted Date:
Students also viewed these algorithms questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
In 1988, a United Nations report by the late Prof. Roger Hood from Oxford University stated, "'In many regions of the world there is little sign that [death penalty] abolition will occur soon." How...
-
A cheetah spots a Thomsons gazelle, its preferred prey, and leaps into action, quickly accelerating to its top speed of 30 m/s, the highest of any land animal. However, a cheetah can maintain this...
-
Thomas Wong, accountant for APB Bikes Ltd., was injured in a biking accident. Another employee prepared the income statement shown on the next page for the fiscal year ended September 30, 2017. The...
-
Compute and tabulate the azimuths of the sides of a regular hexagon (polygon with six equal angles), given the starting direction of side AB. Azimuth of AB = 87o 14' 26'' (Station C is westerly from...
-
Heritage, a general contractor, had filed a breach of contract action against an electrical subcontracting firm that had withdrawn its bid right before the contract was to be performed. A jury had...
-
Entries for Disposition of Assets On December 31, 2010, Chrysler Inc. has a machine with a book value of $940,000. The original cost and related accumulated depreciation at this date are as follows....
-
Consider the following ridiculously simple linear system: 10 (9)(2)-(0) a. What is the solution of this linear system? You don't have to show your work. b. Set up a Neumann iteration with initial...
-
A parent company acquired 70% of the stock of a subsidiary company on January 1, 2017, for $610,000. On this date, the balances of the subsidiarys stockholders equity accounts were Common Stock,...
-
Pure iron crystallizes with a body-centered cubic structure, but small amounts of impurities can stabilize a face-centered cubic structure. Which form of iron has a higher density. Explain why is it...
-
Identify 2 things you learned in this class you found to be the most valuable, and answer: (1) What are they? (tell us what they are and remind us what they are all about - this will be a good...
-
Markowitz portfolio optimization: Harry Markowitz received the 1990 Nobel Prize for his path-breaking work in portfolio optimization. One version of the Markowitz model is based on minimizing the...
-
1. An object is undergoing SHM with an amplitude 0.320 m. It goes through 20 cycles of oscillation in 18 seconds. At t = 0 the object is at x = 0.320 m and is instantaneously at rest. Calculate the...
-
You receive an inheritance from your grandmother in the amount of $9 million. The inheritance will be paid in 3 equal installments and the first payment will arrive only in three years. How much is...
-
A 5.0-kg object slides down a frictionless surface inclined at an angle of 30 from the horizontal. The total distance moved by the object along the plane is 10 meters. The work done on the object by...
-
In the case student from the book Davenport & Kim on Metabical case study from pages 77-88. Can you let me know if the case study align the problem, models, an data an why with giving insights?
-
For the vector whose polar components are (Vr = 1, Vθ = 0), compute in polars all components of the second covariant derivative Vα;μ;ν. To find...
-
This is a stocklist case arising under 220(b) of our [Delaware] General Corporation Law. The issue is whether a shareholder states a proper purpose for inspection under our statute in seeking to...
-
On June 15, a newspaper columnist predicted that the coast of State X would be flooded on the following September 1. Relying on this pronouncement, Gullible quit his job and sold his property at a...
-
Tate and Fitch were longtime friends. Tate was a man of considerable means; Fitch had encountered financial difficulties. To bolster his failing business, Fitch desired to borrow $6,000 from Farmers...
-
Souvenir Sales. The file SouvenirSales.csv contains monthly sales for a souvenir shop at a beach resort town in Queensland, Australia, between 1995 and 2001. [Source: Hyndman and Yang (2018).] Back...
-
Analysis of Canadian Manufacturing Workers Workhours. The time plot in Figure 18 . 22 describes the average annual number of weekly hours spent by Canadian manufacturing workers...
-
Toys R US Revenues. Figure 18 . 23 is a time plot of the quarterly revenues of Toys R US between 1992 and 1995 (thanks to Chris Albright for suggesting the use of these data, which are available in...
Study smarter with the SolutionInn App