Science & Technology

#22 Project using Spring | Loading data in H2

by Telusko

Share:

📚 Main topics

  • Product endpointThe controller adds a GET endpoint at the products URL, using a service and repository to retrieve products instead of accessing the database directly. Initially, it returns an empty array because the table has no data. 0:00
  • Repository and database setupThe product repository extends JpaRepository, and the service uses its find-all method to fetch products. The video also adds generated IDs to the entity so inserts can omit the ID. 2:40
  • Adding sample dataAfter correcting mismatched column names and enabling ID generation, sample car records are inserted into H2 and returned by the endpoint. 5:16
  • Loading data on startupBecause the in-memory H2 database loses its records when the application restarts, a data.sql file is used to load default records. The data source initialization setting is enabled so the table is created before the SQL runs. 8:54

✨ Key takeaways

  • Use layered architectureThe controller delegates to a service, and the service delegates to a repository, keeping database access out of the controller. 1:06
  • In-memory database behaviorH2 data is temporary and disappears when the application restarts, so default data must be loaded again at startup. 8:54
  • Initialization order mattersDeferring data source initialization resolves the startup issue where Spring tries to run data.sql before creating the table. 9:55

🧠 Lessons learned

  • Expect and troubleshoot errorsDevelopment involves resolving issues such as mismatched column names, missing IDs, and startup initialization order. 5:48
  • Choose constructor injectionAlthough field injection is used to keep the example short, constructor injection is recommended. 2:09

🏁 Conclusion/next steps

  • Continue in the next videoThe sample records now load into H2 and appear in the endpoint response. The next video will address connecting the data to React and formatting dates. 10:56

Transcript excerpt

0:00 so now it's time to focus on the controller because now we want to return the products right now first of all we don't have products in the table but let's see what happens when you don't have a data in the table and still you when when you try to fetch it of course we will get blank but let's do that so let's go to the product controller and here we want to create a method which will return you the list of products and the URL would be/ products we have tried that before right so if you go back to your browser and here we are asking for products is giving you 44 we don't want

0:32 this so I will go back here and let me say create a method which will return the list of products because we don't have one product we'll be having list of products so list of products get all products of course you can have any method name doesn't matter but still let's give it proper and we have to also import this okay let's set the URL so we are going for the get mapping because we are going for the get method so get mapping and the you Ur L will be products so the actual URL will be API SL products so we

🔒 The full, searchable transcript is available with Pro.

🔒 Unlock Premium Features

This is a premium feature. Upgrade to unlock unlimited Q&A, transcripts, mindmaps, and translations.

🔒 Unlock Premium Features

Access to Chat is a premium feature. Upgrade now to unlock unlimited studying tools.

🔒 Unlock Premium Features

Access to Mindmap is a premium feature. Upgrade now to unlock unlimited studying tools.

🔒 Unlock Premium Features

Access to Translation is a premium feature. Upgrade now to unlock unlimited studying tools.

Get unlimited summaries, Q&A, transcripts and more with Pro

Upgrade to Pro

Suggestions

🔒 Unlock Premium Features

Access to AI Suggestions is a premium feature. Upgrade now to unlock unlimited studying tools.