Add functionality via Hibernate

Source: Internet
Author: User

 PackageCom.demo.dao;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;ImportCom.demo.Login; Public classLogindao {PrivateSession session=NULL; PrivateTransaction tran=NULL;  PublicLogindao () {//creating a Configuration objectConfiguration config =NewConfiguration (). Configure (); //Create session FactorySessionfactory sessionfactory =config.buildsessionfactory (); //Get Session ObjectSession =sessionfactory.opensession (); }         Public voidSave (Login login) {Try {            //Open Transactiontran=session.begintransaction (); //depositing incoming login objects into the databasesession.save (login); //Commit a transactionTran.commit (); //Prompt InformationSYSTEM.OUT.PRINTLN ("User Information saved successfully!") "); } Catch(RuntimeException e) {
       catching and handling exceptions
if (Null!=tran) {tran.rollback;}
Throwe; }finally{ //Close Session ObjectSession.close (); } }}

In this class, the session and transaction objects are used, the session object is the session object in the Hibernate3.jar package, and all operations on the database are done through the session. The transaction object is used to handle transactions on database operations.

Instantiates a session object in a construction method. The process of instantiation is to find the Hibernate configuration first, then remove the sessionfactory from the configuration, the session factory, and finally remove the session from the Sessionfactory. The Save () method for adding data to the login table is to pass the object of the login type to login as the parameter in the method. At the time of the database operation, it is possible to throw run-time exceptions, all using the try-catch structure to catch exceptions. Then add the login table to the operation.

When you add an operation, the transaction object is instantiated first, which is to open the transaction. It then calls the Save () method of the Session object and passes the object of the login type into the method. The next step is to commit the commit () method of the transaction object to the database and close the session object. If an exception occurs, the transaction is rolled back.

Review the implementation of the added functionality and summarize the steps to perform the persistence operation in Hibernate:

1> Read and parse the configuration file:

Configuration config = new configuration (). Configure ();

2> Read and parse the mapping information and create the Sessionfactory:

Sessionfactory sessionfactory = Config.buildsessionfactory ();

3> Open Session:

Session = Sessionfactory.opensession ();

4> open Transaction (increment, delete, change operation must have, query operation Optional):

Tran = Session.begintransaction ();

5> Persistent Operation:

Session.save (login);

6> COMMIT TRANSACTION:

Tran.commit ();

7> Close session:

Session.close ();

Add functionality via Hibernate

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.