0

I'm trying to write a query in Hibernate to get all genres of a given game with a where clause. But I get this error when running my app :

Initial SessionFactory creation failed. org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

I understand that the hibernate.cfg.xml is needed for XML mapping in order to map POJO classes to their table counterparts, but I'm using annotations directly in my POJO classes to map them, not XML. It seems like using SessionFactory is the wrong way to do it in that case, this leads me to my question: what is the proper way of calling a HQL query when using annotations?

Thank you

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    String query = "FROM Genre WHERE idGame = " + id;
    
    List genres = (List) session.createQuery(query).list();
Johnny Bra
  • 101
  • 2
  • 12

2 Answers2

1

The hibernate configuration file is also used to define different JDBC connection settings hence it is required to create one.

You can create one like this

    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
        <session-factory>
    
            <!-- JDBC Database connection settings -->
            <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/[DATABASE_NAME]?useSSL=false&amp;serverTimezone=UTC</property>
            <property name="connection.username">[USERNAME]</property>
            <property name="connection.password">[PASSWORD]</property>
    
            <!-- JDBC connection pool settings ... using built-in test pool -->
            <property name="connection.pool_size">1</property>
    
            <!-- Select our SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <!-- Echo the SQL to stdout -->
            <property name="show_sql">true</property>
    
            <!-- Set the current session context -->
            <property name="current_session_context_class">thread</property>
     
        </session-factory>
    
    </hibernate-configuration>

You are also required to place it in the project's root directory (like just inside the src folder). Replace DATABASE_NAME, USERNAME, PASSWORD and please double check other properties defined.

Then you can build a SessionFactory like this

    SessionFactory factory = new Configuration()
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(Student.class)
                    .buildSessionFactory();

You can remove the "hibernate.cfg.xml" arg from configure if it is in the root directory(This is the default path the hibernate will look for the configuration file). Or you can manually change the path of cfg file, mentioning it in the arg.

And then get a Session from the SessionFactory

    Session session = factory.getCurrentSession();
arnabxm
  • 149
  • 7
  • I already have my database connection information in application.properties – Johnny Bra Jun 22 '20 at 16:07
  • Okay then. You just need to load the properties and create a SessionFactory like this SessionFactory factory = new Configuration().setProperties(properties).buildSessionFactory(); – arnabxm Jun 23 '20 at 06:09
0

Do I need to add hibernate.cfg.xml? Yes you need a cfg.xml for hibernate in order for it to know about its DB conf. May be you can refer this:

Location of hibernate.cfg.xml in project?