Database Connection via Hibernate

Connection to Database Using Hibernate

A Java application can connect to a MySQL database through Hibernate ORM by creating the environment, adding a database node, configuring the Hibernate files, generating entity mappings, and executing database operations through a Hibernate session.

Create the Environment

1

Create a Java environment

Sign in to the platform dashboard and create an environment with a Java application server.

2

Add a MySQL database node

Add a MySQL database server to the same environment.

Java environment with MySQL for Hibernate
Create a Java application environment with a MySQL database node.

Create the Database and Table

Create a database user with the values used in the source example:

Database namejelasticDb
Usernamejelastic
Passwordjelastic
!

Example credentials

These credentials are used only in the source tutorial. Replace them with strong, unique values in a real environment.

Create a table named books inside jelasticDb with these fields:

  • book_name
  • book_author

Configure hibernate.cfg.xml

Add the database dialect, driver, connection URL, credentials, session context, and mapping resource:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://mysql{node_id}.{your_env_name}.{hoster_domain}:3306/jelasticDb</property>
    <property name="hibernate.connection.username">jelastic</property>
    <property name="hibernate.connection.password">jelastic</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="com/Testdata.hbm.xml"/>
  </session-factory>
</hibernate-configuration>
i

Replace all placeholders

Replace {node_id}, {your_env_name}, and {hoster_domain} with the actual database node ID, environment name, and hosting-provider domain.

The connection URL format is:

jdbc:mysql://mysql{node_id}.{your_env_name}.{hoster_domain}:3306/jelasticDb
MySQL node ID in the dashboard
Find the MySQL container node ID in the dashboard.

Configure hibernate.revenge.xml

Define the database catalog and the table that Hibernate should process:

<hibernate-reverse-engineering>
  <schema-selection match-catalog="jelasticDb"/>
  <table-filter match-name="books"/>
</hibernate-reverse-engineering>

Generate Hibernate Mapping Files

Use Hibernate reverse engineering to generate these files in the web project:

  • Books.java
  • Books.hbm.xml

Create the standard HibernateUtil.java helper file as well. The source tutorial states that no changes are required in this utility file.

Execute a Database Operation

Create a Java method that opens the current Hibernate session, starts a transaction, saves a book record, and commits the transaction:

public void addBook(){
    Session s = HibernateUtil.getSessionFactory().getCurrentSession();
    s.beginTransaction();

    Books book = new Books(
        "romeo and juliet",
        "william shakespeare "
    );

    s.save(book);
    s.getTransaction().commit();
}

JDBC connector required

Add the database connector JAR library to the project or to the appropriate application-server folder in the environment.

Expected Result

The Hibernate configuration connects the Java application to jelasticDb, maps the books table to generated Java and XML mapping files, and inserts a new record through a Hibernate transaction.

Important Notes

  • Replace every connection-string placeholder with the actual environment values.
  • Use secure database credentials instead of the tutorial values.
  • Ensure that the JDBC connector JAR is available to the application server.
  • Keep the mapping resource path consistent with the generated Hibernate mapping file.
  • Confirm that the database name and table name match the reverse-engineering configuration.
  • Review application-server logs when Hibernate cannot create a session or connect to MySQL.

Common Issues and Solutions

Connection URL failsCheck the node ID, environment name, hosting-provider domain, port, database name, and database-node status.
Driver class is unavailableAdd the correct MySQL JDBC connector JAR to the project or application-server library path.
Mapping resource is not foundVerify the package and filename used in the <mapping resource> declaration.
Table is not generated by reverse engineeringCheck the catalog name and table-filter value in hibernate.revenge.xml.
Transaction failsReview the entity constructor, field mapping, database privileges, SQL constraints, and Hibernate logs.