Adding a POJO with JPA Annotations9:34 with Chris Ramacciotti
One of the biggest advantages of using an ORM such as Hibernate is the ability to quickly configure model POJOs for mapping to database tables and columns. You will use JPA (Java Persistence API) annotations to define these mappings.
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone firstname.lastname@example.org:treehouse/contactmgr-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout ... command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Before completing our configuration there is some other coding that we have to do 0:01 and some other concepts that we need to understand. 0:05 So we'll be returning to that XML file in just a bit. 0:07 As you've seen, the power of Hibernate and really any ORM lies in its ability to let 0:12 you, the developer, focus on Java code, while Hibernate takes care of the details 0:17 of fetching, adding, editing and deleting POJOs from the database. 0:21 These POJOs though in our case the contact class 0:26 need to be configured in a certain way for Hibernate to understand 0:29 how to map its fields to database columns in a contact table. 0:33 This is where it's helpful to know about yet 0:38 another acronym JPA J P A stands for Java Persistence. 0:40 A.P.I. and 0:46 is a set of interfaces that our ORM's can implement to provide data persistence. 0:47 Remember, that's just long lasting data storage being a set of interfaces or 0:52 ORM's including hibernate can choose to 0:57 implement these in whatever ways they desire. 1:00 As a developer though using J P A in our code means that we can swap out 1:03 the implementation, that's the RM, while minimizing the impact on our code. 1:08 Some application servers provide an implementation of JPA at runtime. 1:14 Such as J. 1:18 Boss. 1:19 But there is no such thing as a JPA only solution. 1:20 Why? 1:23 Because JPA is not an implementation but rather a contract. 1:24 It's like an expectation you can have when you use an implementation such 1:29 as Hibernate. 1:33 Like the expectation that the plug of a new pair of headphones will fit 1:34 into your iPhone or Android phone or Windows phone or even your computer. 1:37 You expect that they'll fit regardless of the device programming 1:42 that controls the audio signal itself. 1:46 But the presence of a headphone jack isn't enough to make Mumford and 1:49 Sons pump through to your ears. 1:53 For that there must be actual device programming that reads an audio file and 1:54 creates a sound signal that is sent to the headphone interface. 1:59 Okay, you get it. 2:03 J P eight isn't an implementation, but it includes the annotations that we'll use on 2:05 our contact pojo to indicate how we want to map its fields to database columns. 2:09 Let's see what that looks like. 2:15 Since we don't yet have a package for our application code let's create that now. 2:18 I'll right click on JAVA and choose new package and for the name I'll use 2:22 com.teamtreehouse.contactmgr and hit enter. 2:26 And no we won't play as much code in there just yet, let's create our main class and 2:32 call it application. 2:35 So on that package that I just created, I'll right click and 2:37 choose new Java class and name an application and 2:40 in here is where our public static void main method will go. 2:44 So, I'll get that in there now for later use. 2:48 Let's create a sub package of the one we just created where we could stick 2:51 any model or entity class we might need. 2:54 So I'll right click on this package and choose new package and name it model and 2:57 into it we'll put the only model class we'll be creating in this course, 3:02 the contact class. 3:06 So I'll create that now, New Java class and call it contact. 3:07 I'll quickly code this class but 3:13 feel free to copy paste from the one we used in work spaces. 3:15 So we'll get our id in there as well as our firstName lastName. 3:18 Email and 3:30 phone. 3:34 Now I'll use intelligent code generation to get some getters and setters. 3:38 And this time, we will need the setters. 3:42 So I'll choose getters and setters, highlight all of them and hit enter. 3:45 And boom there are our getters and setters. 3:49 And because J.P.A. will call our default constructor when instantiating contact 3:52 objects we need to make sure one is present. 3:57 With no other constructors in this class and no constructors explicitly defined in 4:00 a superclass the compiler will provide a default constructor for us. 4:04 But for purposes of clarity, 4:09 will explicitly define a default constructor in case we need to go back and 4:11 add a non default constructor and as a heads up we will end up doing just that. 4:15 So let's create that now and leave it empty. 4:20 This will be our default constructor for J.P. A public. 4:24 Contact. 4:31 And I'll even do it in one line. 4:32 Beautiful. 4:34 Let's also generate a two string method using all of our fields. 4:36 So again I'll use IntelliJ's code generation. 4:40 And arrow down to toString0 method and 4:43 include all fields in the toString0 it's generating. 4:46 Now it's time to add those J.P.A. annotations. 4:50 The one will start with is the entity annotation. 4:53 This is the one that will mark this class as want to persist to the database. 4:56 By default, entities are given their own table in the database. 5:01 So we will decorate our class. 5:05 With the Javax.persistence.entity annoation. 5:07 When hibernate detects this annotation, 5:12 it will map a contact object to a single row in the contact table of the database. 5:14 There is an optional name element for the entity annotation and you can specify 5:20 a value here if a value is provided that's what will be used as the map's TableName. 5:25 By default though, the class name is used and 5:31 will stick with that default in this course. 5:34 As for the fields, we'll skip over the id field and 5:37 mark all the others with a @Column annotation. 5:41 And again this comes from the javax.persistence library. 5:45 That is the JPA library. 5:49 These annotations are self describing in that each of these fields will 5:59 be mapped to a column of the contact table. 6:02 Again, the name element can be used to specify the column name, but by default, 6:05 the field name will be used and again will stick with the default in this course. 6:10 Now back up to the ID field. 6:15 We'll use a special annotation here to indicate that this field is to serve 6:18 as this entities primary key. 6:22 Now quite intuitively, it's the ID annotation. 6:25 And if you want this value to be automatically generated by Hibernate, 6:29 you can use the generated value annotation, 6:32 specifying the strategy to be used for generating new IDs. 6:36 In our case, we'll use GenerationType.IDENTITY. 6:40 Now it turns out that since we included an ID 6:45 annotation on a field that every other field unless marked as transients. 6:48 Either with a JAVA modifier or with the transience annotation. 6:52 Will be automatically mapped to a database column 6:56 even without these column annotations. 6:58 I'll leave them there though. 7:02 Just for purposes of demonstration. 7:03 One final task before we take a break. 7:06 I mentioned earlier that we'd be returning to that XML configuration file. 7:08 Let's do that now. 7:12 There are two more elements that we need to add. 7:14 The first calls upon a Hibernate tool to generate our database schema 7:16 based on our annotated entities. 7:21 Let me stick a comment in there. 7:23 That references what this property is going to be doing. 7:25 So we want to create the database schema. 7:29 On application start up. 7:34 And to accomplish this we are going to use another property. 7:37 The name of this property is going to be hbm2ddl.auto. 7:42 And the value we're going to use here is create. 7:49 Now hbm2ddl stands for hibernate mapping to data definition language. 7:54 This particular setting tells hibernate to 7:59 create the schema upon starting the application. 8:02 It will do this by reading the hibernate configuration file, looking for 8:06 mapped entity classes, and then examining those classes, JPA annotations, 8:10 to create database tables and columns. 8:15 You might wonder then, how does this configuration file 8:17 inform hibernate that we have a mapped entity class? 8:20 The answer is that we add a mapping element indicating the fully qualified 8:24 name of the entity class. 8:28 I say fully qualified because that name must include the package name as well. 8:30 Let me get a comment in here for this mapping element. 8:34 This is going to name the annotated entity classes. 8:41 In our application we'll have only one class. 8:48 This XML element is called mapping and it has a single attribute named class and 8:51 it has no inner text so I'm going to create it as a self closing element. 8:56 As I said before, the name of this class has to be fully qualified which means it 9:00 includes the package name, in our case com.teamtreehouse.contactmgrif 9:05 the.model.Contact, and there is the fully 9:12 qualified name of our entity class that includes our JPA annotations. 9:16 Hey great work on getting things ready to go for hibernate. 9:23 Why don't we stop for a quick check for understanding before moving on to create 9:26 the hibernate objects that will connect our application to a maps database. 9:29
You need to sign up for Treehouse in order to download course files.Sign up