Simple Hibernate Database Insert Example
Just started learning, I hope to make progress with you. Please don't laugh at me.
This is a simplest example, which is basically made in accordance with Hibernate Reference.
Do this example I choose the application server is the InforWeb of Shandong Zhongchuang Software Commercial Middleware Co., Ltd. (this application server is commercial, if you can't get it, you can replace it with tomcat), the database I choose Oracle, you can Select small mysql.
First you need Hibernate 2.0 related operational environments, you can download from http://hibernate.org/
Step 1: Download Hibernate.
Step 2: Inforweb's Deploy (Tomcat is a webApps) directory first new application, the directory is as follows:% InforWebhome% / Deploy / Cat.
Step 2: Add the driver of the database to the ClassPath or copy the drive to% inforwebhome% / lib (TAMCAT is% Tomcathome% / common / lib);
Step 3: Put the hibernate2.jar provided by Hibernate and some third-party running library to the Hibernate / Web / INF / LIB directory. (These third-party runtuations are included in the downloaded hibernate lib directory).
Let's explain a few must-have:
DOM4J: Hibernate is required to use XML configuration and map meta files.
CGLIB: Hibernate is running using this code generation library enhancement class.
Collectes, Commons Logging:
ODMG4: Hibernate provides an optional ODMG compatible persistent management interface. If you need a map collection, you need this class library, even if you are not using the ODMG API. We are here
There is no collection map in a tutorial, but no matter how this JAR copy is always good.
of.
Although these documents are must, it is still copied all packs, so compare insurance, :).
Step 4: You can configure applications through the InforWeb management tool, add a data source to your application. If you are familiar with deployment descriptors, you can directly modify Server.xml. This file is in the confedment of a connection pool:
TYPE = "javax.sql.datasource" /> parameter> parameter> parameter> parameter> parameter> parameter> parameter> parameter> Resourceparams> Context> In this example we have to configure the context called Cat, it is in InforWeb / Deploy / CAT record. To access any servlet, visit http: // localhost: 8080 / CAT in your browser . Inforweb Under this configuration, use the DBCP connection pool, through the JNDI position: Java: Comp / Env / JDBC / Hibernate offers JDBCConnections with buffer pools. If you get the JDBC driver Exception information, don't use Hibernate, test whether the JDBC connection pool itself is correct. A simple JSP tested as follows: <% @ Page Import = "java.sql. *"%> <% @ Page Import = "javax.naming. *"%> <% @ Page Import = "javax.sql. *"%> <% CONTEXT INITCTX = New InitialContext (); Context envctx = (context) INITCTX.LOOKUP ("Java: Comp / ENV"); System.out.println ("BBB :: Get JNDI OK"); System.out.println ("BBB :: Get DataSource"); DataSource DataSource = (DataSource) Envctx.lookup ("/ JDBC / Hibernate"); System.out.println ("BBB :: Get DataSource OK"); System.out.println ("BBB :: Get Connection"); System.out.println ("BBB :: Get Connection"); Connection conn = DataSource.getConnection (); System.out.println ("BBB :: Get Connection OK" Conn; STATEMENT STM = Conn.createStatement (); System.out.println ("Get STM OK"); String SQL = "SELECT * from Courses"; ResultSet RS = Stm.executeQuery (SQL); While (rs.next ()) { Out.println (Rs.getstring (1)); Out.println ("/ n"); Out.println (rs.getstring (2)); } %> Step 4: Establish a hibernate.cfg.xml file under the application / web-inflight, which will bind the database connection pool provided in JNDI. The content is as follows: XML Version = '1.0' encoding = 'UTF-8'?>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> session-factory> Hibernate-Configuration> This file tells Hibernate how to get the data source provided by JNDI (Java: Comp / Env / JDBC / Hibernate), what database dialect (DIALECT), several mainstream database dialects are as follows: DB2: Net.sf.Hibernate.Dialect.db2DiaLect Oracle: Net.sf.Hibernate.DiaLect.OracledgeAlect MySQL: Net.sf.hibernate.Diaalect.mysqldiaLect Sybase: net.sf.hibasesqldiaalect I believe that everyone has discovered the law, if you use other databases, you will change it yourself (now Hibernate supports the database, if you use your own database ... ^ _ ^).