Struts2 + Spring4.0 + Hibernate4.0 study 3
Next we will mainly learn about the persistence layer of Hibernate:
By using the Hibernate Persistence Layer, you can avoid using the traditional JDBC Method to operate the database. By using the O/R Mapping support provided by Hibernate, this allows programs to operate relational databases in an object-oriented manner, ensuring that the entire software development process is conducted in an object-oriented manner, namely, analysis-oriented, object-oriented design, and object-oriented programming.
A. Design persistence entities
Object-Oriented Analysis refers to extracting objects from applications according to system requirements, abstracting these objects into classes, and then extracting the classes that require persistent storage, these classes to be persistently saved are persistent objects (PO ). The system has not designed databases in advance, but has designed seven persistence classes completely from the object-oriented perspective.
1. Application: applies for the attendance of ordinary employees, including the reason for Application, whether the Application is approved, and the type of Application change.
2. Attend: corresponds to the daily attendance, including attendance time, attendance staff, whether to go to work, attendance type, and other information.
3. AttendType: the type of attendance, including the name of attendance, such as late arrival or early departure.
4. CheckBack: The corresponding approval, including the application corresponding to the approval, whether the application is approved, and the manager who completes the approval and other attributes.
5. Employee: information about the employees in the system, including the username, password, salary, and corresponding manager of the employees.
6. Manager: information about the system Manager, which only contains the Department name managed by the Manager. In fact, the Manager inherits the Employee class, so the class also contains all attributes of the Employee.
7. Payment: the monthly salary information, including the month of the pay-as-you-go, the number of paid employees, and other information.
The domain objects in the system are very simple when adopting the structure model of the anemia model. They are all simple data classes and do not need to consider which business logic methods should be included. Therefore, the development is very convenient;
Objects in the considerable world do not exist in isolation, and the above seven PO classes do not exist in isolation. There is a complex association between them. Analyzing associations is not only a necessary step for object-oriented analysis, but also the only way for Hibernate to perform persistence operations. The associations between the seven PO are as follows.
Class relationship diagrams between the seven PO:
B. Create a Persistent Object Class
We can see that the associations between persistent objects are expressed in the form of member variables. Of course, these member variables also need the support of the setter and getter methods, the associations between persistence Classes usually correspond to the master and foreign key constraints in the database.
First, let's take a look at the knowledge and persistence requirements that will be used. Hibernate adopts a local intrusive design, but it should follow the following rules:
1. A parameter-free Constructor is provided. Hibernate can use Constructor. newInstance () to create a persistent class instance.
2. Provide an identity attribute: the identity attribute is usually mapped to the primary key field of the database table.
Use persistent annotations to change POJO to the PO class.
@ Entity: The POJO modified by this annotation is an Entity.
@ Table: This annotation specifies a Table with multiple Mappings of persistence classes.
Hibernate can fully understand Object-oriented Inheritance, polymorphism, and other concepts. Once a correct inheritance ing is established, the program can access the database in an object-oriented manner.
The SessionFactory of Hibernate can hold an optional level-2 cache, which can improve the persistence access performance of Hibernate.