Created
April 30, 2024 16:20
-
-
Save lol97/ed9f093d62be138d80331fef03c12f32 to your computer and use it in GitHub Desktop.
BarangService untuk artilek Spring boot Hibernate Basic 1 CRUD Menggunakan database H2 (sufyan97.com)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.sufyan97.learn_hibernate.barang; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.stereotype.Service; | |
| import org.springframework.transaction.annotation.Propagation; | |
| import org.springframework.transaction.annotation.Transactional; | |
| @Service | |
| public class BarangService { | |
| @Autowired | |
| protected BarangRepository barangRepository; | |
| @Transactional(propagation = Propagation.REQUIRED) | |
| public Barang store(Barang barang) { | |
| Barang save = barangRepository.save(barang); | |
| return save; | |
| } | |
| public Barang read(Long id) throws Exception { | |
| return barangRepository.findById(id).orElseThrow(() -> new Exception("id barang" + id + " tidak ditemukan")); | |
| } | |
| @Transactional(propagation = Propagation.REQUIRED) | |
| public void update(Barang barang) throws Exception{ | |
| barangRepository.save(barang); | |
| } | |
| @Transactional(propagation = Propagation.REQUIRED) | |
| public void destroy(Long id) throws Exception { | |
| Barang read = this.read(id); | |
| barangRepository.delete(read); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment