0

I am new to hibernate and I don't fully understand how to choose between java configuration and xml in hibernate config file? Tutorials that I have watched seem to be out of date. So my question is what is the best and most up to date way of configuring a model to hibernate.

The following is my current approach which seems to be the only one working for me:

<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> -->

I am configuring my model in a java file as shown below:

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);
    }
}

Is this the right way of doing it? or should it be done via XML file, if so can someone show me an example please?

Thanks in advance

Abass A
  • 713
  • 6
  • 14
Kris22
  • 15
  • 1
  • 5

2 Answers2

0

Look at the following docs for examples:

For annotation mapping:

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

For xml mapping:

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

Use of one of them it's up to you, for more details you can check this answer or this one

EDIT: mapping example in hibernate configuration file

<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>
Abass A
  • 713
  • 6
  • 14
  • I am using annotations in my model class, but I still need to use the following code: Configuration configuration = new Configuration(); configuration.configure(); configuration.addAnnotatedClass(PersonModel.class); Is there an alternative or is this the right way to do it? Thanks in advance – Kris22 Jul 05 '17 at 14:34
  • @Kris22 Yes this way is correct but you can alternatively add your mapping into hibernate configuration file with mapping tag ex : `` and thus you dont need `configuration.addAnnotatedClass(PersonModel.class);` in your java – Abass A Jul 05 '17 at 14:54
  • @Kris22 i've updated my answer with an example of xml config – Abass A Jul 05 '17 at 15:00
  • Thank you this is very useful – Kris22 Jul 05 '17 at 15:05
  • @Kris22 Among advantages of Java config over xml i can quote that with java config you can benefit from type safety and this can save you time , also refactoring and search will be more easier. – Abass A Jul 05 '17 at 15:08
  • @Kris22 Also i realize that your question was more about hibernate java config versus xml config and not annotation versus xml mapping, if so feel free to approve my edit – Abass A Jul 05 '17 at 15:15
0

This way you can scan all entity classes at once

SessionFactory sessionFactory = new LocalSessionFactoryBuilder(yourDataSource())
        .scanPackages("com.yourpackage.name")
        .addProperties(properties)
        .buildSessionFactory();
Anurag
  • 101
  • 1
  • 5