JBoss-IDE 1.2.2 Introduction (Hibernate) 1:
First require:
l To complete the JBoss-IDE 1.2.2 tutorial (http://www.9cbs.net/develop/read_article.asp?id=25298), and the example in successful operation, Fibobean and ComputeServlet
l There must be a certain understanding of JBoss, MySQL and Hibernate and download Hibernate 2.1.2 (http://prdownloads.SourceForge.Net/Hibernate/Hibernate-2.1.2.zip?download) and XDoclet (http: //prdownloads.sourceforge .NET / XDOCLET / XDOCLET-BIN 1.2.Zip? Download
Description:
l I will continue to use the example in the JBoss-IDE tutorial, which is a calculation of the calculation of the Fibonacci in Index.html Call ComputeServlet (Fibonacci, an integer number, wherein each number is equal to the front two sum) FUNCTION (Compute). After applying to Hibernate, I will find out the database (Table - FIBO), such as and input requirements, if I find out the database (Mysql - Test DB) in the Compute Function. In the same way, it is not necessary to calculate the information in the database as a result. If the difference is clear, the data in the database is cleared, recalculate and store the database.
step:
1. I will create a table with MySQL to store the results of the calculation of the Fibonacci number. First of all, I will create a database named 'Test', and create a table name 'fibo', and 'fibo' is only Two fields, one is ID (varchar, pk), one is Value (double). Finally, set this mysql - test DataSource in the DEFAULT Server of JBoss.
2. Download Hibernate 2.1.2, please refer to how to set Hibernate in JBoss in JBoss in JBoss (http://www.hibernate.org/66.html - the author Press: This link seems to be dead, no Dare to affirm, maybe Hibernate has put in jboss, and Hibernate replaces CMP in version 4.0, so this introduction is not too useful, only a reference.) And (http://nemesisit.rdsnet.ro/opendocs/ EjbHibernate.html). Copy the 'cglib-2.0-rc2.jar' in the Hibernate2.jar and Lib data clips in the Hibernate root directory, 'commons-collections-2.1.jar', 'commons-lang-1.0.1.jar ',' Commons-Logging-1.0.3.jar ',' DOM4J-1.4.jar ',' JCS-1.0-Dev.jar ',' odmg-3.0.jar ', to JBoss Default Server LIB (% JBoss_Home / Server / default / lib) Data clip. After completion, go to the xdoclet, download it, copy it, copy the 'xdoclet-hibernate-module-1.2b4.jar' to the Eclipse folder in its lib data clip / in eclipse folder / plugins / Org .jboss.ide.eclipse.xdoclet.core_1.2.2 / Data clip, because this is an additional module in the xdoclet, so I remember to update XDoclet modules and Data in the XDoclet Configure of JBoss-IDE.
3. Use Eclipse (I use 2.1) to open the tutorial's Project, right-click Tutorial Project, select 'Properties'. Then select 'Java Build Path' -> 'Libraries' -> 'Add External Jars', Choose 'lib' (% jboss_home / server / default / lib /) in JBoss Default Folder, select 'Hibernate2.jar', press 'Open' and press 'OK'.
4. Now create a hibernateutil class. First click on Tutorial Project -> 'New' -> 'Class', 'Package' Fill in 'Tutorial.Hibernate', Name Fill in 'HibernateUtil', and then Press 'Finish', and the code is as follows:
Package tutorial.hibernate;
Import javax.naming.context;
Import javax.naming.initialcontext;
Import net.sf.hibernate.Session;
Import Net.sf.hibActory;
Import net.sf.hibernate.hibernateException;
Public class hibernateutil {
PRIVATE STATIC sessionFactory sessionFactory;
STATIC {
Try {
Context ctx = new initialContext ();
SessionFactory = (sessionFactory) CTX.lookup ("java: / hibernate / hibernateFactory);
}
Catch (Exception E) {
E.PrintStackTrace ();
}
}
PUBLIC Static Final Threadlocal session = new threadlocal ();
Public static session currentsession () throws hibernateException {
Session s = (session) session.get ();
// Open a new session, if this thread HAS NONE YET
IF (s == NULL) {
S = sessionFactory.openSession ();
Session.set (s);
}
Return S;
}
Public static void closesession () THROWS HibernateException {
Session s = (session) session.get ();
session.set (null);
IF (s! = null)
s.close ();
}
}