Springframework - Hibernate - MySQL - Maven Project


<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<name>SpringFramework-MVC-Tutorial-4-Hibernate Maven Webapp</name>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/oasis_test_db</property>
        <property name="hibernate.connection.username">oasisdbtestuser</property>
        <property name="hibernate.connection.password">passwd</property>
        <!--  <property name="hbm2ddl.auto">create</property>  - deletes table and create new -->
        <!--  <property name="hbm2ddl.auto">update</property>  - if exist run the query, otherwise create table first-->
        <!--  <property name="show_sql">true</property>  - shows all queries created by hibernate-->

POJO class

package com.oasissofttech.springmvc;

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

@Table(name = "PARAMETER_TABLE")
public class ParameterTable {
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "PRM_ID")
	private int pid;
	@Column(name = "PRM_NAME")
	private String parameterName;

	@Column(name = "PRM_DESC")
	private String parameterDesc;

	//transient for temporary data
	private String doNotCreateThisColumnInTable;

	public ParameterTable() {

	public ParameterTable(int pid, String parameterName, String parameterDesc) {
		this.pid = pid;
		this.parameterName = parameterName;
		this.parameterDesc = parameterDesc;

	public int getPid() {
		return pid;

	public void setPid(int pid) {
		this.pid = pid;

	public String getParameterName() {
		return parameterName;

	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;

	public String getParameterDesc() {
		return parameterDesc;

	public void setParameterDesc(String parameterDesc) {
		this.parameterDesc = parameterDesc;

	public String getDoNotCreateThisColumnInTable() {
		return doNotCreateThisColumnInTable;

	public void setDoNotCreateThisColumnInTable(String doNotCreateThisColumnInTable) {
		this.doNotCreateThisColumnInTable = doNotCreateThisColumnInTable;


Application main class

package com.oasissofttech.springmvc;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class App {

	public static void main(String[] args) {
		ParameterTable prm = new ParameterTable();
		prm.setParameterDesc("Countries description");

		Configuration con = new Configuration().configure().addAnnotatedClass(ParameterTable.class);
		SessionFactory buildSessionFactory = con.buildSessionFactory();
		Session session = buildSessionFactory.openSession();
		Transaction transaction = session.beginTransaction();



Example of @ForeignKey in Hibernate

Hibernate allows to keep foreign key name. Hibernate overrides the foreign key name by @ForeignKey. It has the attribute name that should be defined. Find the example.

- @ForeignKey(name="FK_COUNTRY")
- @ManyToOne
- @OneToMany(cascade=CascadeType.ALL)
- @JoinColumn(name="country_id")
@Table(name = "state")
public class State {
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String name;
	private Country country;

public class Country implements Serializable {
	private static final long serialVersionUID = 1L;
	private int id;
	private String name;
	private Set<State> states;

Eager and Lazy Loading

The first thing that we should discuss here is what lazy loading and eager loading are:

Eager Loading is a design pattern in which data initialization occurs on the spot

Lazy Loading is a design pattern which is used to defer initialization of an object as long as it’s possible Let’s see how this actually works with some examples:

Lazy Loading


  • Initial load time much smaller than in the other approach
  • Less memory consumption than in the other approach


  • Delayed initialization might impact performance during unwanted moments
  • In some cases you need to handle lazily-initialized objects with a special care or you might end up with an exception

Eager Loading:


  • No delayed initialization related performance impacts


  • Long initial loading time
  • Loading too much unnecessary data might impact performance

The UserLazy class:

@Table(name = "USER")
public class UserLazy implements Serializable {
    @Column(name = "USER_ID")
    private Long userId;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    private Set<OrderDetail> orderDetail = new HashSet();
    // standard setters and getters
    // also override equals and hashcode

The OrderDetail class:

@Table (name = "USER_ORDER")
public class OrderDetail implements Serializable {
    private Long orderId;
    @ManyToOne(fetch = FetchType.LAZY)
    private UserLazy user;
    // standard setters and getters
    // also override equals and hashcode

One User can have multiple OrderDetails. In eager loading strategy, if we load the User data, it will also load up all orders associated with it and will store it in a memory.

But, when lazy loading is enabled, if we pull up a UserLazy, OrderDetail data won’t be initialized and loaded into a memory until an explicit call is made to it.

source: https://www.baeldung.com/hibernate-lazy-eager-loading

JBOSS - Hibernate Tools

JBoss Tools eClipse plugin is very powerfull tool to manage your hibernate objects and queries.

Hibernate Tools provides easy generation, testing and prototyping of your Hibernate or JPA mapped projects. Use it to Run queries, browse mappings and generate code for your data projects.

Hibernate Code Generation Launch

Generate artifacts based on Hibernate model

Hibernate Tools provide a code generation launch configuration which can take the configured Hibernate model and use for various code generation tasks.

Since it is a launch configuration it is easy to run it repeatedly while developing via the Launch Configuration menu.


Reverse Engineering


Hibernate Console

Browse your model & database

Mapping Diagram

Mapping Diagram controls

HQL Editor

Prototype your queries In the HQL editor you can prototype your HQL queries and see the result in the Hibernate Query Result viewer.

Dynamic SQL Preview


Criteria Editor

hbm.xml XML Editor


Reverse Engineering Strategy Editor



Hibernate eclipse plugin - JBOSS : https://tools.jboss.org/features/hibernate.html