Hibernate Tutorial #12 - Hibernate Annotations - Part 2

preview_player
Показать описание
FULL COURSE: Spring MVC and Hibernate (200+ videos)

----

This Hibernate tutorial series will help you quickly get up to speed with Hibernate.

----

DOWNLOAD SOURCE CODE for Hibernate tutorial.

----

----

Follow luv2code for more Hibernate tutorials:

---

If you liked my Hibernate tutorial, then join my mailing list: Get exclusive access to new Java tutorials.

---

Questions or problems about this Hibernate tutorial? Post them in the comments section below.

---

Want to suggest a video for my Hibernate tutorial? Leave a comment below. I'm always looking for new video ideas.

Let me know what video you'd like for me to create for this Hibernate tutorial.

---

Hibernate Tutorial Transcript

All right. Let's go ahead and get our hands dirty. Let's go ahead and move into Eclipse. We're actually going to write the code to annotate our Java class.

The first thing I want to do is I want to create a new package to hold this class or this entity. What I'll do is I'll simply, just right-click on this package that I have ... Actually, I'll right-click on "source." I'll say, "new package." The name I'll give for this package, I'll call it, "Com.LovetoCode.Hibernate.Demo.Entity"

Again, I'm just creating a new package where I'm going to place my entity class. Then I'll hit "finish."

All right. Great. I have this new package. It's empty right now. Let's go ahead and add a new class to this package. On this Demo.Entity, I'll do a right-click. I'll say, "new class." The name of the class that we're going to create is called "student." I'll keep all the other defaults here for this dialogue. I'll go ahead and hit the "finish" button.

Okay great. We have this very, basic class. Let me go ahead and expand the window here. Give myself some space to work in. Need some room to stretch. The first step, we're going to map the class to a database table. Just like we saw in the slides, the first thing I'll do here is I'll make use of this @Entity. Then I'd say, @Table(name="student").

Again, this basically maps this class or this entity to a given table in our database. That's called "student." Now, let me fix the imports here. I'll show you another technique. I'll simply right-click. I'll go to "source." I'll choose "organize imports."

Good. We're on track there. Let's go ahead. One thing that we need to do is we need to create a no-arg constructor. Again, the constructor, same name as the class, "public student." That's our no-arg constructor, very simple, very straightforward. We have our class mapped to a table. Let's go ahead and set up our fields.

First off, I'll just go ahead and write out our fields for this class. I have ID, the first name; I also have the last name. Then finally, we have the actual email address. This is all very basic stuff. You've probably seen this before in some of my previous video courses.

All right. Getting the little white space here at the bottom. The next step here is to actually map these fields to database columns. For this student, the ID is going to be our actual primary key. We make use of the @id. Then we say @column. Then we give the actual column name we're going to use, so @column(name="id"). That's the actual name of the column in the database.

Now, let's go ahead and annotate all these other fields here. I'll just do a copy paste on column. Here I'll say, (name="First_name"). Again, column is the actual name of the database column. Then last_name. Then finally, here, I'll say email. All right. This looks pretty good. This is basically annotating or mapping these Java fields to the appropriate database columns. Again, the actual database column names could be anything as long as you map them explicitly here using the annotation.

This is the end of the Hibernate Tutorial transcript
Рекомендации по теме
Комментарии
Автор

It is very great. You are the best teacher i saw in youtube

saidchaoufi
Автор

Best (non-game) programming tutorials I've seen ... very clear, well presented, and useful.

BlackJar
Автор

Besides teaching hibernate you have done excellent job. You really make great videos. Great job. God bless you.

anuragsoni
Автор

Hey man, awesome job! Short, precise, and sweet.

neel_epoch
Автор

Sir, i will buy your course today, it`s the best course ever i seen, and your pronunciation is perfect, my English is not so good, but i understand without any problem!! My recommendation, give table name in plural like "students" or "users".

tarasmalinovskyy
Автор

Friggen awesome video series! Thanks so much 👏👏👏👏👏

rosshoyt
Автор

Your tutorials are great. Thanks for good contents

shaypatrickcormac
Автор

Sir, Why do we need a no arg constructor?

successshrestha
Автор

hi Chad
Does one have to run a hibernate application through the browser ?

edwinmwangi
Автор

The organise import option is importing org.hibernate.Annotation instead of javax.persistence. Please help.

shrutipant