1 00:00:01,310 --> 00:00:05,190 Before completing our configuration there is some other coding that we have to do 2 00:00:05,190 --> 00:00:07,800 and some other concepts that we need to understand. 3 00:00:07,800 --> 00:00:11,140 So we'll be returning to that XML file in just a bit. 4 00:00:12,200 --> 00:00:17,160 As you've seen, the power of Hibernate and really any ORM lies in its ability to let 5 00:00:17,160 --> 00:00:21,860 you, the developer, focus on Java code, while Hibernate takes care of the details 6 00:00:21,860 --> 00:00:26,570 of fetching, adding, editing and deleting POJOs from the database. 7 00:00:26,570 --> 00:00:29,710 These POJOs though in our case the contact class 8 00:00:29,710 --> 00:00:33,680 need to be configured in a certain way for Hibernate to understand 9 00:00:33,680 --> 00:00:38,670 how to map its fields to database columns in a contact table. 10 00:00:38,670 --> 00:00:40,750 This is where it's helpful to know about yet 11 00:00:40,750 --> 00:00:46,565 another acronym JPA J P A stands for Java Persistence. 12 00:00:46,565 --> 00:00:47,680 A.P.I. and 13 00:00:47,680 --> 00:00:52,420 is a set of interfaces that our ORM's can implement to provide data persistence. 14 00:00:52,420 --> 00:00:57,940 Remember, that's just long lasting data storage being a set of interfaces or 15 00:00:57,940 --> 00:01:00,480 ORM's including hibernate can choose to 16 00:01:00,480 --> 00:01:03,660 implement these in whatever ways they desire. 17 00:01:03,660 --> 00:01:08,160 As a developer though using J P A in our code means that we can swap out 18 00:01:08,160 --> 00:01:12,770 the implementation, that's the RM, while minimizing the impact on our code. 19 00:01:14,160 --> 00:01:18,400 Some application servers provide an implementation of JPA at runtime. 20 00:01:18,400 --> 00:01:19,210 Such as J. 21 00:01:19,210 --> 00:01:20,190 Boss. 22 00:01:20,190 --> 00:01:23,810 But there is no such thing as a JPA only solution. 23 00:01:23,810 --> 00:01:24,740 Why? 24 00:01:24,740 --> 00:01:28,480 Because JPA is not an implementation but rather a contract. 25 00:01:29,780 --> 00:01:33,590 It's like an expectation you can have when you use an implementation such 26 00:01:33,590 --> 00:01:34,750 as Hibernate. 27 00:01:34,750 --> 00:01:37,860 Like the expectation that the plug of a new pair of headphones will fit 28 00:01:37,860 --> 00:01:42,960 into your iPhone or Android phone or Windows phone or even your computer. 29 00:01:42,960 --> 00:01:46,140 You expect that they'll fit regardless of the device programming 30 00:01:46,140 --> 00:01:48,239 that controls the audio signal itself. 31 00:01:49,270 --> 00:01:53,100 But the presence of a headphone jack isn't enough to make Mumford and 32 00:01:53,100 --> 00:01:54,880 Sons pump through to your ears. 33 00:01:54,880 --> 00:01:59,340 For that there must be actual device programming that reads an audio file and 34 00:01:59,340 --> 00:02:03,480 creates a sound signal that is sent to the headphone interface. 35 00:02:03,480 --> 00:02:05,000 Okay, you get it. 36 00:02:05,000 --> 00:02:09,620 J P eight isn't an implementation, but it includes the annotations that we'll use on 37 00:02:09,620 --> 00:02:15,780 our contact pojo to indicate how we want to map its fields to database columns. 38 00:02:15,780 --> 00:02:16,890 Let's see what that looks like. 39 00:02:18,520 --> 00:02:22,360 Since we don't yet have a package for our application code let's create that now. 40 00:02:22,360 --> 00:02:26,516 I'll right click on JAVA and choose new package and for the name I'll use 41 00:02:26,516 --> 00:02:31,372 com.teamtreehouse.contactmgr and hit enter. 42 00:02:32,470 --> 00:02:35,940 And no we won't play as much code in there just yet, let's create our main class and 43 00:02:35,940 --> 00:02:37,360 call it application. 44 00:02:37,360 --> 00:02:40,100 So on that package that I just created, I'll right click and 45 00:02:40,100 --> 00:02:44,030 choose new Java class and name an application and 46 00:02:44,030 --> 00:02:48,540 in here is where our public static void main method will go. 47 00:02:48,540 --> 00:02:51,290 So, I'll get that in there now for later use. 48 00:02:51,290 --> 00:02:54,280 Let's create a sub package of the one we just created where we could stick 49 00:02:54,280 --> 00:02:57,290 any model or entity class we might need. 50 00:02:57,290 --> 00:03:02,790 So I'll right click on this package and choose new package and name it model and 51 00:03:02,790 --> 00:03:06,190 into it we'll put the only model class we'll be creating in this course, 52 00:03:06,190 --> 00:03:07,940 the contact class. 53 00:03:07,940 --> 00:03:12,830 So I'll create that now, New Java class and call it contact. 54 00:03:13,910 --> 00:03:15,180 I'll quickly code this class but 55 00:03:15,180 --> 00:03:18,730 feel free to copy paste from the one we used in work spaces. 56 00:03:18,730 --> 00:03:27,009 So we'll get our id in there as well as our firstName lastName. 57 00:03:30,545 --> 00:03:34,953 Email and 58 00:03:34,953 --> 00:03:38,820 phone. 59 00:03:38,820 --> 00:03:42,380 Now I'll use intelligent code generation to get some getters and setters. 60 00:03:42,380 --> 00:03:44,840 And this time, we will need the setters. 61 00:03:45,930 --> 00:03:49,800 So I'll choose getters and setters, highlight all of them and hit enter. 62 00:03:49,800 --> 00:03:52,778 And boom there are our getters and setters. 63 00:03:52,778 --> 00:03:57,640 And because J.P.A. will call our default constructor when instantiating contact 64 00:03:57,640 --> 00:04:00,910 objects we need to make sure one is present. 65 00:04:00,910 --> 00:04:04,850 With no other constructors in this class and no constructors explicitly defined in 66 00:04:04,850 --> 00:04:09,410 a superclass the compiler will provide a default constructor for us. 67 00:04:09,410 --> 00:04:11,380 But for purposes of clarity, 68 00:04:11,380 --> 00:04:15,710 will explicitly define a default constructor in case we need to go back and 69 00:04:15,710 --> 00:04:20,820 add a non default constructor and as a heads up we will end up doing just that. 70 00:04:20,820 --> 00:04:22,750 So let's create that now and leave it empty. 71 00:04:24,700 --> 00:04:31,340 This will be our default constructor for J.P. A public. 72 00:04:31,340 --> 00:04:32,300 Contact. 73 00:04:32,300 --> 00:04:34,740 And I'll even do it in one line. 74 00:04:34,740 --> 00:04:36,180 Beautiful. 75 00:04:36,180 --> 00:04:40,050 Let's also generate a two string method using all of our fields. 76 00:04:40,050 --> 00:04:43,000 So again I'll use IntelliJ's code generation. 77 00:04:43,000 --> 00:04:46,180 And arrow down to toString0 method and 78 00:04:46,180 --> 00:04:49,300 include all fields in the toString0 it's generating. 79 00:04:50,390 --> 00:04:53,560 Now it's time to add those J.P.A. annotations. 80 00:04:53,560 --> 00:04:56,910 The one will start with is the entity annotation. 81 00:04:56,910 --> 00:05:01,070 This is the one that will mark this class as want to persist to the database. 82 00:05:01,070 --> 00:05:05,230 By default, entities are given their own table in the database. 83 00:05:05,230 --> 00:05:07,605 So we will decorate our class. 84 00:05:07,605 --> 00:05:12,792 With the Javax.persistence.entity annoation. 85 00:05:12,792 --> 00:05:14,900 When hibernate detects this annotation, 86 00:05:14,900 --> 00:05:20,860 it will map a contact object to a single row in the contact table of the database. 87 00:05:20,860 --> 00:05:25,870 There is an optional name element for the entity annotation and you can specify 88 00:05:25,870 --> 00:05:31,960 a value here if a value is provided that's what will be used as the map's TableName. 89 00:05:31,960 --> 00:05:34,580 By default though, the class name is used and 90 00:05:34,580 --> 00:05:36,970 will stick with that default in this course. 91 00:05:37,980 --> 00:05:41,220 As for the fields, we'll skip over the id field and 92 00:05:41,220 --> 00:05:45,140 mark all the others with a @Column annotation. 93 00:05:45,140 --> 00:05:49,580 And again this comes from the javax.persistence library. 94 00:05:49,580 --> 00:05:51,754 That is the JPA library. 95 00:05:59,382 --> 00:06:02,848 These annotations are self describing in that each of these fields will 96 00:06:02,848 --> 00:06:05,610 be mapped to a column of the contact table. 97 00:06:05,610 --> 00:06:10,650 Again, the name element can be used to specify the column name, but by default, 98 00:06:10,650 --> 00:06:15,670 the field name will be used and again will stick with the default in this course. 99 00:06:15,670 --> 00:06:18,740 Now back up to the ID field. 100 00:06:18,740 --> 00:06:22,800 We'll use a special annotation here to indicate that this field is to serve 101 00:06:22,800 --> 00:06:25,360 as this entities primary key. 102 00:06:25,360 --> 00:06:29,030 Now quite intuitively, it's the ID annotation. 103 00:06:29,030 --> 00:06:32,650 And if you want this value to be automatically generated by Hibernate, 104 00:06:32,650 --> 00:06:36,350 you can use the generated value annotation, 105 00:06:36,350 --> 00:06:40,670 specifying the strategy to be used for generating new IDs. 106 00:06:40,670 --> 00:06:44,554 In our case, we'll use GenerationType.IDENTITY. 107 00:06:45,570 --> 00:06:48,290 Now it turns out that since we included an ID 108 00:06:48,290 --> 00:06:52,050 annotation on a field that every other field unless marked as transients. 109 00:06:52,050 --> 00:06:56,390 Either with a JAVA modifier or with the transience annotation. 110 00:06:56,390 --> 00:06:58,970 Will be automatically mapped to a database column 111 00:06:58,970 --> 00:07:02,530 even without these column annotations. 112 00:07:02,530 --> 00:07:03,620 I'll leave them there though. 113 00:07:03,620 --> 00:07:06,300 Just for purposes of demonstration. 114 00:07:06,300 --> 00:07:08,500 One final task before we take a break. 115 00:07:08,500 --> 00:07:12,510 I mentioned earlier that we'd be returning to that XML configuration file. 116 00:07:12,510 --> 00:07:14,520 Let's do that now. 117 00:07:14,520 --> 00:07:16,890 There are two more elements that we need to add. 118 00:07:16,890 --> 00:07:21,000 The first calls upon a Hibernate tool to generate our database schema 119 00:07:21,000 --> 00:07:23,300 based on our annotated entities. 120 00:07:23,300 --> 00:07:25,090 Let me stick a comment in there. 121 00:07:25,090 --> 00:07:29,080 That references what this property is going to be doing. 122 00:07:29,080 --> 00:07:32,960 So we want to create the database schema. 123 00:07:34,260 --> 00:07:35,350 On application start up. 124 00:07:37,020 --> 00:07:41,060 And to accomplish this we are going to use another property. 125 00:07:42,240 --> 00:07:49,177 The name of this property is going to be hbm2ddl.auto. 126 00:07:49,177 --> 00:07:54,010 And the value we're going to use here is create. 127 00:07:54,010 --> 00:07:59,990 Now hbm2ddl stands for hibernate mapping to data definition language. 128 00:07:59,990 --> 00:08:02,730 This particular setting tells hibernate to 129 00:08:02,730 --> 00:08:06,830 create the schema upon starting the application. 130 00:08:06,830 --> 00:08:10,270 It will do this by reading the hibernate configuration file, looking for 131 00:08:10,270 --> 00:08:15,050 mapped entity classes, and then examining those classes, JPA annotations, 132 00:08:15,050 --> 00:08:17,990 to create database tables and columns. 133 00:08:17,990 --> 00:08:20,720 You might wonder then, how does this configuration file 134 00:08:20,720 --> 00:08:24,270 inform hibernate that we have a mapped entity class? 135 00:08:24,270 --> 00:08:28,230 The answer is that we add a mapping element indicating the fully qualified 136 00:08:28,230 --> 00:08:30,080 name of the entity class. 137 00:08:30,080 --> 00:08:34,200 I say fully qualified because that name must include the package name as well. 138 00:08:34,200 --> 00:08:38,870 Let me get a comment in here for this mapping element. 139 00:08:41,138 --> 00:08:46,870 This is going to name the annotated entity classes. 140 00:08:48,420 --> 00:08:51,790 In our application we'll have only one class. 141 00:08:51,790 --> 00:08:56,830 This XML element is called mapping and it has a single attribute named class and 142 00:08:56,830 --> 00:09:00,990 it has no inner text so I'm going to create it as a self closing element. 143 00:09:00,990 --> 00:09:05,990 As I said before, the name of this class has to be fully qualified which means it 144 00:09:05,990 --> 00:09:12,300 includes the package name, in our case com.teamtreehouse.contactmgrif 145 00:09:12,300 --> 00:09:16,420 the.model.Contact, and there is the fully 146 00:09:16,420 --> 00:09:21,079 qualified name of our entity class that includes our JPA annotations. 147 00:09:23,070 --> 00:09:26,120 Hey great work on getting things ready to go for hibernate. 148 00:09:26,120 --> 00:09:29,940 Why don't we stop for a quick check for understanding before moving on to create 149 00:09:29,940 --> 00:09:34,070 the hibernate objects that will connect our application to a maps database.