Hibernate Learning Notes (i) Introduction and implementation of-hibernate configuration files

Source: Internet
Author: User
Tags cdata

First, to recognize hibernate

Disadvantages of 1.JDBC:

1). Write code too cumbersome, try and catch more than 2). No data cache 3). Not object-oriented Programming 4). SQL statement fixed, portability poor

Advantages of JDBC : efficiency is relatively high

2.Hibernate Advantages

1). Fully object-oriented programming 2). Hibernate cache is very cow, level cache, level two cache, query cache 3). Strong cross-platform

4). Many applications in the enterprise internal system

Hibernate Disadvantages

1). Low efficiency 2). Data in the table if it is tens , hibernate is not suitable for 3). If the relationship between tables and tables is particularly complex, hibernate is not suitable

3.ORM

To learn Hibernate, first understand the ORM(object/relation Mapping), object-relational mapping, the main idea is: the relational database in the table records mapped into objects, in the form of objects to show , The programmer can turn the operation of the database into the operation of the object.

ORM usesMeta DataTo describe object-relational mapping details, metadata is typically in XML format and is stored in a dedicated object-relational mapping file

4.Hibernate Development Step Diagram



Two, hibernate configuration file

1.hibernate.cfg.xml and XXX.HBM. Writing of XML

Tell hibernate what database (dialect) to connect the database with, according to

Persistence classes and mapping file policies for generating tables


Within hibernate, there is a type table that has the following mapping relationships:


Therefore, the way to write Java type is more efficient, hibernate type of writing is relatively simple


loading of the 2.hibernate.cfg.xml

public class Hibernateutils {private static sessionfactory factory;static{factory = new Configuration ()////This approach requires attention, Hibernate's profile name must be hibernate.hbm.xml,//and must be placed in the Classpath directory. Configure ()//.buildsessionfactory ();//new configuration ( ). Configure (XXX) can specify the path of the configuration file, the configuration file can be arbitrarily placed}public static sessionfactory Getsessionfactory () {return factory;} public static Session Opensession () {return factory.opensession ();}}

3. Sessionfactory interface

1). Hibernate configuration files, mapping files, persisted class information are in Sessionfactory

2). The information stored in the sessionfactory is shared.

3). Sessionfactory is thread-safe

4). One hibernate frame sessionfactory only one

5). Sessionfactory is a heavyweight class that consumes resources

4. Session Interface

A session is a single-threaded object that interacts between an application and a database and is the center of hibernate operations, and all persistent objects must be managed by the session to be persisted. This object has a short life cycle. The session object has a first-level cache, and the data for all persisted layer operations is slowed down until the session object is persisted until flush is explicitly performed. equivalent JDBC in the Connection.

1). Got a session, equivalent to opening a database connection

2). In Hibernate, CRUD operations on data are performed by the session.

5. Generate Data table

public class Sessionfatorytest {@Beforepublic void init () {Sessionfactory factory =  Hibernateutils.getsessionfactory ();} @Testpublic void Test () {}}
Once the sessionfactory is initialized, the corresponding data table generates a

6. Transaction (business)

represents an atomic operation, which has the concept of a database transaction. All persistence tiers should be under transaction management, and transactions in Hibernate are not automatically committed by default


The connection Setautocommit is set to false, and only the connection is generated for the transaction to operate. So only after the session, can there be transaction

public class Sessionfatorytest {Sessionfactory factory; Session session; Transaction Transaction; @Beforepublic void Init () {factory =  hibernateutils.getsessionfactory (); session = Factory.opensession (); transaction = Session.begintransaction ();} @Testpublic void Test () {User user = new User (); User.setage (User.setname); ("A"); Session.save (User); @Afterpublic void Destory () {transaction.commit (); Session.close ();}}
7. Hibernate execution Process

8. Internal execution schematic diagram









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate Learning Notes (i) Introduction and implementation of-hibernate configuration files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.