To establish a connection to the database with Hibernate, prepare the following:
1. Create an entity class: An entity class (also called a persisted Class) is a JavaBean class with some attributes, and the entity class accesses the property by using the standard JavaBean naming convention (Getterand setter method) while setting the access level of the class property to private. In order to instantiate the object of the class through the reflection mechanism, we need to provide an argument-free constructor, all entity classes require a parameterless constructor, because hibernate needs to use the Java reflection mechanism to create objects for you. Finally, implement the Java.io.Serializable interface for the entity class so that hibernate can better cache the entity objects.
2. Create and configure a mapping file: Through the entity mapping file, Hibernate knows how to load and store objects in the entity class, know which tables in the database should be accessed, and which fields in the table should be used.
The sample code is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate -mapping-3.0.dtd ">
Primary key mapping: In an Entity class mapping file, the mapped class must define the properties of the primary key field for the corresponding database table. The <id> element of the mapping file defines the mapping of the attribute to the primary key field of the database table.
Primary key generation policy: The primary key generator generator is optional and is used to specify the identifier of the entity object (called the primary key in the table) generation policy.
3. Add the path to the entity mapping class file in the master configuration file:
Add the path information for the mapping file to Hibernate.cfg.xml: <mapping resource= "***/***/***.hbm.xml"/>
4. Test if the connection is successful.
After the connection is successful, hibernate can be used to manipulate the database.
The seven steps of Hibernate operations database are as follows:
1. Get config configuration
2. Create sessionfactory (Session factory)
Session factories are heavyweight, single-instance, and are frequently referenced because of the high cost of creation, so it is best to create only one instance and initialize it once in the static initialization block.
3. Open session
4. Start a transaction
5. Persistent operation
6. Commit a transaction
7. Close the session.
The sample code is as follows:
1. Get config
cfg = new configuration (). Configure ();
Serviceregistry serviceregistry = new Serviceregistrybuilder ()
. Applysettings (Cfg.getproperties ()). Buildserviceregistry ();
2, create sessionfactory
sessionfactory factory = cfg.buildsessionfactory (serviceregistry);
3, Get session session Session
= Factory.opensession ();
Transaction tx = Session.gettransaction ();
try {
//4, open transaction
tx.begin ();
Seeker Seeker = new Seeker ();
Seeker.setemail ("abc@163.com");
Seeker.setpassword ("abc");
5, Save Seeker object
Session.save (seeker);
6. Commit Transaction
session.gettransaction (). commit ();
} catch (Exception e) {
tx.rollback ();
E.printstacktrace ();
} finally {
//7, close session
Session.close ();
}
}
Hibernate's core interface: 1.Configuration Interface Configuration objects are used to configure and boot Hibernate, a hibernate application uses a configuration instance to specify the location of the master configuration file, and then creates a session factory. Configuration cfg = new configuration (). Configure ("/cfg/auction.cfg.xml") 2.SessionFactory interface in general, The entire app has only one sessionfactory, which should be created when the app is initialized. Write a tool class that sets the instance of Sessionfactory to a static member (only one in memory) and initializes it in a static initialization block (once only). 3.Session Interface The session interface is the primary interface used by Hibernate applications and has a series of APIs that manipulate persisted objects, which can be used to manage (for example, load and save) objects, so we also call it the persistence Manager. Session instances are lightweight, and the cost of creating and destroying them is not expensive, usually we create a session instance for each transaction and close it after use. 4.Transaction (transaction) interface The Transaction (transaction) interface is an abstraction of the actual transaction implementation, which includes JDBC transactions or JTA transactions. Commit () and rollback () Two methods are primarily defined in the transaction interface, which is the method of committing the transaction, which is the method of rolling back the transaction. 5.Query and Critera interface The query interface allows queries to be executed on the database and controls how the query executes. The query uses HQL or SQL. The query instance can be used to bind queries, limit the number of results returned by the query, and ultimately execute the query.