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
Create a Java environment
Sign in to the platform dashboard and create an environment with a Java application server.
Add a MySQL database node
Add a MySQL database server to the same environment.

Create the Database and Table
Create a database user with the values used in the source example:
jelasticDbjelasticjelasticExample 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_namebook_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>
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

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.javaBooks.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
<mapping resource> declaration.table-filter value in hibernate.revenge.xml.