CRUD with Spring MVC & Hibernate

5. Create Repository Class

Repository class is a class that performs as data supplier. The data can be from database or mockup (synthetic data). It is encouraged to make an abstraction using interface for each repository. It will make a program become loosely coupled.

A repository usually consists of the following methods:

  • findAll(): returns all the data.
  • findById(): returns the data with certain id.
  • add(): insert new data to the table.
  • edit(): update data in the table.
  • remove(): delete data from the table.
  • any other methods for data access/query.

For data access to the MySQL server, we will use session factory object from Hibernate.

For this example, we need to make:

  • Category
    • CategoryRepository.java. It is interface for category repository.
    • CategoryRepositoryDB.java. It is concrete class that implements CategoryRepository interface and act as repository service (@Repository annotation).
  • Product
    • ProductRepository.java. It is interface for a product repository.
    • ProductRepositoryDB.java. It is concrete class that implements CategoryRepository interface and act as repository service (@Repository annotation).

Make sure that you put these classes in the repository package.

package org.munif.repository;

import java.util.List;

import org.munif.domain.Category;

public interface CategoryRepository {
    public List<Category> findAll();
    public Category findById(Integer categoryId);

<pre><code>public void addCategory(Category category);
public void editCategory(Category category, Integer categoryId);
public void removeCategory(Integer categoryId);
</code></pre>

}
package org.munif.repository;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.munif.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class CategoryRepositoryDB implements CategoryRepository {

<pre><code>@Autowired
private SessionFactory sessionFactory; // session factory object

private Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}

@SuppressWarnings(&amp;quot;unchecked&amp;quot;)
@Override
@Transactional
public List&amp;lt;Category&amp;gt; findAll() {
    return getCurrentSession().createQuery(&amp;quot;from Category&amp;quot;).list();
}

@Override
@Transactional
public Category findById(Integer categoryId) {
    return (Category)getCurrentSession().get(Category.class, categoryId);
}

@Override
@Transactional
public void addCategory(Category category) {
    getCurrentSession().save(category);
}

@Override
@Transactional
public void editCategory(Category category, Integer categoryId) {
    Category categoryToUpdate = findById(categoryId);
    System.out.println(categoryToUpdate);
    if(categoryToUpdate != null) {
        System.out.println(&amp;quot;Updating...&amp;quot;);
        categoryToUpdate.setCategoryName(category.getCategoryName());
        categoryToUpdate.setCategoryDescription(category.getCategoryDescription());
        getCurrentSession().update(categoryToUpdate);
        getCurrentSession().flush();
    }
}

@Override
@Transactional
public void removeCategory(Integer categoryId) {
    Category categoryToDelete = findById(categoryId);
    System.out.println(categoryToDelete);
    if(categoryToDelete != null) {
        getCurrentSession().delete(categoryToDelete);

        getCurrentSession().flush();
    }
}
</code></pre>

}
package org.munif.repository;

import java.util.List;

import org.munif.domain.Product;

public interface ProductRepository {

<pre><code>public List&amp;lt;Product&amp;gt; findAll();
public Product findById(Integer productId);

public void addProduct(Product product);
public void editProduct(Product product, Integer productId);
public void removeProduct(Integer productId);
</code></pre>

}
package org.munif.repository;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.munif.domain.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductRepositoryDB implements ProductRepository {

<pre><code>@Autowired
private SessionFactory sessionFactory; // session factory object

private Session getCurrentSession() {
    return sessionFactory.getCurrentSession();
}

@SuppressWarnings(&amp;quot;unchecked&amp;quot;)
@Override
@Transactional
public List&amp;lt;Product&amp;gt; findAll() {
    return getCurrentSession().createQuery(&amp;quot;from Product&amp;quot;).list();
}

@Override
@Transactional
public Product findById(Integer productId) {
    return (Product)getCurrentSession().get(Product.class, productId);
}

@Override
@Transactional
public void addProduct(Product product) {
    getCurrentSession().save(product);
}

@Override
@Transactional
public void editProduct(Product product, Integer productId) {
    Product productToEdit = findById(productId);
    if(productToEdit != null) {
        productToEdit.setProductName(product.getProductName());
        productToEdit.setProductPrice(product.getProductPrice());
        productToEdit.setProductStock(product.getProductId());
        productToEdit.setProductDescription(product.getProductDescription());
        productToEdit.setCategory(product.getCategory());

        getCurrentSession().update(productToEdit);
    }
}

@Override
@Transactional
public void removeProduct(Integer productId) {
    Product productToRemove = findById(productId);
    if(productToRemove != null) {
        getCurrentSession().delete(productToRemove);
    }
}
</code></pre>

}

3 thoughts on “CRUD with Spring MVC & Hibernate

Leave a comment