hibernate.propertiesファイルとhibernate.cfg.xmlファイルの違い


多くの友達は両者の違いを知らないので、ここで詳しく話しましょう.hibernateを使うならpropertiesがプロファイルとして使用すると、プロファイルの内容は大体次のようになります.
--------------------------------------------------------------hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.connection.driver_class=com.mysql.jdbc.Driverhibernate.connection.url=jdbc:mysql://localhost:3306/workhibernate.connection.username=roothibernate.connection.password=1234hibernate.show_sql=true
==============================================================cfg.xmlでは、orgのような内容になります.gjt.mm.mysql.Driver                org.hibernate.dialect.MySQLDialect                jdbc:mysql://localhost:3306/work                root                1234                                皆さんは何か違うところを見つけましたか?そうだなproperties構成ではマッピングされたクラスは構成されていません.hibernateはhibernateにはいられないpropertiesではマッピングが必要なクラスを知っていますが、どこで知ることができますか?答えはソースファイルにあります.hibernateを使うとpropertiesがプロファイルとして使用される場合、ソースファイルにはconfigを構築する必要があります.
この方法は静的な方法で修飾するか,構造関数に置く.
    
static{
     try{
      // Create a configuration based on the properties file we've put
       Configuration config = new Configuration();
       config.addClass(Company.class)
             .addClass(HourlyEmployee.class)
             .addClass(SalariedEmployee.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}

  }

ここのconfigではaddClass()メソッドを呼び出さなければなりません.これがhibernateにマッピングクラスを知らせる場所です.もちろんhibernateを使います.cfg.xmlで構成すると、コンフィギュレーションconfig=newコンフィギュレーション()と書くことができます.              SessionFactory sf = config.configure("/hibernate.cfg.xml").buildSessionFactory();//ここではconfigure()メソッドを呼び出してhibernateを指定します.cfg.xmlの位置Session session=sf.openSession(); マッピングが必要なクラスを再構成する必要はありません.再構成すると、重複マッピング異常が発生します.つまり、hibernate.cfg.xmlにマッピング関係が説明されていなければ、ソースファイルでaddClass()メソッドを使えばいいのですが、繰り返すことはできません!
       hibernate.propertiesファイルはデフォルトで読み込まれます.また、hibernateの起動中にhibernateを探します.properties、hibernateを読みます.cfg.xml、後者は前者と同じ属性を上書きします