The Case for Object-Relational Mapping2:43 with Chris Ramacciotti
After all that work in writing a method whose job is to save a contact to the database, in this video we consider the convenience of writing a method for each of the other three CRUD operations. In doing so, we begin building a strong case for a tool that provides this convenience for us.
Now that you've got some experience storing POJO data into a database, 0:01 let's consider the work we have left using this approach. 0:04 We would still need to incorporate updating and deleting existing contacts. 0:08 Also, we haven't created a similar approach for fetching all contacts and 0:12 storing them into a list of contact objects. 0:16 And we'd probably want to have groups in our contact manager. 0:20 So we should probably create a POJO for a group. 0:23 And all the operations there, as well. 0:26 And we'll need to be able to translate a contact's group 0:30 field into a database relationship. 0:32 Whoa. 0:35 We've got some work to do. 0:36 And honestly, 0:38 sometimes a project could involve this kind of approach we're taking right now. 0:39 But often our data becomes too complex in our code 0:43 too verbose to keep going down this path. 0:46 I mean, what if we had 50 objects, 0:49 each having 5 to 10 fields, and what if there are complex relationships among 0:52 those objects that we have to translate to database relationships? 0:56 This could be a SQL nightmare. 0:59 But this is where Object Relational Mapping tools come to the rescue. 1:02 Because so many applications are backed by databases, we need a way to translate 1:07 between JAVA objects and the persistent storage of data. 1:12 Be it MySQL, SQL Server or post press. 1:15 There are several benefits to using an ORM. 1:19 We could use POJOs to model data. 1:23 We can persist POJOs with little code. 1:25 Manage relationships among POJOs and model that relationship in the database. 1:28 And we can swap the backend database with another system. 1:33 And only update the parts in the ORM that interface with the DB. 1:36 And we can do all of these without ever having to change POJOs, 1:42 models, views, controllers, and what have you? 1:46 In general, here is a look at how a Hibernate application can be represented. 1:50 In the application will have our typical code that includes our user interface and 1:55 the business logic with POJOs. 2:00 Then, we'll hand over those POJOs to Hibernate to let it persist them to 2:02 a database. 2:06 For the remainder of the course, we'll be using an H2 database, 2:08 since SQLite is not fully supported by Hibernate. 2:12 It will be our job to write the business logic, along with 2:15 invoking the proper Hibernate methods to persist POJOs to our H2 database. 2:18 Before we start discussing the specifics of hibernate, 2:23 it's worth noting here that Hibernate is not the only ORM out there. 2:26 There are a lots, and some of them on much lighter weight frameworks that 2:30 are perfectly acceptable to use on reasonably small projects. 2:34 Let's break here. 2:39 And when you're ready to hit Hibernate, I'll see you in the next stage. 2:40
You need to sign up for Treehouse in order to download course files.Sign up