Hibernate operations Database 3 Preparation 7 steps __ Database

Source: Internet
Author: User
Tags commit

7 Steps to using Hibernate
The interview encountered this problem, for a long while on these things
The Hibernate.cfg.xml file must be placed in your classpath.

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

1.//Three Preparation
1. Add Hibernate support
2. Create a Hibernate.cfg.xml file
3. Creating objects and Mapping files
2.
3.//Seven Steps
4.//1. Read and parse the Hibernate.cfg.xml file------load File
5. Configuration cfg = new configuration (). Configure ();
6.//2. Read and parse HBM files (all mapping corresponding HBM files)------After loading, create objects
7. Sessionfactory SF = Cfg.buildsessionfactory ();
8.//3. Open session, Execute object------equal to the connection received
9. Session session=sf.opensession ();
10.//4. Start a transaction
One. Transaction tran=null;
try {
Tran=session.begintransaction ();
14.//5. Long-lasting operation
User user=new User ("AA", "123");
Session.save (user);
17.//6. Commit a transaction
Tran.commit ();
19.
.} catch (Hibernateexception e) {
21.//6. Back to Business
Tran.rollback ();
E.printstacktrace ();
FINALLY{//7 Close Connection
Session.close ();
26.}


Hibernate best practice is to build and cache hibernate factory to improve performance. So we'd better build one in the first and second steps

A struts plug-in to cache hibernate factory in the servlet context. As shown in LIST5:

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.

Related Article

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.