Programming     Travel Logs     Life Is Good     Surfing Online     About Me
Leverage is a force multiplier for your judgement.
-Naval Ravikant
2018-05-12 17:08:21

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/92

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.

/Images/20170914/01.jpg

/Images/20170914/02.jpg

/Images/20170914/03.jpg

/Images/20170914/04.jpg

/Images/20170914/05.jpg

/Images/20170914/06.jpg

1. Download Hibernate and related libraries.

    I. Open a web browser, navigate to http://hibernate.org/orm/, 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 --> 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>    
    
    <!-- Spring ORM support -->
    <!-- spring-orm-4.3.10.RELEASE.jar -->
    <!-- spring-jdbc-4.3.10.RELEASE.jar -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>    
    
    <!-- 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 -->    
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.11.Final</version>
    </dependency>
    
    <!-- Hibernate Entity manager -->
    <!-- hibernate-entitymanager-5.2.11.Final.jar -->
    <!-- byte-buddy-1.6.14.jar -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.2.11.Final</version>
    </dependency>

    <!-- MySQL Connector for Java -->
    <!-- mysql-connector-java-5.1.44.jar -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.44</version>
    </dependency>
    
    <!-- Apache Commons DBCP -->
    <!-- commons-dbcp2-2.1.1.jar -->
    <!-- commons-pool2-2.4.2.jar -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
      <version>2.1.1</version>
    </dependency>

FYI: referring to this page for detailed steps to use maven: http://www.casperlee.com/x/y/blog/82

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

3. Modify the class "com.casperlee.today.domain.day" to use JPA annotations for mapping:

package com.casperlee.today.domain;

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

@Entity
@Table(name="day")
public class Day {

	@Id
	@Column(name="dayId")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	
	@Column(name="dayKey")
	private String key;
	
	@Column(name="dayName")
	private String name;
	
	@Column(name="dayDescription")
	private String description;

	public Day() {
		
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.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) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@Override
	public String toString () {
		
		return this.name;
	}
}

4. Modify the class "com.casperlee.today.service.DayServiceImpl" to use Hibernate framework:

package com.casperlee.today.service;

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;

import com.casperlee.today.domain.Day;

@Service
public class DayServiceImpl implements DayService {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	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"
        destroy-method="close">
      <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]" />
    </bean>

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

    <bean id="hibernateAnnotatedSessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
        <list>
          <value>com.casperlee.today.domain.Day</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <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>
        </props>
      </property>
    </bean>

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.

/Images/20170914/103.jpg

9. It works!