Difference between revisions of "JPA - Java Persistance API"
(Created page with " ==ORM - Object Relational Mapping== ==Spring Data JPA==") |
|||
| Line 2: | Line 2: | ||
==[[ORM - Object Relational Mapping]]== | ==[[ORM - Object Relational Mapping]]== | ||
==[[Spring Data JPA]]== | ==[[Spring Data JPA]]== | ||
| + | |||
| + | |||
| + | ==Examples== | ||
| + | |||
| + | |||
| + | ===Making CRUD Operation with Repository=== | ||
| + | |||
| + | '''BrandRepository interface''' | ||
| + | |||
| + | <syntaxhighlight lang="java"> | ||
| + | package com.oasissofttech.springboot.brand; | ||
| + | |||
| + | import org.springframework.data.repository.CrudRepository; | ||
| + | |||
| + | public interface BrandRepository extends CrudRepository<Brand, Integer> { | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | |||
| + | '''BrandService Class''' | ||
| + | <syntaxhighlight lang="java"> | ||
| + | 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); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight> | ||
Latest revision as of 11:57, 3 June 2018
Contents
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);
}
}