原创作者: lizwjiang
阅读:2917次
评论:0条
更新时间:2011-06-01
最近这一个阶段一直在研究hibernate,总感觉好象什么地方特别扭,研究一段时间,看了论坛上的各位大虾和小虾写的例子,自己也写了一个,在这里贴出来,希望得到大家的批评指正,特别是在架构上如何设计给些意见,因为对照关系如一对一,一对多的实现论坛的例子已经很清楚了.
开发环境:
struts 1.1
tomcat4.1.24
hibernate2.1.2
oracle 8.16
表﹕company
id 公司ID
company_name 公司名稱
配置就不說了﹐
po:company.java
company持久類對應的映射文件﹕
company.hbm.xml
配置文件hibernate.cfg.xml
提供session的类HebernateSessionFactory(利用Thread管理session)
封装CRUD操作的类(我觉的此类应该设置为abstract类)
在filter中关闭session,这个filter还可以用于字符集转换.
HibernateFilter.java
以上是hibernate相关部分.
开发环境:
struts 1.1
tomcat4.1.24
hibernate2.1.2
oracle 8.16
表﹕company
id 公司ID
company_name 公司名稱
配置就不說了﹐
po:company.java
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; } }
company持久類對應的映射文件﹕
company.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="com.foxconn.hibernate.po.Company" table="company" dynamic-update="false"> <id name="id" column="id" type="string" unsaved-value="any" > <generator class="assigned"> </generator> </id> <property name="companyname" type="string" update="true" insert="true" column="company_name"/> </class> </hibernate-mapping>
配置文件hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration D TD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.datasource">java:comp/env/jdbc/fox221</property> <property name="show_sql">false</property> <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property> <property name="jdbc.fetch_size">50</property> <property name="jdbc.batch_size">30</property> <!-- Mapping files --> <mapping resource="/hbm/Company.hbm.xml"/> </session-factory> </hibernate-configuration>
提供session的类HebernateSessionFactory(利用Thread管理session)
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 currentSession(); throws HibernateException{ Session s = (Session); session.get();; 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();; } }
封装CRUD操作的类(我觉的此类应该设置为abstract类)
public class HibernateUtil { public static void add( Object object );throws HibernateException{ Session s = HibernateSessionFactory.currentSession();; s.save( object );; s.flush();; s.beginTransaction();.commit();; } public static void update( Object object ); throws HibernateException { Session s = HibernateSessionFactory.currentSession();; s.beginTransaction();; s.saveOrUpdate( object );; s.flush();; } public static void remove(Class clazz, String id); throws HibernateException { Session s = HibernateSessionFactory.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 = HibernateSessionFactory.currentSession();; obj = s.load( clazz, id );; s.flush();; return obj; } }
在filter中关闭session,这个filter还可以用于字符集转换.
HibernateFilter.java
public class HibernateFilter implements Filter { private String encode = "big5"; public void init(FilterConfig config); { this.encode = config.getInitParameter("encode");; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); { try{ request.setCharacterEncoding(this.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(this.getClass(););.error("error request uri:"+((HttpServletRequest);request);.getRequestURI(););; Logger.getLogger(this.getClass(););.error(e);; } }finally{ try{ HibernateSessionFactory.closeSession();; }catch(Exception ex);{ System.out.println("Error in closing session");; } } } public void destroy(); { } }
以上是hibernate相关部分.
评论 共 0 条 请登录后发表评论