In fact, Hibernate itself is a separate framework that does not require any support for Web Server or Application Server. However, most of the Hibernate introductions have added a lot of non-Hibernate, such as Tomcat, Eclipse, Log4j, Struts, XDoclet, and even JBoss. This is easy to generate hibernate complex misunderstandings, especially the enthusiasm of the initiator. In this article will not involve Eclipse, Log4j, Struts, Tomcat, XDoclet, and JBoss. The purpose of this article is to demonstrate the Hibernate installation process and the most basic function, thus giving the beginners a low gate threshold. Download the file you require Java SDK, Hibernate Package, ANT Pack, and JDBC Driver. 1, Hibernate package download address: http://prdownload/hibernate/? Sort_by = DATE & SORT = desc2, ANT package download address: http://apache.130th.net/ant/binaries/apache-ant-1.6 .1-bin.zip3, JDBC Driver To set according to the Database you used, there will be on the Database official website. Hibernate supports common Database, such as MySQL, Oracle, PostgreSQL, and MS-SQL Server. These databases have JDBC Driver: Oracle JDBC Driver download address (must agree to Oracle Agreement before download) http://otn.oracle.com/software/htdocs/distlic.html?/software/tech/java/sqlj_jdbc/HTDOCS/ jdbc9201.htmlMySQL JDBC Driver Download http://dev.mysql.com/downloads/connector/j/3.0.htmlPostgreSQL JDBC Driver Download http://jdbc.postgresql.org/download.htmlMS-SQL Server JDBC Driver Download http://www.microsoft.com/downloads/details.aspx?familyid=9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71&displayLANG=EN4, extracting the Hibernate package and the ANT package to C: / dev / under (this directory is not important You can change any other directory). Configuring the environment 1, you need to add a new environment variable: Ant_home, let it point to the C: / dev /
So you have the following file structure: c: / workspace / My1stHibernate / c: / workspace / My1stHibernate / srcc: / workspace / My1stHibernate / classesc: / workspace / My1stHibernate / libc: / workspace / My1stHibernate / lib / hibernatec: / workspace / My1sthibernate / lib / db4, the directory of the C: / DEV /
Write TEST IMPORT NET.SF.HIBERNATE. *; Import Net.sf.hibernate.cfg. *; Public Class Test {PUBLIC VOID MAIN (String [] args) {Try {sessionFactory sf = new configuration (). Configure ( .BUILDSessionFactory (); session session = sf.opensession (); transaction tx = session.begintransaction (); for (int i = 0; i <200; i ) {Customer Customer = New Customer (); Customer.Setusername "Customer" i); Customer.SetPassword ("Customer"); session.save (Customer);} tx.commit (); session.close ();} catch (hibernateException E) {E.PrintStackTrace ();} }} Use this class as a C: /Workspace/my1sthibernate/src/test.java file. Create a hibernate mapping file because there is only one class - Customer and a table --- Customer, you only need to create a mapping file - Customer.hbm.xml, to correspond to the relationship between the Customer class and the Customer table. XML Version = "1.0"?>
Write an Ant Build.xml file You don't have to know the details of this build.xml, in fact, Ant is not necessary for Hibernate. Here are Ant to simplify some tasks, such as compilation, COPY, running, etc.
xml version = "1.0"?>
Configuring Hibernate Description File Hibernate Description files can be a Properties or XML file, where the most important is to define the connection of the database. I am listed here is an XML format hibernate.cfg.xml description file. XML Version = "1.0" Encoding = "UTF-8"?>