CRUD with Spring MVC & Hibernate

6. Create Service Classes

In application, service class acts as a business layer. It performs data processing and transactions. In this example we will make service for Category and Product and only include data processing. Similar with repository pattern, in service package we also need to declare interface to make loosely coupled applications.

For this example, we will make:

  • CategoryService.java
  • CategoryServiceDB.java
  • ProductService.java
  • ProductServiceDB.java
package org.munif.service;

import java.util.List;

import org.munif.domain.Category;

public interface CategoryService {
    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.service;

import java.util.List;

import org.munif.domain.Category;
import org.munif.repository.CategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceDB implements CategoryService {

<pre><code>// It will be wired with CategoryRepositoryDB, because it has @Repository annotation
@Autowired
private CategoryRepository categoryRepository;

@Override
public List&amp;lt;Category&amp;gt; findAll() {
    return categoryRepository.findAll();
}

@Override
public Category findById(Integer categoryId) {
    return categoryRepository.findById(categoryId);
}

@Override
public void addCategory(Category category) {
    categoryRepository.addCategory(category);
}

@Override
public void editCategory(Category category, Integer categoryId) {
    categoryRepository.editCategory(category, categoryId);
}

@Override
public void removeCategory(Integer categoryId) {
    categoryRepository.removeCategory(categoryId);
}
</code></pre>

}
package org.munif.service;

import java.util.List;

import org.munif.domain.Product;

public interface ProductService {
    public List&lt;Product&gt; findAll();
    public Product findById(Integer productId);

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

}
package org.munif.service;

import java.util.List;

import org.munif.domain.Product;
import org.munif.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceDB implements ProductService {

<pre><code>// It will be wired with ProductRepositoryDB, because it has @Repository annotation
@Autowired
private ProductRepository productRepository;

@Override
public List&amp;lt;Product&amp;gt; findAll() {
    return productRepository.findAll();
}

@Override
public Product findById(Integer productId) {
    return productRepository.findById(productId);
}

@Override
public void addProduct(Product product) {
    productRepository.addProduct(product);
}

@Override
public void editProduct(Product product, Integer productId) {
    productRepository.editProduct(product, productId);
}

@Override
public void removeProduct(Integer productId) {
    productRepository.removeProduct(productId);
}
</code></pre>

}
Advertisements

3 thoughts on “CRUD with Spring MVC & Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s