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.