JPA - Java Persistance API

Revision as of 11:57, 3 June 2018 by Rasimsen (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

ORM - Object Relational Mapping

Spring Data JPA

Examples

Making CRUD Operation with Repository

BrandRepository interface

package com.oasissofttech.springboot.brand;

import org.springframework.data.repository.CrudRepository;

public interface BrandRepository extends CrudRepository<Brand, Integer> {

}


BrandService Class

package com.oasissofttech.springboot.brand;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BrandService {
	@Autowired
	private BrandRepository brandRepository;
	
	public List<Brand> getAllBrands(){
		List<Brand> brands = new ArrayList<>();
		brandRepository.findAll()
		.forEach(brands::add);
		
		return brands;
	}
	
	
	public Optional<Brand> getBrand(int id) {
		return brandRepository.findById(id);
	}


	public void addBrand(Brand brand) {
		brandRepository.save(brand);
	}


	public void updateBrand(Brand brand, int id) {
		brandRepository.save(brand);
	}


	public void deleteBrand(int id) {
		brandRepository.deleteById(id);
	}
	
}