Hibernate Introduction to Core technology
1 , Hibernate Mapping File Development
the hibernate mapping file is the *.hbm.xml file in the project, which mainly completes the configuration of each element, including root element, class element, defining primary key, setting primary key generation method, defining attributes, and associating mappings. The following one by one separate descriptions:
(1) root element
The root element of the mapping file is
The
(2) Class elements <class>
<class> is a sub-element of
(3) Defining a primary key
In a relational database table, the primary key is used to identify records and to guarantee uniqueness of each record. In the Java language, you can determine whether two objects are equal by comparing the memory addresses of the objects referenced by the two variables, or by comparing the values of the objects referenced by two objects. Hibernate to resolve the differences between the two, use the object identifier (OID) to identify the uniqueness of the object. The OID is the equivalent of the primary key in the relational database in the Java object model. At run time, Hibernate maintains the correspondence between the Java objects and the records in the database tables based on the OID.
The primary key in the Java class is typically defined using the primary key attribute ID in the database table, which is a one by one correspondence.
(4) Setting the primary key generation method
The <generator> element is typically used to specify the corresponding primary key, such as the following example code in which the ID is the primary key:
<hibernate-mapping Package= "Com.demo.hibernate.beans"> <classname= "User"Table= "User"> <IDname= "id"column= "ID"type= "integer"> <Generatorclass= "Native"/> </ID> < Propertyname= "username"column= "username"type= "string" /> < Propertyname= "Password"column= "Password"type= "string" /> < Propertyname= "Email"column= "Email"type= "string" /> </class> </hibernate-mapping>
(5) Defining attributes
In a database table, each property corresponds to a corresponding data type, so the Java class generated by the ORM Association mapping will have a corresponding data type for each member variable. For example, int, long, String
, char, date, text, and so on, where date and text are not data types in Java code, but here are the built-in mapping type properties for hibernate mappings.
You can typically use the <property> element to define non-primary key attributes.
(6) Association mappings
Association mappings resemble the correspondence between entities in a relational database, but the relationship between the persisted classes generated after the mapping represented here contains the following types of:<many-to-one>, <one-to-one>, <one-to-many >, <many-to-many>.
2 , Hibernate Core Programming
When developing persistence-based applications with Hibernate, the first thing to do is to be familiar with its programming interfaces. Mainly includes the use of the following four core classes:
Configuration load Config class
Sessionfactory Creating Session Classes
Session database Operation class
Transcation transaction Operation class
Common Core interfaces include the following six: Session, Sessionfactory, Configuration, Transaction, query, and criteria. Its specific relationship is as follows:
Diagram One: The generation of interface classes
Here's a brief description of the role of the four core classes:
(1) Configuration load Config class
The purpose of the configuration interface is to configure Hibernate and start it. During Hibernate startup, an instance of the configuration class first locates the location of the mapped document, reads the configuration, and then creates a Sessionfactory object.
(2) Sessionfactory Create session Class
Sessionfactory actually played a buffer in hibernate, buffering hibernate's automatically generated SQL statements and other mapping data, and buffering some data that might be reused in the future.
(3) Session database operation class
The main function of the session is to provide creation, read, and delete operations on the mapped entity class instances.
(4) transcation Transaction Operation class
The transaction interface is an abstraction of real-world implementations that include JDBC transactions, usertransaction in Jta, and even CORBA transactions. The purpose of this design is to enable developers to use a unified transaction interface, so that their projects can be easily ported between different environments and containers.
3 , using Query make HQL Statement Query
The query interface implements querying operations on databases and persisted objects, which can be expressed in two ways: HQL language or SQL statement of a local database. Queries are often used to bind query parameters, limit the number of query records, and ultimately perform query operations.
To get the query object, you need to use the session's CreateQuery () function to execute the queries, the parameters of the query are based on the HQL syntax, the object queried is Hibernate's persisted object name, and hibernate finds the name of the table to find based on the object name. The specific application is as follows:
(1) Query without parameters
The code examples are as follows:
Query query = Session.createquery ("from User");
(2) Query with parameters
The code examples are as follows:
Query query = Session.createquery ("from User where Username =: username");
Query.setstring ("username", "admin");
(3) Get the list result set
The code examples are as follows:
List List = Query.list ();
(4) Get Iteration list result set
The code examples are as follows:
Iterator it1 = Query.iterate ();
Iterator it2 = Query.list (). Iterator ();
while (It2.hasnext ()) {
User user = (user) it2.next ();
}
(5) Get an object
The sample code is as follows:
Query query = Session.createquery ("From User where username=?");
Query.setstring (0, "admin");
User user = (user) query.uniqueresult ();
(6) Scalar query
The sample code is as follows:
Iterator results = session.createquery ("Select User.username,count (user.email) from the user user group by User.username"). List (). iterator ();
(7) Paging query
The sample code is as follows:
Query query = Session.createquery ("from User");
Query.setfirstresult (10);
Query.setmaxresult (20);
List List = Query.list ();
(8) Create SQL query
The sample code is as follows:
List users = Session.createsqlquery ("Select {user.*} from User{user}"). List ();
4 , using Criteria make a conditional query
the Criteria interface is very similar to the query interface, which allows you to create and execute object-oriented, standardized queries.
In contrast, if you want to be able to use the API dynamically for object-oriented queries instead of embedding strings in Java code, choosing criteria can do the same. The following are the specific applications:
(1) Create a criteria instance
The instance code is as follows:
Criteria = Session.createcriteria (User.class);
Criteria.setmaxresult (50);
List users = Criteria.list ();
(2) Add query criteria
The instance code is as follows:
Criteria = Session.createcriteria (User.class); Criteria.add (restrictions.like ("username", "admin%"));
Criteria.add (Restrictions.like ("ID", 1,10));
List users = Criteria.list ();
(3) Adding sorting criteria
The instance code is as follows:
List users = Session.createcriteria (User.class). Add (Restrictions.like ("username", "admin"). AddOrder (Order.asc (" Username ")). AddOrder (Order.desc (" password ")). Setmaxresults (). List ();
(4) using the sample query
The sample code is as follows:
User user = new user ();
User.setusername ("admin");
List results = Session.createcriteria (user.class). Add (Example.create (User)). List ();
Hibernate Core Technology Brief