From the course: Java Persistence with JPA and Hibernate
Defining an entity - JPA Tutorial
From the course: Java Persistence with JPA and Hibernate
Defining an entity
- [Instructor] Now, we are going to define the very first entity. First of all, run the MariaDB in the SQL script if you haven't done so. So go to mariadb-init.sql file and run it. (keyboard clicks) And now, if you go to the library database by going to SQL Tools and expanding the database, here's the library database. There are two tables, book and author, and here's the book table. It's a simple table with book_id, book_name and ISBN number. When you are working with an ORM framework like Hibernate, you should represent all the database tables or entities as classes in the application. So, let's represent the book table as a class in our application. So, go to Explorer and create a new package named entities inside the app folder, so go to src, java and select the app folder and right click on it. New Folder. Name it entities. In the entities package, create a class named book by right clicking New Java File and Class. Type the name Book as the class name and here's your Book class. Now, there should be a way to specify that this class is modeling a database table or an entity. How do you do that? You have the @Entity annotation to the rescue. So, mark the book class with the @Entity annotation. It's a piece of metadata or additional information to tell the Jakarta Persistence implementation or ORM framework, which in this case is Hibernate, that this is a class that models a table in the database. You can but not required to specify the name attribute. What you should understand is that it's not the name of the table, but it is just giving a name of how the entity instance is represented in the Persistence context. Remember that every record in the table is represented as an entity instance in the Persistence context. However, you don't usually need to specify it. Then the @Table annotation is an optional annotation. It is used to ensure that the ORM implementation or Hibernate knows how the table that corresponds to this entity class is named in the database. In this case, the class name starts with a capital letter and the name of the table is all lowercase. So though they are the same, they are not identical. Therefore, I'm specifying the @Table annotation with the name Book, which is the table name. So let's type name = "book" in lowercase. However, if both the name of the class and the name of the table are identical, this annotation need not be used. That is, for example, if the table was named Book with an uppercase B. One more thing, the table name would be the name used by the queries behind the scenes. So, that's how you define a Jakarta Persistence entity.
Contents
-
-
-
-
-
-
Defining an entity3m 59s
-
(Locked)
Mapping entity fields to table columns3m 57s
-
(Locked)
Specifying the primary key4m 57s
-
(Locked)
Creating a new entity instance in the context5m 39s
-
(Locked)
Challenge: Art Class Management app, part 21m 10s
-
(Locked)
Solution: Art Class Management app, part 22m 27s
-
-
-
-
-
-