Hibernate Learning Notes

Source: Internet
Author: User

Hibenate Development process:

1: Add the relevant jar package;

2:src added Hibernate.cfg.xml (the contents of the document are as follows);

1 <?XML version= ' 1.0 ' encoding= ' UTF-8 '?>2 <!DOCTYPE hibernate-configuration Public3 "-//hibernate/hibernate Configuration DTD 3.0//en"4 "Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">5 6 <!--Generated by MyEclipse Hibernate Tools.  -7 <hibernate-configuration>8 9     <session-factory>Ten     <!--dialect developing a database dialect (different SQL for different databases) One Oracle Org.hibenate.dialect.Oracle9Dialect A MYSQL Org.hibenate.dialect.MySQLDialect - SQL Server Org.hibenate.dialect.SQLServerDialect -      the       - -         < Propertyname= "dialect"> - Org.hibernate.dialect.MySQLDialect -         </ Property> +          -     <!--Specify the URL of the linked database - +         < Propertyname= "Connection.url"> A Jdbc:mysql://localhost:3306/testhib at         </ Property> -     <!--user name and password for the linked library - -          -         < Propertyname= "Connection.username">Root</ Property> -         < Propertyname= "Connection.password">Root</ Property> -          in     <!--Specifies the JDBC driver class for the database - -         < Propertyname= "Connection.driver_class"> to Com.mysql.jdbc.Driver +         </ Property> -  the          *     <!--the values of the HBM2DDL are: $ Create-drop: Run-time creation, finish running DeletePanax Notoginseng Create: Each run will be removed from the existing, created. When testing, use the Create - Update: Map files and tables. Only the data in the table will be updated the Validate: View Map file and table is not corresponding, if not corresponding, will error +          A      - the         < Propertyname= "Hbm2ddl.auto">Update</ Property> +          -     <!--show_sql hibenate Output A statement as a log - $         < Propertyname= "Show_sql">True</ Property> $      -     <!--mapping relation of the object to be sued - -         <MappingResource= "User.hbm.xml" /> the  -     </session-factory>Wuyi  the </hibernate-configuration>
View Code

3: Add Pojo and corresponding mapping files;

4: Add log4j configuration (optional, set debug information and print level);

5: Development Program.

A:uibutil

1 Importorg.hibernate.Session;2 Importorg.hibernate.SessionFactory;3 Importorg.hibernate.cfg.Configuration;4 5 6  Public classHibutil {7     Private StaticSessionfactory Factory;8     9     PrivateHibutil () {Ten          One     } A      -     Static { -Configuration Configuration =NewConfiguration (); the         //configuration.configure ("Hibernate.cfg.xml"); - configuration.configure (); -Factory =configuration.buildsessionfactory (); -          +  -     } +      A      Public Staticsessionfactory getfactory () { at         returnFactory; -     } -      -      Public StaticSession getsession () { -         returnfactory.opensession (); -     } in}
View Code

B:test.java

1 Importjava.util.Date;2 3 Importorg.hibernate.Session;4 Importorg.hibernate.SessionFactory;5 Importorg.hibernate.Transaction;6 Importorg.hibernate.cfg.Configuration;7 8 9  Public classTest {Ten  One     /** A      * @paramargs -      */ -      Public Static voidMain (string[] args) { the         //TODO auto-generated Method Stub -         /* - Configuration conf = new configuration (). Configure (); - Sessionfactory factory = Conf.buildsessionfactory (); + Session session = Factory.opensession (); - Transaction Transaction = session.begintransaction (); + User user = new User ("Xiao", New Date ()); A session.save (user); at transaction.commit (); - session.close (); -         */ -          -Session session =NULL; -Transaction tx =NULL; in         Try{ -Session =hibutil.getsession (); totx =session.begintransaction (); +User U = (user) session.get (user).class, 5);//Get Object -U.setname ("Xi Big");//Modifying Properties the             //session.delete (u); Delete Object *Session.update (U);//Write to Database $ Tx.commit (); Panax Notoginseng}finally{ -             if(Session! =NULL){ the session.close (); +             } A         } the     } +      -  $}
View Code

     

  

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.