CALL 1.800.975.6814    LIVE CHAT

@dailyrazor Blog

How to Install Hibernate ORM

March 7 2017 0
how to install hibernate

In this article we will show you how to install Hibernate along with needed packages in Tomcat for the development of Hibernate based web applications. Hibernate is an open source, lightweight, high performance ORM (Object Relational Mapping) tool. In addition and as you may already know, Hibernate is a widely used ORM tool due to its high performance features.

In fact, here at DailyRazor we are an early adaptor of the latest web development technologies. A large number of websites, web applications and web services based on Hibernate framework run optimally on our hosting infrastructure. Therefore, our objective is to show you how to easily install Hibernate on either our Private or Shared Tomcat hosting servers for use in development and deployment of any Java web application .

 

Install Hibernate Environment in Tomcat

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Hibernate Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

Step 1: Create a MySQL Server Database and Database User

 
First and foremost to install Hibernate, the framework needs a database connection to work with. Therefore, you need to create a database. In this article we are assuming that you have already created database and user with all privileges. Otherwise, log into your control and follow this tutorial to create a MySQL database and database user.

Step 2: Configure Database settings in web application:

Next, before you can deploy your WAR file to the remote Tomcat on our hosting infrastructure, you need to add the remote database connection details in your web application. Presently, for a Hibernate based application connection parameters are stipulated in the hibernate.cfg.xml file. Whereas, in some other Java frameworks like Spring, Spring Boot, etc database connections are stipulated in either applicationContext.xml or application.properties files.

As a result, in this section we are providing some set of properties’ and xml file you can use to configure the database for your application. Kindly contact us at support@dailyrazor.com if you have any problem even after reading this article. Therefore, in the MySQL and SQL Server database related configuration files provided below, you need to change these database parameters with yours as follows:

  • Replace “database-name” with your database name.
  • Replace “database-user” with your database user.
  • Replace “database-password” with your database password.

 

Sample hibernate.cfg.xml file for MySql

<?xml version="1.0" encoding="utf-8"?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/database-name</property>
<property name="connection.username">database-user</property>
<property name="connection.password">database-password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">none</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

 

Sample MySql connection application.properties file for Spring Boot framework

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/database-name
spring.datasource.username=database-user
spring.datasource.password=database-password
spring.jpa.database-platform =org.hibernate.dialect.MySQL5Dialect

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Hibernate Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

Sample MySql applicationContext.xml file for Spring framework

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
  xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util" xmlns:data="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
      <context:component-scan base-package="com.dailyrazor.*" />
      <context:annotation-config />
      <data:repositories base-package="com.dailyrazor" />
      <bean id="propertyPlaceholderConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
	<property name="searchSystemEnvironment" value="true" />
      </bean>
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	  <property name="dataSource" ref="dataSource" />
	  <property name="packagesToScan" value="com.dailyrazor.beans" />
	  <property name="jpaVendorAdapter">
	  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
	    </property>
	    <property name="jpaProperties">
	      <props>
		<prop key="hibernate.hbm2ddl.auto">none</prop>
		  <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
	      </props>
	    </property>
	</bean>
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
	  <property name="url" value="jdbc:mysql://localhost:3306/database-name" />
	  <property name="username" value="database-user" />
	  <property name="password" value="database-password" />
	  <property name="initialSize" value="5"/>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	  <property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	  <property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Hibernate Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

Sample hibernate.cfg.xml file for SQL Server

<?xml version="1.0" encoding="utf-8"?>
<!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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=database-name</property>
<property name="connection.username">database-user</property>
<property name="connection.password">database-password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">none</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>

 

Sample SQL Server applicationContext.xml file for Spring framework

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:data="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<context:component-scan base-package="com.dailyrazor.*" />
<context:annotation-config />

<data:repositories base-package="com.dailyrazor" />

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.dailyrazor.beans" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=database-name" />
<property name="username" value="database-user" />
<property name="password" value="database-password" />
<property name="initialSize" value="5"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Hibernate Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]

 

Step 3: Build your web application again after database configuration:

Once you’ve completed Steps 1 & 2, you need to create a WAR file for your web application. As a matter of fact, best practice recommends you create the WAR file and deploy it locally first of all. In the event you have not performed proper database configuration via the any of the above mentioned files, the WAR file will not deploy properly on your local PC.

