hibernate了解

hibernate所需JAR包(WEB-INF/lib)antlr-2.7.6.jar,cglib-nodep-2.1_3.jar,dom4j-1.6.1.jar,hibernate3.jar,jta.jar,ehcache-1.2.3.jar(有异常才加),junit.jar(单元测试才需要).配置文件(SRC目录下)hibernate.cfg.xml,hibernate映射文件(XXX.hbm.xml)

1.hibernate映射文件
映射文件名称同JAVA对象名称,扩展名为.hbm.xml--注意配置文件中的映射关系要成对出现,其DOCTYPE类型如下:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
增加<hibernate-mapping>标签,里面再增加<class>标签

 


2.hibernate配置文件---hibernate.cfg.xml,其DOCTYPE类型如下:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
增加<hibernate-configuration>标签,里面再增加<session-foactoy>标签

 

3.hibernate运行(Configuration实例时)需要dom4j-1.6.1.jar支持,configuration.configure()调用此方法时需要读取根目录下的hibernate.cfg.xml(所以此文件要放到src目录下).configuration.buildSessionFactory()调用此方法时需要cglib-nodep-2.1_3.jar支持(此包放到WEB-INF/lib下).

 

4.出现错误:java.lang.NoClassDefFoundError:net/sf/ehcache/CacheException,ehcache-1.2.3.jar此包没有加载(此包放到WEB-INF/lib下),要使用junit来测试,需要junit.jar包支持(此包放到WEB-INF/lib下).
5.出现错误:Not binding factory to JNDI, no JNDI name configured.读取配置文件修改如下:File f=new File("src/hibernate.cfg.xml");configuration.configure(f);--如果是提示信息则表示当前没有JNDI方式,而时JDBC
6.出现错误:java.lang.NoClassDefFoundError: javax/transaction/Synchronization.需要jta.jar包(WEB-INF/lib).
7.出现错误:NoClassDefFoundError: antlr/ANTLRException.需要antlr-2.7.6.jar包(WEB-INF/lib).

 

8.Digester使用:

Digester digester=new Digester(); digester.setValidating(false);
  digester.addObjectCreate("config", "com.blog.config.BlogConfig");
  digester.addSetProperties("config"); digester.addObjectCreate("config/dao", "com.blog.config.DaoConfig"); digester.addSetProperties("config/dao"); 
  digester.addSetNext("config/dao", "addDao", "com.blog.config.DaoConfig");
InputStream in=classLoader.getResourceAsStream("com/blog/config/BlogConfig.xml");
BlogConfig config=(BlogConfig) digester.parse(in);

 

9.hibernate事务管理:---在WEB.XML配置过滤器
通过Filter来实现,doFilter方法如下:SessionFactory sf=HibernateUitl.getSessionFactory();sf.getCurrentSession().beginTransaction();chain.doFilter(request, response);sf.getCurrentSession().getTransaction().rollback();

 

10.hibernate.cfg.xml内容如下:
 <session-factory> <!-- 对于sqlite来说,用户名和密码属性可以不设 -->
 <property name="connection.username"></property>
 <property name="connection.password"></property>
 <property name="hibernate.current_session_context_class">thread</property>
 <property name="hibernate.connection.url">jdbc:sqlite:F:/eclipse_workspace3.5/blog/Test.db</property>
 <!-- SQLiteDialect自定义类继承Dialect 不需要实现任何方法 -->
 <property name="hibernate.dialect">com.blog.hibernate.SQLiteDialect</property>
 <property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
 <mapping resource="com/blog/model/User.hbm.xml"/>
 <mapping resource="com/blog/model/Message.hbm.xml"/>
 <mapping resource="com/blog/model/ReplyMessage.hbm.xml"/> </session-factory>

 11.BlogConfig.xml配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<config> <dao id="userDao" type="com.blog.dao.impl.UserDao"/>
 <dao id="messageDao" type="com.blog.dao.impl.MessageDao"/></config>
2.BlogConfig类如下:
public static Hashtable<String,DaoConfig> beans;
 public BlogConfig(){beans=new Hashtable<String, DaoConfig>();}
 public void addDao(DaoConfig bean){beans.put(bean.getId(), bean);}
 public Object getDao(String name){ DaoConfig config=beans.get(name);
  if(config==null){throw new BlogException("Could't not Dao:"+name);}
  return config.getInstance();}
3.DaoConfig类如下:
private String id; private String type; private Object instance;
public Object getInstance() { if(null!=instance){ return instance; } 
  try { Class cls=Class.forName(getType()); instance=cls.newInstance(); return instance;
  } catch (Exception e) { throw new BlogException("Couldn't not class:"+getType());}}

 

12.  HibernateSessionFactory此类很重要(一般工具会产生),也可以自已写.--主要是通过configuration.configure()读取hibernate.cfg.xml文件得到/重建/关闭ssesionFactry等方法.可以设置得到Interceptor.部分内容如下:
private static final String INTERCEPTOR_CLASS="hibernate.uitl.interceptor.class";
configuration=new Configuration(); configuration.configure();
   String interceptorName=configuration.getProperty(INTERCEPTOR_CLASS);if(interceptorName!=null){Class intercptorClass=HibernateUitl.class.getClassLoader().loadClass(interceptorName);
    Interceptor interceptor=(Interceptor) intercptorClass.newInstance();    configuration.setInterceptor(interceptor);}    if(configuration.getProperty(Environment.SESSION_FACTORY_NAME)!=null){configuration.buildSessionFactory(); }else{ sessionFactory=configuration.buildSessionFactory(); }

public static SessionFactory getSessionFactory() {SessionFactory sf=null;
  String sfName=configuration.getProperty(Environment.SESSION_FACTORY_NAME);
  if(sfName!=null){log.debug("Looking up sessionFactory in JINI.");
   try { sf=(SessionFactory) new InitialContext().lookup(sfName); } catch (NamingException e) {throw new RuntimeException(e);
   } }else{ sf=sessionFactory; } 
  if(sf==null){throw new IllegalStateException("SessionFactory avaiable");} return sf;}

 

13.可以自已DaoFactory类,来得到持久化对象(Digester使用),内容如下:
public static Object getDao(String name){
  if(blogConfig==null){ return null;} 
  if(log.isInfoEnabled()){log.info("Getting the Dao:"+name);} 
  return blogConfig.getDao(name);}
 public static IUserDao getUserDao(){log.info("Getting the UserDao");
  return (IUserDao) blogConfig.getDao("userDao");}
 public static IMessageDao getMessageDao(){log.info("Getting the MessageDao");
  return (IMessageDao) blogConfig.getDao("messageDao");}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值