Mapping Entities
In Hibernate, the process of mapping Java objects to database tables is achieved through the use of annotations. Hibernate provides a set of annotations that can be used to define the various aspects of the mapping.
Let's take a look at an example of how to map a Java entity class to a database table using Hibernate annotations:
1@Entity
2@Table(name = "employees")
3public class Employee {
4
5 @Id
6 @GeneratedValue(strategy = GenerationType.IDENTITY)
7 private Long id;
8
9 @Column(name = "first_name")
10 private String firstName;
11
12 @Column(name = "last_name")
13 private String lastName;
14
15 private double salary;
16
17 // Getters and setters
18
19}
In this example, we have a Employee
class that is annotated with @Entity
and @Table(name = "employees")
. The @Entity
annotation marks the class as an entity, indicating that it should be mapped to a database table. The @Table
annotation specifies the name of the table in the database.
Each field in the Employee
class is annotated with @Column
, which specifies the column name in the database table. By default, Hibernate will use the field name as the column name, but we can override it using the name
attribute of the @Column
annotation.
The id
field is annotated with @Id
and @GeneratedValue
, indicating that it is the primary key of the table and its value will be automatically generated by the database.
To persist an instance of the Employee
class to the database, we can use the following code:
1// Create an instance of the SessionFactory
2SessionFactory sessionFactory = new Configuration()
3 .configure()
4 .buildSessionFactory();
5
6// Create a session
7Session session = sessionFactory.openSession();
8
9// Begin a transaction
10session.beginTransaction();
11
12// Create a new entity object
13Employee employee = new Employee();
14employee.setFirstName("John");
15employee.setLastName("Doe");
16employee.setSalary(5000);
17
18// Save the entity to the database
19session.save(employee);
20
21// Commit the transaction
22session.getTransaction().commit();
23
24// Close the session
25session.close();
26
27// Close the SessionFactory
28sessionFactory.close();
In this code, we create an instance of SessionFactory
and open a session. We begin a transaction, create a new Employee
object, set its properties, and then save it to the database using the save()
method of the session. Finally, we commit the transaction, close the session, and close the SessionFactory
.
Mapping entities in Hibernate is a fundamental step in using Hibernate to interact with the database. It allows us to define the structure and relationships of our data model in Java classes and have Hibernate handle the persistence details.
xxxxxxxxxx
}
class Main {
public static void main(String[] args) {
// Create an instance of the SessionFactory
SessionFactory sessionFactory = new Configuration()
.configure()
.buildSessionFactory();
// Create a session
Session session = sessionFactory.openSession();
// Begin a transaction
session.beginTransaction();
// Create a new entity object
Employee employee = new Employee();
employee.setFirstName("John");
employee.setLastName("Doe");
employee.setSalary(5000);
// Save the entity to the database
session.save(employee);
// Commit the transaction
session.getTransaction().commit();
// Close the session
session.close();
// Close the SessionFactory