Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Capital and labor are permissioned leverage. Everyone is chasing capital, but someone has to give it to you. Everyone is trying to lead, but someone has to follow you.
-Naval Ravikant
2018-05-12 17:08:21

Copy this link when reproducing:

Since the database was created, the next step will be using it in my application. I don't want to deal with the database directly. Instead, I want to select an ORM (Object-relational Mapping) framework. Since the Hibernate framework is the most popular, I want to know how it works.







1. Download Hibernate and related libraries.

    I. Open a web browser, navigate to, and find the latest version of Hibernate ORM framework.

        It is "5.2.11.Final" when I write this article.

    II. Use maven project and the following dependencies to download Hibernate ORM framework and other related libraries:

    <!-- Spring Transactions -->
    <!-- spring-tx-4.3.10.RELEASE.jar --> 
    <!-- Spring ORM support -->
    <!-- spring-orm-4.3.10.RELEASE.jar -->
    <!-- spring-jdbc-4.3.10.RELEASE.jar -->
    <!-- Hibernate Core -->
    <!-- hibernate-core-5.2.11.Final.jar -->
    <!-- jboss-logging-3.3.0.Final.jar -->
    <!-- hibernate-jpa-2.1-api-1.0.0.Final.jar -->
    <!-- javassist-3.20.0-GA.jar -->
    <!-- antlr-2.7.7.jar -->
    <!-- jboss-transaction-api_1.2_spec-1.0.1.Final.jar -->
    <!-- jandex-2.0.3.Final.jar -->
    <!-- classmate-1.3.0.jar -->
    <!-- dom4j-1.6.1.jar -->
    <!-- hibernate-commons-annotations-5.0.1.Final.jar -->    
    <!-- Hibernate Entity manager -->
    <!-- hibernate-entitymanager-5.2.11.Final.jar -->
    <!-- byte-buddy-1.6.14.jar -->

    <!-- MySQL Connector for Java -->
    <!-- mysql-connector-java-5.1.44.jar -->
    <!-- Apache Commons DBCP -->
    <!-- commons-dbcp2-2.1.1.jar -->
    <!-- commons-pool2-2.4.2.jar -->

FYI: referring to this page for detailed steps to use maven:

2. Copy the libraries which were just downloaded into the folder "\WebContent\WEB-INF\lib", and refresh the "today" project.

3. Modify the class "" to use JPA annotations for mapping:


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class Day {

	private int id;
	private String key;
	private String name;
	private String description;

	public Day() {
	public int getId() {
		return id;
	public void setId(int id) { = id;
	public String getKey() {
		return key;
	public void setKey(String key) {
		this.key = key;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getDescription() {
		return description;
	public void setDescription(String description) {
		this.description = description;
	public String toString () {

4. Modify the class "" to use Hibernate framework:


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


public class DayServiceImpl implements DayService {
	private SessionFactory sessionFactory;

	public Day GetByKey(String aKey) {

		Session session = this.sessionFactory.openSession();
		Query<Day> query = session.createQuery("from Day where key = :keyName", Day.class);
		// for test purpose, use 0915 instead of "aKey"
		query.setParameter("keyName", "0915");
		Day day = query.uniqueResult();
		return day;

5. Open the file "WebContent\WEB-INF\config\springmvc-config.xml", put the following lines into it to declare / define a data source:

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/DayDB" />
      <property name="username" value="root" />
      <property name="password" value="[Password]" />

6. Put the following lines into the file "WebContent\WEB-INF\config\springmvc-config.xml" to declare / define a session factory:

    <bean id="hibernateAnnotatedSessionFactory"
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
      <property name="hibernateProperties">
          <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          <prop key="hibernate.current_session_context_class">thread</prop>
          <prop key="hibernate.show_sql">false</prop>

7. Save all.

8. Right click on the "today" project, select "Run As -> Run on Server" on the popped menu to test if it works now.


9. It works!