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 ();}}