<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Hibernate 自舉 > 原文: [https://javabeginnerstutorial.com/hibernate/hibernate-bootstrapping/](https://javabeginnerstutorial.com/hibernate/hibernate-bootstrapping/) 在本文中,我將向您介紹 Hibernate 5 的新本機自舉 API。 ## 自舉有什么好處? 如果您覺得需要對 Hibernate 的內部配置進行更多控制,則可以利用此新功能來實現此目標。 如果您有一個僅需要使用 Hibernate 而不需要其他 JPA 框架的簡單項目,這將非常有用:借助自舉 API,您可以在沒有太多魔術的情況下啟動和運行項目。 自然,每一朵玫瑰都有其刺:新的本機自舉 API 的使用使配置更加復雜,但它比 JPA 自舉 API 更強大。 ### 為什么這比以前更好? 引入的新功能使您可以通過 Java 代碼訪問 API。 這意味著您不必依賴單一的 XML 配置,您可以在代碼庫中添加一些配置,只有當您提供新版本的軟件時,該配置才能更改。 在某些情況下,這非常有用,因為您的應用不必依賴通過配置文件配置的屬性的正確性,或者您可以在 Hibernate 中調整一些您不想通過外部文件更改的內部配置。 。 ## 如何使用 API??? 首先,您需要項目中正確的依賴項。 至于本系列文章中的所有示例,我正在使用 Hibernate 版本 5.3.6。 最終版本和 H2 版本 1.4.197。 我們需要創建一個`StandardServiceRegistry`,一個元數據對象,并使用它來啟動`SessionFactory`。 ### `hibernate.cfg.xml` 大多數時候,我使用基于 XML 的配置文件`hibernate.cfg.xml`來設置`StandardServiceRegistry`: ```java <hibernate-configuration> ??? <session-factory> ??????? <!-- Database connection settings --> ??????? <property name="connection.driver_class">org.h2.Driver</property> ??????? <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property> ??????? <property name="connection.username">sa</property> ??????? <property name="connection.password"/> ??????? <!-- JDBC connection pool (use the built-in) --> ??????? <property name="connection.pool_size">1</property> ??????? <!-- SQL dialect --> ??????? <property name="dialect">org.hibernate.dialect.H2Dialect</property> ??????? <!-- Echo all executed SQL to stdout --> ??????? <property name="show_sql">true</property> ??????? <!-- Drop and re-create the database schema on startup --> ??????? <property name="hbm2ddl.auto">create</property> ??????? <mapping class="hibernate_example.Book"/> ??? </session-factory> </hibernate-configuration> ``` 使用此文件可以使配置獨立于源代碼,并以結構化的方式概述配置。 ```java final Configuration configuration = new Configuration().configure(); final StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().configure(“hibernate.cfg.xml”); ``` 如示例代碼所示,您需要告訴 Hibernate 您正在使用`hibernate.cfg.xml`文件。 在以前的 Hibernate 版本中,您不需要指定使用此文件來設置服務注冊表,因為這是默認行為。 ### 程序配置 使用 Hibernate 5,您可以選擇以 Java 代碼編程配置 ORM。 讓我們看看如何將先前顯示的`hibernate.cfg.xml`文件映射到 Java 代碼中。 為此,我創建了一個名為`HibernateUtil`的類,如下所示: ```java package hibernate_example; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; /** ?* Utility class for bootstrapping Hibernate through Java code only. ?* ?* @author GHajba ?*/ public class HibernateUtil { ??? protected HibernateUtil() { ??? } ??? public static SessionFactory createSessionFactory() { ??????? MetadataSources metadataSources = new MetadataSources(configureServiceRegistry()); ??????? addClasses(metadataSources); ??????? return metadataSources.buildMetadata() ??????????????? .getSessionFactoryBuilder() ??????????????? .build(); ??? } ??? private static ServiceRegistry configureServiceRegistry() { ??????? return new StandardServiceRegistryBuilder() ??????????????? .applySettings(getProperties()) ??????????????? .build(); ??? } ??? private static Properties getProperties() { ??????? Properties properties = new Properties(); ??????? properties.put("hibernate.connection.driver_class", "org.h2.Driver"); ??????? properties.put("hibernate.connection.url", "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"); ??????? properties.put("hibernate.connection.username", "sa"); ??????? properties.put("hibernate.connection.password", ""); ??????? properties.put("hibernate.connection.pool_size", 1); ??????? properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); ??????? properties.put("hibernate.show_sql", "true"); ??????? properties.put("hibernate.hbm2ddl.auto", "create"); ??????? properties.put("", ""); ??????? return properties; ??? } ??? private static void addClasses(MetadataSources metadataSources) { ??????? metadataSources.addAnnotatedClass(Book.class); ??? } } ``` 如您所見,所有內容現在都在 Java 代碼中。 這使得僅處理 Java 類變得很方便-但是想想如果您有很多實體,您會得到什么? 我認為,它將像下面的屏幕截圖那樣混亂您的代碼庫: 因此,我更喜歡將配置保存在單獨的文件中,而不是在 Java 代碼中-但這是我個人的看法。 ### `hibernate.properties` 讓代碼喘氣的一種方法是將配置提取到屬性文件中-在大多數情況下,該文件稱為`hibernate.properties`。 使用上面的示例,我們可以將配置提取到以下文件中: ```java hibernate.connection.driver_class=org.h2.Driver hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE hibernate.connection.username=sa hibernate.connection.password= hibernate.connection.pool_size=1 hibernate.dialect=org.hibernate.dialect.H2Dialect hibernate.show_sql=true hibernate.hbm2ddl.auto=create ``` 現在,我們也必須改編`HibernateUtil`類以使用此新文件: ```java package hibernate_example; import java.io.IOException; import java.util.Properties; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.service.ServiceRegistry; /** ?* Utility class for bootstrapping Hibernate through Java code and hibernate.properties file. ?* ?* @author GHajba ?*/ public class HibernateUtil { ??? protected HibernateUtil() { ??? } ??? public static SessionFactory createSessionFactory() { ??????? MetadataSources metadataSources = new MetadataSources(configureServiceRegistry()); ??????? addClasses(metadataSources); ??????? return metadataSources.buildMetadata() ??????????????? .getSessionFactoryBuilder() ??????????????? .build(); ??? } ??? private static ServiceRegistry configureServiceRegistry() { ??????? return new StandardServiceRegistryBuilder() ??????????????? .applySettings(getProperties()) ??????????????? .build(); ??? } ??? private static Properties getProperties() { ??????? Properties properties = new Properties(); ??????? try { ??????????? properties.load(HibernateUtil.class ??????????????????? .getResourceAsStream("/hibernate.properties")); ??????? } catch (IOException e) { ??????????? throw new RuntimeException(e); ??????? } ??????? return properties; ??? } ??? private static void addClasses(MetadataSources metadataSources) { ??????? metadataSources.addAnnotatedClass(Book.class); ??? } } ``` ## 總結 使用自舉 API,您可以配置 Hibernate 應用-這使您的項目更加復雜,但是您可以將這些功能隱藏在服務類中。 如果只想在簡單項目中使用 Hibernate,這將很方便。 如果您仔細閱讀本系列文章隨附的示例應用,您會發現我自己使用此 API 來使應用與 Hibernate 一起運行。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看