The technical principle analysis and application instance of the "Hibernate Commons Annotations" framework in the Java class library
The technical principles of the "Hibernate Commons Annotations" framework in the Java class library
Overview
Hibernate is an object relationship mapping (ORM) framework that is widely used in the Java platform to map the Java class and databases.Hibernate Commons Annotations is part of the Hibernate project, which provides a set of expansion based on Java annotations to define the mapping relationship of Hibernate entity and other persistent related configuration information.
Technical principle
Hibernate Commons Annotations provides a series of annotations based on Java's metastical solution mechanism to define the persistent configuration of the Hibernate entity class.Using these annotations, developers can directly use annotations in the physical class to mark their attributes, associations and other mapping information.When running, the Hibernate framework will analyze these annotations, and automatically generate SQL statements based on the configuration information defined by the annotation to achieve mapping between objects and databases.Here are some commonly used Hibernate Commons Annotations:
1. @entity: Used to mark a class as a Hibernate entity, indicating that this class will be persisted into the database.
2. @Table: Used to specify the database table name corresponding to the physical class.
3. @ID: The unique identifier for marking a attribute as an entity.
4. @GENERATEDVALUE: Used to specify the primary key to generate strategy.
5. @Column: The database name and data type for specifying the corresponding attribute.
6.@Manytoone,@Onetomany,@Onetoone,@Manytomany: For the association relationship between entities.
7. @Joincolumn: Database outside the database outer key column used to specify the relationship.
Applications
Below is a simple example of using Hibernate Commons Annotations:
First of all, the dependence of the introduction of Hibernate Commons Annotation:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>xxx</version>
</dependency>
Create a Hibernate entity User.java:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
// getters and setters
}
In the above example, we use @entity annotations to mark the User class as a Hibernate entity, and use the @Table annotation to specify the database table named "Users" corresponding to the entity.@Id and @GENERATEDVALUE annotations are used to define the primary key attribute ID of the entity, and specify the primary key generating strategy to automatically increase.@Column annotation is used to specify the database list corresponding to the attribute.
Then, we can write a code to save a user entity to the database:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
User user = new User();
user.setUsername("john");
user.setPassword("password");
session.save(user);
session.getTransaction().commit();
session.close();
In this code, we first created a SessionFactory object and configure the Hibernate connection information by configured the file.Then, we got the session of the current thread and opened a transaction.Next, we created a User entity object and assigned its attributes.Finally, we call the SaVE method of Session to save the entity into the database and submit it.Finally, we closed session.
Summarize
Through the Hibernate Commons Annotations framework, we can use annotations to simplify the configuration of the Hibernate entity class and avoid the tedious XML configuration files.This makes the use of Hibernate more convenient and flexible.It is hoped that the technical principle analysis and application instances provided in this article can help readers better understand and use the Hibernate Commons Annotations framework.