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&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();