Created
July 22, 2019 11:53
-
-
Save KiryhaPikoff/9c62c9250ce84c08e273e3122643c4ed to your computer and use it in GitHub Desktop.
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 projects.mvc.first.repositorys; | |
import org.apache.log4j.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Repository; | |
import projects.mvc.first.entitys.Person; | |
import projects.mvc.first.exceptions.db.NoteNotFoundException; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.criteria.CriteriaBuilder; | |
import javax.persistence.criteria.CriteriaQuery; | |
import javax.persistence.criteria.Root; | |
import java.util.List; | |
@Repository(value = "personBaseDaoImpl") | |
public class PersonBaseDaoImpl implements PersonBaseDAO { | |
/* Что будет лучше: делать свою реализацию или унаследовать | |
PersonBaseDAO от CrudRepository, где она сама сгенерируется? */ | |
private static final Logger log = Logger.getLogger(SomeKindPersonBase.class); | |
@Autowired | |
EntityManager entityManager; | |
@Override | |
public Person getPerson(Integer id) throws NoteNotFoundException { | |
Person person = entityManager.find(Person.class, id); | |
if(person != null) { | |
log.info("Successfully found note with id=" + id); | |
return person; | |
} else { | |
throw new NoteNotFoundException("Not found note with id=" + id); | |
} | |
} | |
@Override | |
public List<Person> getAllPeople() { | |
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); | |
CriteriaQuery<Person> personCriteria = cb.createQuery(Person.class); | |
Root<Person> personRoot = personCriteria.from(Person.class); | |
personCriteria.select(personRoot); | |
return entityManager.createQuery(personCriteria).getResultList(); | |
} | |
@Override | |
public void addPerson(Person person) { | |
EntityTransaction transaction = entityManager.getTransaction(); | |
transaction.begin(); | |
entityManager.persist(person); | |
transaction.commit(); | |
} | |
@Override | |
public void deletePerson(Integer id) throws NoteNotFoundException { | |
EntityTransaction transaction = entityManager.getTransaction(); | |
Person person = entityManager.find(Person.class, id); | |
if(person != null) { | |
transaction.begin(); | |
entityManager.remove(person); | |
transaction.commit(); | |
log.info("Successfully delete note with id=" + id); | |
} else { | |
throw new NoteNotFoundException("Delete operation failed. A note with id=" + id + " not exists"); | |
} | |
} | |
@Override | |
public void updatePerson(Person person) throws NoteNotFoundException { | |
EntityTransaction transaction = entityManager.getTransaction(); | |
if(entityManager.find(Person.class, person.getId()) != null) { | |
transaction.begin(); | |
entityManager.merge(person); | |
transaction.commit(); | |
log.info("Successfully update note with id=" + person.getId()); | |
} else { | |
throw new NoteNotFoundException("Updating operation failed. A note with id=" + person.getId() + " not exists"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment