Transfer from: Demo - Use Hibernate 2.1beta6 Roberto Nie, Roberto_nie@163.com December 25, 2003 Summary This article describes the simplest Hibernate instance under WSAD 5.1. Environment OS: Windows XP SP1 Simplified Chinese version MEM: 512M Requirement: Hibernate 2.1 beta6 Hibernate eclipse plug in 0.9.6 WSAD 5.1 DB2 8.1 Steps: 1: build the demo database and tables: create the demo database in DB2 database, CREATE DATABASE DEMO; Create Table Demo, Create Table DB2Admin.Demo (ID Integer Not Null, Name Varchar (100)) 2 :: Create a new web application Click "File" -> "New" -> "Project", the converged conversation In the box, select "J2EE", and "Enterprise Applications", click Next, select "Create J2EE 1.3 Enterprise Appjectment", click Next, the project name fills in Demo, click Next, in the new pop-up dialog box Select "New Module", just select "Web Project" and click Finish. 3: Configure the database connection pool that comes with the WAS application server. Create a server: Open the server perspective, right click on "Server", select "New / Server and Server Configuration", the server name fills in "WAS5", then select the WebSphere V5.0 test environment, click Finish. Configure JNDI: Open the WAS5 server configuration in the server configuration window in the server perspective, select the "Security" tab, add the authentication entry in the JAAS authentication, click "Add", then fill in the alias (Demo), user ID (Db2admin), and password (123456), the description can not be filled. Then select the "Data Source" tab, select "DEFAULT DB2 JDBC Provider" in the JDBC Provider list, and then click "Add" in "Add" in "Data Source" in the program provided above the JDBC above. " Source "dialog, select" DB2 JDBC Provider "and" V5.0 Data Source "click" Next ". In this page, the name bar fills "demo", JDI name fills in "JDBC / DEMO", component management authentication alias, and container management authentication alias pull selection "demo", do not choose "Persistence (CMP) in container management (CMP) This authentication name, other use defaults. Select Next. In the "Resource Properties" list, select Database Name, and fill in "Demo"; select "PortNumber" and the value fills "50000" (this is the default port that accesses the DB2 instance). Click to complete. Finally save the set page. Open the "Web" perspective, edit /WebContent/web-inf/web.xml (Note Open mode is "Deploy Descriptor Editor", which is convenient to edit the source file). Click the "Resources" tab, then click "Add", the name of the reference resource is changed to "JDBC / DEMO", and the type is changed to "java.sql.datasource". Save the launch.
4. Loading Hibernate and Hibernate Eclipse Plugin class libraries can get from SourceForge.net, the above ZIP files, I use Hibernate2.1Beat6 and Eclipse Plugin 0.9.6. Different hibernate-2.1beta6.zip to the TMP directory, copy all the JAR files under Hibernate2.jar and TMP / Hibernate-2.1 / LIB to Workspace_Demo / demoweb / webcontent / web-inf. (Perhaps you don't need anything, but so much thing to save) extract Hibernator-0.6.zip to the Eclipse / PLUGIN directory under the WSAD installation directory. Note that if you want this plugin to support the latest version of Hibernate, you need to replace the JAR files in Hibernate / Lib and the latest version of Hibernate2.jar to replace WASD51 / Eclipse / PLUGINS / Hibernator_0.9.6 / liba, and hibernate2.jar files. . 5. Writing hibernate.cfg.xml file Hibernate can identify XML and Properies files, which is said that Hibernate will change to use XML, so I choose to use XML to configure Hibernate. Select the web perspective, in the javasource directory, create hibernate.cfg.xml. The creation method is to right click on "Javasource" -> "New" -> "Others" -> "XML", establish an empty file called hibernate.cfg.xml according to the wizard.
Then open hibernate.cfg.xml in the source code, copy it as follows: XML Version = "1.0" Encoding = "UTF-8"?>
The method is, right-click "Javasource", then select "New Build Pack" and "New Class" and create according to its wizard, and will not be described here. Now open the demo.java file, add two field: private int id, private string name; then right click on the source file, select Source -> "Generate Setter and getter", let the tool to generate the setter and getter methods . (When the field is more, this feature is very handsome). Then use the Hibernate plugin to help you generate a mapping file, first open "Window" -> "Show Trial" -> "Other", then you should be able to see Hibernate options (note, restart WSAD after the plugin is installed, the plugin decompressed The directory does not cause the plugin to not be displayed in this view). Select "Hibernate" -> "hibernate", after clicking OK, you will see the automatically generated demo.hbm.xml file, this automatically generated tool is said to have a poor manual, need more manual modify. Keep a part: XML Version = "1.0"?>