zoukankan      html  css  js  c++  java
  • 关于Hibernate中的Configuration

    Hibernate中,关于从 Configuration中建立一个SessionFactory常用的可以有两种方法,一种是为Configuration提供hibernate.cfg.xml配置文件,还有一种就是提供hibernate.properties配置文件。

    1.提供hibernate.cfg.xml配置文件:

     static {
      Configuration config=null;
      try {
       config = new Configuration().configure();//使用configure()方法加载默认的hibernate.cfg.xml配置文件,因为配置文件中已经含有数据库映射信息,因此就不需要使用addClass方法加载各个类文件的映射文件
      } catch (HibernateException e1) {
       e1.printStackTrace();
      }
      try {
       sessionFactory=config.buildSessionFactory();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

    2.提供hibernate.properties配置文件:

     static {
      Configuration config=null;
      config = new Configuration();//因为new一个Configuartion默认会加载hibernate.proprties文件,所以就不需要调用configure方法了
      try {
       config.addClass(Manager.class);//因为hibernate.properties文件中没有映射信息,所以需要调用addClass方法来显式的加载类文件所对应的映射文件
       sessionFactory=config.buildSessionFactory();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }

    如果方法使用错误,错误信息会多种多样,我在这个问题在困扰两个星期之后终于解决了!!!

  • 相关阅读:
    sql中生成随机字符串的function
    postgresql中uuid的使用
    sql中循环的存储过程
    java发送http的get、post请求
    data:image/png;base64
    Matcher和Pattern总结
    OPENXML解析sp_xml_preparedocument获取的XML句柄
    SqlServer性能优化
    python的2D绘图库matplotlib
    sift&surf检测关键点及描述子
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/4527116.html
Copyright © 2011-2022 走看看