Data persistence technical principles in the Javaee API framework
Data persistence technical principles in the Javaee API framework
Data persistence refers to the technology that keeps data still retained in the storage medium after the application is running.In Javaee, some major technologies are used to achieve durable data, including JDBC, JPA, and Hibernate.
1. JDBC (Java database connection)
JDBC is the standard API of the Java connection database. It provides a set of classes and interfaces that allow Java applications to interact with various relationship databases.The working principle of JDBC is to establish a connection with the database, send SQL statements and process the results.The following is a simple JDBC example:
import java.sql.*;
public class JDBCEample {
public static void main(String[] args) {
Connection connection = null;
try {
// Load the database drive
Class.forName("com.mysql.jdbc.Driver");
// Create a database connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
// Create a query statement
String query = "SELECT * FROM users";
// Execute the query
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
// Process query results
while (resultSet.next()) {
String username = resultSet.getString("username");
System.out.println(username);
}
// Turn off the connection
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
2. JPA (Java persistent API)
JPA is a more advanced API. It encapsulates JDBC and provides the function of managing the mapping relationship between the Java object and the database table.The working principle of the JPA is to define the mapping relationship between the physical class and the database table through annotations and XML configuration files, and to handle the interaction with the database through the implementation classes provided by the JPA during runtime.The following is a simple JPA example:
import javax.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
// omit other attributes and methods
}
public class JPASample {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
EntityManager em = emf.createEntityManager();
// Query all users
Query query = em.createQuery("SELECT u FROM User u");
List<User> userList = query.getResultList();
for (User user : userList) {
System.out.println(user.getUsername());
}
em.close();
emf.close();
}
}
3. Hibernate
Hibernate is an open source ORM (object relationship mapping) framework that encapsulates the function of JPA and provides more advanced database operation and performance optimization.The working principle of Hibernate is to perform the mapping relationship between the Java object and the database table in the XML or annotation, and then perform the interactive operation with the database through Hibernate's session.The following is a simple Hibernate example:
import javax.persistence.*;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
// omit other attributes and methods
}
public class HibernateSample {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
// Query all users
Query query = session.createQuery("FROM User");
List<User> userList = query.list();
for (User user : userList) {
System.out.println(user.getUsername());
}
session.close();
sessionFactory.close();
}
}
Summarize:
In the Javaee API framework, data persistence is achieved through technologies such as JDBC, JPA, and Hibernate.As the underlying database connection interface, JDBC provides basic functions of interacting with the database; JPA is a packaging of JDBC, providing more advanced ORM functions; Hibernate is a more powerful ORM framework that enhances JPA.Developers can choose the appropriate technology to achieve data persistence according to their needs.