Hibernate operations Database 3 Preparation 7 steps

Source: Internet
Author: User

There are several steps to using hibernate in your program:

1. Set up a Hibernate configuration object

2. Use Hibernate configuration object to create a Hibernate factory object.

3. Use Hibernate factory object to set up a Hibernate session object.

4. Use Hibernate Session object to start a transaction (optional)

5. Use Hibernate session object to set up, read, update, delete data in the database

6. Commit the transaction (optional)

7. Close session

Three preparations
Add Hibernate support
Create a Hibernate.cfg.xml file
creating objects and Mapping files

Seven Steps
1. Read and parse the Hibernate.cfg.xml file------load File
Configuration cfg = new configuration (). Configure ();
2. Read and parse HBM files (all mapping corresponding HBM files)------After loading, create objects
Sessionfactory SF = Cfg.buildsessionfactory ();
3. Open session, Execute object------equivalent to the connection
Session session=sf.opensession ();
4. Start a transaction
Transaction Tran=null;
try {
Tran=session.begintransaction ();
5. The operation of long-time
User user=new User ("AA", "123");
Session.save (user);
6. Commit a transaction
Tran.commit ();

} catch (Hibernateexception e) {
6. Back to the transaction
Tran.rollback ();
E.printstacktrace ();
} FINALLY{//7 Close the connection
Session.close ();
}


Hibernate is a very powerful product, and there are some unknown features left for you to discover. Our simple example is just about

Read this behavior, but the other functions in crud are just as simple. Functional updates are as simple as reading a specified object, calling

JavaBean Setter, invoke the session's commit method. Hibernate is responsible for generating SQL statements and updating your database. A deleted

In addition to being very simple-session.delete (element) is all you have to do! The final build just needs to initialize the object, call

Setters method, and then calls Session.save (Element).

Hibernate best practices recommend caching Hibernate factory objects. We chose to build and cache through struts plug-in

Factory You can also choose to use other methods to cache it in your class.

While this excerpt is good enough for your needs, there are other drawbacks to it. First, we used the struts action in the

Hibernate. Migrating to other persistent layer frameworks will require us to change each action that uses hibernate. Second, we hold

The long layer is closely connected with the presentation layer. This association allows us to have no opportunity to reuse the persistence layer logic in other presentation layer mechanisms, such as batch

Handler.

While there is a lot of room for improvement, this excerpt works well when you don't need to reuse the presentation layer.

Hibernate operations Database 3 Preparation 7 steps

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.