Recently, this stage has been studying Hibernate, always feels like a special twist, study for a while, read the example of all prawns and shrimps on the forum, I also wrote one, put it here, hope to get Everyone's criticism, especially how to design on the architecture give some opinions, because the control relationship is like one, a couple of realizes the example of the forum is very clear. Development environment: struts 1.1 Tomcat4.1.24 hibernate2.1.2 Oracle 8.16 table : Company ID Company ID Company_name does not say, PO: Company.java
Java code:
public
Class Company
{
Private
String ID;
Private
String CompanyName;
public
String getID
(
)
{
Return ID;
}
public
String getcompanyname
(
)
{
Return CompanyName;
}
public
Void setID
(
String ID
)
{
THIS.
ID = ID;
}
public
Void setcompanyname
(
String CompanyName
)
{
THIS.
CompanyName = CompanyName;
}
}
Map file corresponding to Company's persistent class: company.hbm.xml
Java code:
XML Version = "
1.
0 "?>
Public
"
// hibernate / hibernate mapping DTD 2.0 // en "
"http:
//hibernate.sourceForge.Net/Hibernate-mapping-2.0.dtd ">
<
Class Name = "COM.
FoxConn.
Hibernate.
Po.
Company "Table =" Company "Dynamic-Update ="
False>
Class = "assigned"> generator> id> Class> hibernate-maping> Profile hibernate.cfg.xml Java code:
PUBLIC "- // Hibernate / Hibernate Configuration D TD // en "" http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd "> False property> sf. Hibernate. Diagect. OracleDiaAlakect Property> 50 Property> 30 Property> session-factory> Hibernate-Configuration> Provide Session, HEBERNATESSESSIONFAACTORY (using Thread Management Session) Java code: public Class HibernateSessionFactory { public Static Final Threadlocal session = New Threadlocal ( ); Private STATIC sessionFactory sessionFactory; Static { Try { SessionFactory = New Configuration ( ). CONFIGURE ( ). BuildSessionFactory ( ); } Catch (HibernateException EX ) { Throw New RuntimeException ( " Exception building sessionFactory: " EX. GetMessage ( ), EX ); } } public Static session currentsssion ( ) Throws HibernateException { Session s = (Session Sense. get ( ); IF (s == NULL ) { S = sessionFactory. OpenSession ( ); SESSION. set (s) ); } Return S; } public Static Void CloseSession ( ) Throws HibernateException { Session s = (Session Sense. get ( ); SESSION. set ( NULL ); IF (s! = NULL s. Close ( ); } } Class package CRUD operation (I do this) should be set to Abstract class) Java code: public Class HibernateUtil { public Static Void Add ( Object Object ) Throws HibernateException {Session s = hibernatesisfactory. CurrentSession ( ); S. Save (Object) ); S. Flush ( ); S. BeGintransaction ( ). Commit ( ); } public Static Void Update ( Object Object ) Throws HibernateException { Session s = HibernatessionFactory. CurrentSession ( ); S. BeGintransaction ( ); S. Saveorupdate (Object) ); S. Flush ( ); } public Static Void Remove ( Class Clazz, String ID ) Throws HibernateException { Session s = HibernatessionFactory. CurrentSession ( ); S. BeGintransaction ( ); Object object = s. Load (Clazz, ID ); S. Delete (Object) ); S. Flush ( ); } public Static Object Findbyid ( Class Clazz, String ID ) Throws HibernateException { Object obj = NULL; Session s = HibernatessionFactory. CurrentSession ( ); Obj = s. Load (Clazz, ID ); S. Flush ( ); Return Obj; } } Close the session in the FILTER, which can also be used for character set conversion. HibernateFilter.java Java code: public Class HibernateFilter IMPLEMENTS FILTER { Private String encode = "BIG5"; public Voidin (FilterConfig Config) ) { THIS. ENCODE = Config. GetinitParameter ("encode" ); } public Void DOFILTER (ServletRequest Request, ServletResponse Response, Filterchain Chain ) { Try { REQUEST. SetcharacterenceeCoding . ENCODE ); RESPONSE. SetContentType ("Text / Html; Charset =" Encode ); CHAIN. Dofilter (Request, Response ); } Catch ( Exception E ) { IF (! (e instanceof java. Net. SocketException ) ) { IF (Request InstanceOf HttpServletRequest ) Logger. GetLogger . GetClass ( ) ). Error ("Error Request Uri:" ( (HttpservletRequest Request ). GetRequesturi ( ) ); Logger. GetLogger . GetClass ) ). Error (e ); } } Finally { Try { HibernateSessionFactory. CloseSession ( ); } Catch ( Exception EX ) { SYSTEM. OUT. PRINTLN (" Error In Closing Session ); } } } public Void Destroy ( ) { } } The above is the Hibernate related part. COMPANYDAO.JAVA is an interface. Java code: public Interface CompanyDao { public Void Addcompany (Company Company ) Throws HibernateException; } Factory HibernatedaOfactory.java Java code: public Class Hibernatedaofactory Implements Daofactory { public Static Object GetInstance ( String subclassname ) Throws ClassNotFoundException, InstantiationException, ILLEGALACCESSEXCEPTION { Return ( Class. Forname (SubclassName ). NewInstance ( ) ); } } DAO implementation class companiesDaoImpl Java code: public Class CompanyDaoImpl Implements CompanyDao { public Void Addcompany (Company Company ) Throws HibernateException { HibernateUtil. Add (Company ); } } Struts's Action class is only responsible for the transfer of COMPANYACTION.JAVA Java code: public Class companiesControl Extends dispatchaction { Public ActionForward SaveCompany (ActionMapping Mapping, Actionform Form, HttpservletRequest Request, HTTPSERVLETRESPONSE RESPONSE ) Throws EXCEPTION { HttpSession session = request. GetSession ( ); String ID = Request. GetParameter ("ID" ); String name = request. GetParameter ("Name" ); Company companies = New Company ( ); COMPANY. setID (ID) ); COMPANY. Setcompanyname (Name) ); CompanyDao CompanyDao = (CompanyDao) Hibernatedaofactory. GetInstance ("CompanyDaoImpl" ); CompanyDAO. Addcompany (Company ); Return mapping. FINDFORWARD ("Success" ); } }