When you use Struts to develop project development, what do you use to persistence your data? (OJB, CASTOR is also hibernate?) If not, I suggest you look at my article, Hibernate is ORM that developers generally respected, and it comes extremely rich (why didn't I choose OJB, I am afraid this is Main reasons.) Struts launched its 1.1 official version in the first half of this year, marking that it has a more step in ripe and stabilization. This article is based on this release. I originally want to use good features in Struts1.1 to do a better example. Due to time, such as: Tiles, Exception Hadling, Validator, Multi-Module, Internationalization, etc. There is no involvement in the example. Related content, please see my column articles in 9cs. (1) Preparation: a. Struts1.1Http: //jakarta.apache.org/strutsb. Hibernate 2.0http: //hibernate.bluemars.net/c. Eclipse2.1 (Oh, I prefer an IDE, this is optional) http://www.eclipse.org/d. Ant1.5.1http: //ant.apache.org/e. JDK1.4f. Tomcat4.1
(The relevant website can get its latest version) (2) An example of a simple example, the relationship is not complex: animal.java and cat (Cat.java). The former is a pair of relationships on the latter, and the latter is multi-to-one relationship. The first part, and Hibernate related 1. Animal class
Package com.iplateau.test.hibernate.Persistence; import java.util.set; / ** * Class or interface discrtiption * @Author $ author: jack $ * @version $ revision: 1.0 $ ID: Animal.java 2003-8-4 16:44:02 Jack Exp. * / Public Class Animal {Private String ID; Private String Name; Private SET CAT;
Public animal () {}
/ ** * @Return * / public set getcats () {return cats;} / ** * @return * / public string getId () {return ID;} / ** * @return * / public string getname () { Return name;} / ** * @Param set * / public void set;} / ** * @Param string * / public void setid (String string) {ID = string;} / * * @Param string * / public void setname (string string) {name = string;}}
CAT class:
Package com.iplateau.test.hibernate.Persistence; / ** * Class or Interface discrtion * @Author $ author: jack $ * @version $ revision: 1.0 $ * $ ID: Cat.java 2003-8-8-8-8 3 14:33:11 Jack Exp. * / Public class cat {private string id; private string name; private string sex; private float weight; // select name form cat as cat where cat.name = private animal animal
Public cat () {} / ** * @Return * / public string getId () {return ID;} / ** * @return * / public string getname () {return name;} / ** * @return * / Public string getsex () {return sex;} / ** * @Return * / public float getWeight () {Return Weight;} / ** * @Param string * / public void setid (string string) {id = string;} / ** * @Param string * / public void setName (String string) {name = string;} / ** * @Param c * / public void setsex (string c) {sex = C;} / ** * @param F * / public void setWeight (float f) {weight = f;} / ** * @return * / public animal getanimal () {return animal;} / ** * @Param animal * / public void setanimal (animal animal) {This.animal = animal;}} These two classes can be found in the code package behind this article, the reason why I am here, I want you to know that the sustainable class is often familiar with you. There is no difference between Java class objects: attribute, get, set method. "Transparent" is "transparent" here.
Hibernate is equipped with a maping file for each sustainable class, they can generate through Hibernate or other tools (the same: there is a mapping file, then generate Java code), when you start learning It is best to be familiar with handwritten.
Animal.hbm.xml
XML Version = "1.0"?>
XML Version = "1.0"?>
Ok, usually after the mapping file is completed, you can use the Hibernate's own tool to generate a table statement, then use Ant to help you complete. {See BUILD.XML} in the attached code package, the following is the table clip: .........
Let's talk about the creation method of SessionFactory, about three methods (see Hibernate manual): via * .class; pass * .hbm.xml; pass hibernate.cfg.xml; I prefer to use the last method, This example is hibernate.cfg.xml to create sessionFactory and database connections. Hibernate.cfg.xml
xml version = '1.0' encoding = 'UTF-8'?>
Hibernate-Configuration>
OK, it is possible to apply after the creation method, it should be only created once for SessionFactory, so it is recommended to build in system initialization, then use Struts's PLUGIN mechanism to complete this task, see:
Com.Iplateau.test.hibernate.InithibernatedTaStore.java (Hibernate manual also recommended to create this method in Struts) About Struts Extended mechanism See I have another article "Expand your struts"
Package com.iplateau.test.hibernate;
Import java.io.file; import java.util.list; import java.Net.URL; import javax.naming.context; import javax.naming.initialcontext; import javax.naming.namingexception;
Import net.sf.hibrnate.SessionFactory; import net.sf.hibrnate.cfg.configuration;
Import javax.servlet.servletException;
Import org.apache.struts.action.actionServlet; import org.apache.struts.action.plugin; import org.apache.struts.config.Moduleconfig;
/ ** * Class or Interface discrtion * @Author $ author: Jack $ * @version $ revision: 1.0 $ * $ ID: IITHIBERNATETASTORE.JAVA 2003-8-3 14:08:50 Jack Exp. * / Public class inithibernateDataStore Implements plugin {
private String _configFilePath = "/hibernate.cfg.xml"; private SessionFactory _factory = null; static String contextFactory = "com.sun.jndi.rmi.registry.RegistryContextFactory"; private Context ctx = null; / * (non-Javadoc) * @see org.apache.struts.action.plugin # destroy () * / public void destroy () {// Todo auto-generated method stub try {if (ctx! = null) {ctx.close (); CTX = NULL;}} catch (namingexception ex) {ex.printstacktrace ();}}
/ * (non-javadoc) * @see org.apache.struts.action.plugin # init (org.apache.struts.Action.ActionServlet, org.apache.struts.config.moduleconfig) * / public void init (ActionServlet Arg0 , ModuleConfig arg1) throws ServletException {// TODO Auto-generated method stub Configuration configuration = null; URL configFileURL = null; System.out.println ( "4385738758237482173841847382374823742"); try {configFileURL = InitHibernateDataStore.class.getResource (_configFilePath); configuration = (new Configuration ()) configure (configFileURL);. _factory = configuration.buildSessionFactory (); Context ctx = new InitialContext (); ctx.addToEnvironment (javax.naming.Context.INITIAL_CONTEXT_FACTORY, contextFactory);
CTX.bind ("Hibernate_Connection_Factory", _Factory;
} catch (exception e) {
}}}
After creating sessionFactory, use JNDI to get: sessionFactory sf = (sessionFactory) INTTEX.LOOKUP ("Hibernate_connection_Factory"); {See com.iplateau.test.hiba }.dbManager.java}
The database of the database, I use Filter to solve, the effect is not bad, please refer to Web.xml and com.iplateau.test.hibernate.setencodingfilter.java
Summary: Overall, combined with Struts and Hibernate in the industry's more advanced open source technologies, in project development, whether it is a perfect combination of efficiency or easy maintenance. I hope to give you more prompts from this simple example, and I hope that you will make a good suggestion, contact me via Plateau_t@sina.com. Attached code package: http://www.ifreeway.com.cn/download/strutsinhibhongnate.rar (valid for two weeks) Reference Resources: Hibernate Reference Manual (Bibernate Release Bag) Hibernate Example Http : //SourceForge.Net/Project/ShowFiles.php? Group_id = 40712 Introduction to Hibernate http://www.theserverside.com/Resources/Article.jsp?l=hibernate
At the same time http://www.chinaxp.org and http://www.jdon.com all have many discussions about Hibernate.