filmov
tv
Hibernate Tutorial #11 - Hibernate Annotations - Part 1

Показать описание
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
In this video we'll annotate a JAVA class. Just as a checkpoint with our Hibernate Development Process, our To Do List, in the previous video we covered the Hibernate configuration file and how to add it to our project.
Now in this video were going to focus on annotating the JAVA class. As far as terminology, Hibernate has it's concept of an entity class. What's an entity? It's really just a fancy name for a JAVA class that is mapped to a database table. It's really just a plain old JAVA class with fields and getters and center methods and then you simply add annotations on it to help with mapping it to a database table.
You'll hear the term thrown around entity class, but think of it as a regular old JAVA class or a POJO with special annotations on it.
There's a diagram here with the object to relational mapping or ORM. On the far left we have a JAVA class for a student and it has fields for the ID, first name, last name and email. We want to take this class and we want to map it to a database table over on the far right. We have this database table called Student and it has an ID, first_(underscore)name, last_name, and also an email address. We need to somehow tell Hibernate how to map this class to the actual table and also how to map fields to the actual columns; that's where we come into the whole mapping aspect of it.
Just to let you know, there's two options for mapping a class; option one is the XML.config file. I want to say this is more of a Legacy approach, this was used heavily when Hibernate first came out. However there's also the option two, using JAVA annotations; this is the more modern approach, the preferred approach. In this tutorial series, I'm only going to show you how to do option two, JAVA annotations. If you want information on the old school Legacy approach, just go search the internet, there's plenty of old examples out there that show you how to do it the long way. I want to show you the modern, preferred way of using JAVA annotations.
How do we do annotations? The first step is that we need to map the class to a given database table, then in step two we'll map the fields to database columns. I'll show you how to do this step by step in the following slides.
Let's start with step one, mapping a class to a database table. Over on the far left you see our public class student, this is a regular old JAVA class but notice the first two lines. We're going to add what they call annotations. In JAVA, an annotation has an @ (at) symbol and the name of that annotation. In this case, we're actually going to have is two annotations. The first one is @Entity, just to let them now that this is an entity that we're going to map to a database table. Then the second item is @Table, we get the actual name of the database table we're mapping this class to.
In this example, the @Table name equals student is actually optional. In our case, the JAVA class name has the same name as the database table. Just for completeness, I always like to give the explicit name here just so we know exactly what we're mapping to. That's the piece there, we set up @Entity and @Table and we're mapping this class to a given table. That covers step one.
Moving ahead to step two, this is where we're going to map the fields to database columns. We have our class student, it has two fields; it has it's ID and a first name. For the ID, the ID is actually the primary key for our class, so kind of think of the student as having a student ID or something, it's an unique number; it's the primary key. Here we might use @ID, so that signifies that this is a primary key for this class and we have @Column where you get name =(equals) ID. I'm going to specify that this field called ID maps to a database column called ID.
The column name for the database could be actually different, it could be student_ID or social security number or unique ID or something.
This is the end of the Hibernate tutorial transcript
----
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
In this video we'll annotate a JAVA class. Just as a checkpoint with our Hibernate Development Process, our To Do List, in the previous video we covered the Hibernate configuration file and how to add it to our project.
Now in this video were going to focus on annotating the JAVA class. As far as terminology, Hibernate has it's concept of an entity class. What's an entity? It's really just a fancy name for a JAVA class that is mapped to a database table. It's really just a plain old JAVA class with fields and getters and center methods and then you simply add annotations on it to help with mapping it to a database table.
You'll hear the term thrown around entity class, but think of it as a regular old JAVA class or a POJO with special annotations on it.
There's a diagram here with the object to relational mapping or ORM. On the far left we have a JAVA class for a student and it has fields for the ID, first name, last name and email. We want to take this class and we want to map it to a database table over on the far right. We have this database table called Student and it has an ID, first_(underscore)name, last_name, and also an email address. We need to somehow tell Hibernate how to map this class to the actual table and also how to map fields to the actual columns; that's where we come into the whole mapping aspect of it.
Just to let you know, there's two options for mapping a class; option one is the XML.config file. I want to say this is more of a Legacy approach, this was used heavily when Hibernate first came out. However there's also the option two, using JAVA annotations; this is the more modern approach, the preferred approach. In this tutorial series, I'm only going to show you how to do option two, JAVA annotations. If you want information on the old school Legacy approach, just go search the internet, there's plenty of old examples out there that show you how to do it the long way. I want to show you the modern, preferred way of using JAVA annotations.
How do we do annotations? The first step is that we need to map the class to a given database table, then in step two we'll map the fields to database columns. I'll show you how to do this step by step in the following slides.
Let's start with step one, mapping a class to a database table. Over on the far left you see our public class student, this is a regular old JAVA class but notice the first two lines. We're going to add what they call annotations. In JAVA, an annotation has an @ (at) symbol and the name of that annotation. In this case, we're actually going to have is two annotations. The first one is @Entity, just to let them now that this is an entity that we're going to map to a database table. Then the second item is @Table, we get the actual name of the database table we're mapping this class to.
In this example, the @Table name equals student is actually optional. In our case, the JAVA class name has the same name as the database table. Just for completeness, I always like to give the explicit name here just so we know exactly what we're mapping to. That's the piece there, we set up @Entity and @Table and we're mapping this class to a given table. That covers step one.
Moving ahead to step two, this is where we're going to map the fields to database columns. We have our class student, it has two fields; it has it's ID and a first name. For the ID, the ID is actually the primary key for our class, so kind of think of the student as having a student ID or something, it's an unique number; it's the primary key. Here we might use @ID, so that signifies that this is a primary key for this class and we have @Column where you get name =(equals) ID. I'm going to specify that this field called ID maps to a database column called ID.
The column name for the database could be actually different, it could be student_ID or social security number or unique ID or something.
This is the end of the Hibernate tutorial transcript
Комментарии