JBoss-Ide 1.2.2 Introduction (Hibernate) 1
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 a plus Module in xdoclet, so I remember to update XDoclet modules and data.3 in the XDoclet Configure of JBoss-IDE. With Eclipse (I am Use 2.1) to open the Tutorial's Project, right-click Tutorial Project, select 'Properties'.
Then select 'Java Build Path' -> 'Libraries' -> 'Add External Jars', select '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 ();
}
}
JBoss-Ide 1.2.2 Introduction (Hibernate) 2
JBoss-IDE 1.2.2 Introduction (Hibernate) 2: 5. After the completion, we will create a NUM Hibernate property profile to image Fibo Table in the database, right click Tutorial Project -> 'New' -> 'Class' , 'Package' fills in 'tutorial.hibernate', Name fills in 'Num', then press 'Finish'. The Hibernate tab in the xdoclet is used because this example is too simple, so it is not particularly This creates the template of the Hibernate tag. And Hibernate's ID generation will use UUID, the code is as follows:
Package tutorial.hibernate;
/ **
* @Author patrick.ip
*
* @ hibernate.class table = "fibo"
* /
Public class num {
Private string id;
Private Double value;
Public num () {
}
/ **
* @ hibernate.id generator-class = "uuid.hex"
* /
Public string getId () {
Return ID;
}
Public void setid (String ID) {
THIS.ID = ID;
}
/ **
* @ hibernate.property
* @Return Double
* /
Public Double getValue () {
Return Value;
}
Public void setValue (double value) {
THIS.VALUE = VALUE;
}
}
Apply these two classes will first add Hibernate's Import: Fibobean.java in Fibobean.java
Package tutorial.ejb;
Import java.rmi.remoteexception;
Import java.util.iterator;
Import javax.ejb.ejbexception;
Import javax.ejb.sessionbean;
Import javax.ejb.sessionContext;
Import javax.ejb.createException;
// Hibernate Imports
Import tutorial.hibernate. *;
Import net.sf.hibernate.query;
Import net.sf.hibernate.Session;
Import net.sf.hibernate.transaction;
Import net.sf.hibernate.hibernateException;