Skip to content

Instantly share code, notes, and snippets.

@lol97
Created April 30, 2024 16:20
Show Gist options
  • Select an option

  • Save lol97/ed9f093d62be138d80331fef03c12f32 to your computer and use it in GitHub Desktop.

Select an option

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)
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