Hibernate 自举
原文: 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:
<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>
使用此文件可以使配置独立于源代码,并以结构化的方式概述配置。
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的类,如下所示:
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。
使用上面的示例,我们可以将配置提取到以下文件中:
hibernate.connection.driver_class=org.h2.Driverhibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUEhibernate.connection.username=sahibernate.connection.password=hibernate.connection.pool_size=1hibernate.dialect=org.hibernate.dialect.H2Dialecthibernate.show_sql=truehibernate.hbm2ddl.auto=create
现在,我们也必须改编HibernateUtil类以使用此新文件:
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 一起运行。