【问题标题】:Hibernate Java Config Versus XmlHibernate Java 配置与 Xml
【发布时间】:2017-12-09 02:19:07
【问题描述】:

我是 hibernate 新手,我不完全了解如何在 java 配置和 hibernate 配置文件中的 xml 之间进行选择?我看过的教程似乎已经过时了。所以我的问题是将模型配置为休眠的最佳和最新方法是什么。

以下是我目前的方法,似乎是唯一适合我的方法:

<hibernate-configuration>
<session-factory>


<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</property>

<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>

<!-- <property name="hibernate.generate_statistics">true</property> -->

我正在一个 java 文件中配置我的模型,如下所示:

public class HibernateUtil {

private static SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {

        // Create your SessionFactory with mappings for every Entity in a specific package
        Configuration configuration = new Configuration();
        configuration.configure();
        configuration.addAnnotatedClass(PersonModel.class);

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        //SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

        return sessionFactory;

    } catch (Throwable ex) {
        // Make sure you log the exception, as it might be swallowed
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

这是正确的做法吗?还是应该通过 XML 文件完成,如果可以的话,有人可以给我一个例子吗?

提前致谢

【问题讨论】:

  • 如果你也使用spring,你可以将dataSource打包到你的application.xml中并用jpa映射你的类
  • 现在我想自己使用 Hibernate 更容易学习

标签: java hibernate


【解决方案1】:

查看以下文档以获取示例:

注解映射:

https://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping

对于 xml 映射:

https://docs.jboss.org/hibernate/orm/3.3/reference/fr-FR/html/xml.html

使用其中一个由您决定,有关更多详细信息,您可以查看此answer 或此one

编辑:休眠配置文件中的映射示例

<hibernate-configuration>
  <session-factory>
    <mapping package="test.animals"/>
    <mapping class="test.Flight"/>
    <mapping class="test.Sky"/>
    <mapping class="test.Person"/>
    <mapping class="test.animals.Dog"/>
    <mapping resource="test/animals/orm.xml"/>
  </session-factory>
</hibernate-configuration>

【讨论】:

  • 我在我的模型类中使用注解,但我仍然需要使用以下代码:Configuration configuration = new Configuration();配置.configure(); configuration.addAnnotatedClass(PersonModel.class);是否有替代方案或者这是正确的方法?提前致谢
  • @Kris22 是的,这种方式是正确的,但您也可以将映射添加到带有映射标签 ex 的休眠配置文件中:&lt;mapping class="yourpackage.PersonModel"/&gt;,因此您的 java 中不需要 configuration.addAnnotatedClass(PersonModel.class);
  • @Kris22 我已经用 xml 配置示例更新了我的答案
  • 谢谢,这很有用
  • @Kris22 在 Java 配置优于 xml 的优势中,我可以引用 Java 配置,您可以从类型安全中受益,这可以节省您的时间,重构和搜索也会更容易。
【解决方案2】:

这样你可以一次扫描所有实体类

SessionFactory sessionFactory = new LocalSessionFactoryBuilder(yourDataSource())
        .scanPackages("com.yourpackage.name")
        .addProperties(properties)
        .buildSessionFactory();

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2013-04-22
    • 2011-11-01
    • 2016-06-17
    • 2012-02-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多