Note:In order to connect to a remote database server from your local PC, you should allow remote connection to your database. Here’s a quick guide on how to do this via your hosting server’s control panel.

Step 4: Deploy your web application WAR file using Filezilla:

Once your WAR file is ready and deployed successfully on your local Tomcat installation, go ahead and upload it to our hosting server using an FTP client such as Filezilla.

For instance, in this article we will deploy “HibernateDemoDailyRazor.war” file in Tomcat. It is located at “D:\learning\hibernate-application\HibernateDemoDailyRazor\target\HibernateDemoDailyRazor.war” on our local PC.

Step 4.1 – Open the FTP client on your local PC

Locate the Filezilla application (or any other FTP client you’re using) on your local PC and open it.
install hibernate

Step 4.2 – Login with the login credentials provided to you after your hosting server set up

At this point, you need to enter your FTP information to connect to remote hosting server. For Host we suggest you to enter your server’s IP address. You can also enter your domain name if it’s already pointing to our servers. Once you have provided all the information correctly. Click on Quickconnect icon to access the server.
“Directory listing successful” – This message will appear once Filezilla is successfully connected to the server. Once successfully connected, Filezilla is ready to communicate with the server. After successful connection Filezilla would look like the image below.
install hibernate
NOTE: In Filezilla, the left panel shows folders/directories in your local computer and the right Panel shows your remote host’s folders/directories.

Step 4.3 – Locate the required folders on your local computer and the remote server.

First of all, locate the folder/directory on your local computer where your .war file resides. In our case, we want to deploy “HibernateDemoDailyRazor.war” which is located at “D:\learning\hibernate-application\HibernateDemoDailyRazor\target\HibernateDemoDailyRazor.war” in our local system. As a result, in the left panel of Filezilla, we’ll open “D:\learning\hibernate-application\HibernateDemoDailyRazor\target”.

Next, locate the “webapps” folder/directory on the remote server. We need to upload “HibernateDemoDailyRazor.war” to “/home/privtomc/tomcat/webapps/privatetomcat.readwritebox.com/” So we have opened the directory in right panel of Filezilla.
install hibernate

Step 4.4 : Uploading your .war file or “complete web directory”

Next, please select your .war file in the left panel. Right click and select Upload. See the below image showing the process to upload HibernateDemoDailyRazor.war. It will upload your .war to the selected directory on the remote server.
install hibernate
Note: This same method can be used to upload HibernateDemoDailyRazor directory as well (i.e. if did not park your web application into a WAR file).

At any rate, once the .war has been moved to remote server, it gets extracted automatically if you are using a Private Tomcat server. You can see in the image below HibernateDemoDailyRazor.war has been extracted to HibernateDemoDailyRazor. If you’re using a Shared Tomcat server, you need to request Tomcat restart by emailing support@dailyrazor.com
install hibernate

Step 4.5 : Check your url after successful deployment

In conclusion, we have deployed HibernateDemoDailyRazor.war file. Since our base url is http://PrivateTomcatexamples.dailyrazor.com/ we can find the complete web application at http://PrivateTomcatexamples.dailyrazor.com/HibernateDemoDailyRazor

Advice: Some common problems while deploying Hibernate based web application

  • java.lang.OutOfMemoryError: GC overhead limit exceeded:
  • If your war file size is more than 200 MB and you’re trying to deploy WAR via Tomcat Manager application. Please use Filezilla to upload your war file. Deploying war file using Tomcat Manager may cause this error.

  • java.util.zip.ZipException: error in opening zip file
  • Restart your Tomcat server to solve this issue.

  • java.sql.SQLException: Access denied for user ‘someuser’@’localhost’ (using password: YES)
  • This error occurs if you are using invalid database credentials. Make sure you’re using the correct database credentials. Make sure the database user exists in your remote database server and make sure all privileges are assigned to the user.

 
Thanks for reading this article. In case you need any help regarding Hibernate framework deployment, please email us at support@dailyrazor.com. We are available 24/7 and very much looking forward to assisting you.

 

[SPECIAL OFFER]: 15 Days FREE Trial Java Web Hosting

[BENEFITS]:

  • Host Your Hibernate Web Application for Free!
  • Private Tomcat Server with Dedicated JVM Heap & PermGen
  • Custom Java Development Kit (JDK) version
  • Super Fast Servers with 24/7/365 Technical Support

Click here to access this [SPECIAL OFFER]