How to use the Hibernate Core RELOCATION framework in the Java class to implement the object relationship mapping
How to use the Hibernate Core RELOCATION framework in the Java class to implement the object relationship mapping
Brief introduction
Hibernate is a popular Java persistence framework that can map objects into the relationship database.It provides simple programming models and object -oriented query language, allowing developers to easily handle the task of data persistence.This article will introduce how to use the Hibernate Core Relocation framework in the Java library to achieve object relationship mapping.
Introduce Hibernate Core Relocation
First, you need to introduce Hibernate Core RELOCATION dependencies in your Java library project.
Maven dependency configuration is as follows:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-relocation</artifactId>
<version>5.4.32.Final</version>
</dependency>
Gradle dependency configuration is as follows:
gradle
implementation 'org.hibernate:hibernate-core-relocation:5.4.32.Final'
Configure hibernate property file
Next, you need to create a Hibernate property file to configure the database connection and other related attributes.
Create a file called `hibernate.properties` under the resource folder, and add the following configuration:
hibernate.connection.url=jdbc:mysql://localhost:3306/mydatabase
hibernate.connection.username=root
hibernate.connection.password=password
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
In the above examples, you need to modify the values of the attributes such as `hibernate.connection.url`,` hibernate.connection.username` and `Usernate.Connection.password`.
Define the physical class
In Hibernate, you need to define a physical class to map the table in the database.A simple physical class example is as follows:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int age;
// Constructors, getters, and setters
}
In the above examples, the `@Entity` annotation is used to identify that this is a Hibernate entity class,`@ID` is used to specify the primary key attribute, `` ``@genonedvalue `annotations are used to specify the genetic strategy of the primary key.
Configure Hibernate session factory
Create a class to configure the Hibernate session factory. This class will be responsible for creating and managing the Hibernate session object.
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
sessionFactory.close();
}
}
In the above examples, we use Hibernate's `Configuration` class to load the Hibernate configuration file, and then use the` StandardServiceRegistrybuilder` class to build a session factory.
Perform database operations
Now, you can use Hibernate for database operations in your Java library.The following is a simple example code, which is used to save a new user in the database:
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
User user = new User();
user.setName ("Zhang San");
user.setAge(25);
session.save(user);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
HibernateUtil.shutdown();
}
}
In the above examples, we can obtain a Hibernate session object through the `hibernateutil.getSessionFactory (). OpenSession ()`.
Summarize
By introducing the Hibernate Core Relocation framework, the Hibernate property file is configured, the physical class is defined, the Hibernate session factory, and the execution of the database operation. We can implement the object relationship mapping in the Java library.Hibernate provides a more convenient and flexible way to process the durable data, allowing developers to focus on business logic without much attention to the underlying database operation.
I hope this article will help you understand how to use the Hibernate Core Relocation framework to achieve the object relationship mapping.If you have any questions, please ask me at any time.