Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package purluno.springrain.book
- import javax.annotation.Resource
- import org.hibernate.Session
- import org.hibernate.SessionFactory
- import org.springframework.stereotype.Service
- import org.springframework.transaction.TransactionStatus
- import org.springframework.transaction.support.TransactionCallback
- import org.springframework.transaction.support.TransactionTemplate
- @Service
- class BookService {
- /**
- * 트랜잭션 관리를 책임지는 서비스 객체
- */
- @Resource
- TransactionTemplate transactionTemplate
- /**
- * 데이터베이스 서비스 객체
- */
- @Resource
- SessionFactory sessionFactory
- /**
- * 특정 ISBN을 가진 책 정보를 가져온다.
- *
- * @param
- * isbn ISBN
- * @return
- * 책 정보 객체. 또는, 책이 없을 경우 null.
- */
- Book get(String isbn) {
- assert isbn != null, "ISBN은 null이 아니어야 합니다."
- assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
- transactionTemplate.execute({
- def session = sessionFactory.currentSession
- session.get(Book, isbn)
- } as TransactionCallback)
- }
- /**
- * 책 정보를 생성한다.
- *
- * @param isbn
- * @param title
- * @param author
- * @param year
- * @param intro
- * @param authorIntro
- */
- void save(String isbn, String title, String author, Integer year,
- String intro, String authorIntro) {
- assert isbn != null, "ISBN은 null이 아니어야 합니다."
- assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
- transactionTemplate.execute({
- def session = sessionFactory.currentSession
- def book = new Book()
- book.isbn = isbn
- book.title = title
- book.author = author
- book.year = year
- book.intro = intro
- book.authorIntro = authorIntro
- session.save(book)
- } as TransactionCallback)
- }
- /**
- * 책 정보를 갱신한다.
- *
- * @param isbn
- * @param title
- * @param author
- * @param year
- * @param intro
- * @param authorIntro
- */
- void update(String isbn, String title, String author, Integer year,
- String intro, String authorIntro) {
- assert isbn != null, "ISBN은 null이 아니어야 합니다."
- assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
- transactionTemplate.execute({
- def session = sessionFactory.currentSession
- def book = session.load(Book, isbn) as Book
- book.title = title
- book.author = author
- book.year = year
- book.intro = intro
- book.authorIntro = authorIntro
- } as TransactionCallback)
- }
- /**
- * 책 정보를 삭제한다.
- *
- * @param isbn
- * 삭제할 책 정보의 ISBN
- */
- void delete(String isbn) {
- assert isbn != null, "ISBN은 null이 아니어야 합니다."
- assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
- transactionTemplate.execute({
- def session = sessionFactory.currentSession
- session.delete(new Book(isbn: isbn))
- } as TransactionCallback)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement