Hibernate construction, hibernate

Source: Internet
Author: User

Hibernate construction, hibernate
1. The Hibernate. cfg. xml configuration file is used to configure various attributes required for database connection. The default file name is "hibernate. cfg. xml" // Driver Oracle. jdbc. driver. OracleDriver Jdbc: oracle: thin: @ localhost: 1521: WXHS // fixed + local ip: oracle port number: database instance name Scott 123 Org. hibernate. dialect. Oracle10gDialect // dialect, specifying the database type used True False // Ing file configuration, under classpath 2. ORM3. object class public class Dept implements Serializable {private int deptNo; private String dName; private String loc; // get, set Method} 4. Dept. hbm. xml ing File // You must configure the persistence class and table primary key ing. Put. hbm. add xml to the cfg file. hibernate uses public void saveDept () {// manages hibernate Configuration information Configuration config = null; // creates sessionSessionFactory sessionFactory = null; // The Object Persistence operation is equivalent to connection-level cache Session session = null; // start Transaction operation Transaction tr = null; try {// 1. to read and parse the configuration file, find hibernate under src by default. cfg. xmlconfig = new Configuration (). configure ("hibernate. cfg. xml "); // 2. read and parse the ing information and create sessionFactory = config. buildSessionFactory (); // 3. open sessionsession = sessionFactory. openSession (); tr = session. beginTransaction (); // Save the data Dept d = new Dept (); d. setDeptNo (3); d. setdName ("Personnel"); d. setLoc ("Jinan"); session. save (d); Tr. commit ();} catch (HibernateException he) {he. printStackTrace (); tr. rollback ();} finally {session. close ();}}

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.