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...
-
The balance sheet of Collingwood International Inc. at December 31, 2016, presented the following shareholders' equity: Contributed capital Common shares, 2,000,000 shares authorized, 500,000 shares...
-
Review the parts of the plot, found above in the Information section, and answer the following: As the plot carries on in this novel, we see how Eva exists within the stormy conditions of Montreal....
-
Construct a Gantt chart for the general multipurpose plant in Figure 22.14, but with the unit assignments specified in Figure 22.13. Figure 22.13:- Figure 22.14:- Product A Product B Task 1 Task 2...
-
The charter for WCAP-TV, Inc. authorizes the company to issue 100,000 shares of $2, no-par preferred stock and 500,000 shares of common stock with $1 par value. During its start-up phase, WCAP...
-
1. Determine the atmospheric pressure at a location where the barometric reading is 720 mm Hg. 2. A pressure gauge connected to a tank reads 52 psi at a location where the barometric reading is 29.6...
-
A probability plot of 66 yr of peak discharges for the Kentucky River near Salvisa, Kentucky, is shown in Fig. P3-19 (a) What probability distribution is being used? (b) What are the mean and...
-
A successful grocery store wants a. low inventory turnover and high account receivable turnover b. low inventory turnover and low account receivable turnover c. high inventory turnover and high...
-
Stella is assessing the financial stability of a company during a business acquisition. She is looking at the balance sheet of the company during a business meeting. Where can she find information...
-
This is where you share your thoughts and submit your recitation. Question: Cite the difference between the Legislation in the 1950s and of the current year. What is Law-making Process?
-
What are Financial or intangible assets? Explain with examples. What are Tangible or physical assets? Also Match the category of asset with its examples car, art and real estate savings accounts,...
-
Auditors also pay attention to documentation retention policies. Properly retaining records related to sales transactions, including sales orders, invoices, and shipping documents, is essential for...
-
Grant Company gathered the following reconciling information in preparing its July bank reconciliation: Cash balance per books, 7/31........... .$4,500 Deposits-in-transit......... $ 150 Notes...
-
Find the radius of convergence of? 1.2.3 1.3.5 (2n-1) r2n+1 -1
-
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...
-
External auditors collect consulting fees from the same clients they perform independent audits for, which allows good and close relationship with the clients. True/false
-
To deal with the turmoil caused by the various accounting scandals, the SOX Act was passed in 1999. True/false
-
Auditors should not demand full access to the entire general ledger so that they can be more focused on the audit. True/false
Study smarter with the SolutionInn App