Connection to DB using Hibernate
To connect to DB using Hibernate users have to make the next steps:
- Create an environment at the platform
- Add a database node to this environment
- Modify some configuration files in a web-app
- Execute queries
Let’s do it step by step:
1. Create environment with a database server (MySQL in our case):
2. Create a new user in a database:
How to create a new user - click here.
|
|
For this example we’ve created table books with fields book_name and book_author inside the iNET.elasticDb database.
3. Modify the following configuration files of your web-application:
hibernate.cfg.xml
|
|
Note: Don’t miss to put the correct values to the hibernate.connection.url string instead of the text inside curly brackets.
jdbc:mysql://mysql{node_id}.{your_env_name}.{hoster_domain}:3306/iNET.elasticDb
where {node_id} - ID of the container with MySQL server you want to receive the access to. It can be seen at the dashboard:
hibernate.revenge.xml
|
|
For the next step we’ve used reverse engineering mechanism and got 2 files in our web-project:
- Books.java
- Books.hbm.xml
Also you need to create the HibernateUtil.java file but do not need to change it.
4. Create a simple Java method, which will add a new row to the books table in our database:
|
|
Note that you have to put the connector for database (.jar library) to your project or to the appropriate web-server’s folder in the environment.