Advertisement
purluno

purluno.springrain.book.BookService

Apr 15th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.92 KB | None | 0 0
  1. package purluno.springrain.book
  2.  
  3. import javax.annotation.Resource
  4.  
  5. import org.hibernate.Session
  6. import org.hibernate.SessionFactory
  7. import org.springframework.stereotype.Service
  8. import org.springframework.transaction.TransactionStatus
  9. import org.springframework.transaction.support.TransactionCallback
  10. import org.springframework.transaction.support.TransactionTemplate
  11.  
  12. @Service
  13. class BookService {
  14.     /**
  15.      * 트랜잭션 관리를 책임지는 서비스 객체
  16.      */
  17.     @Resource
  18.     TransactionTemplate transactionTemplate
  19.  
  20.     /**
  21.      * 데이터베이스 서비스 객체
  22.      */
  23.     @Resource
  24.     SessionFactory sessionFactory
  25.  
  26.     /**
  27.      * 특정 ISBN을 가진 책 정보를 가져온다.
  28.      *
  29.      * @param
  30.      *     isbn ISBN
  31.      * @return
  32.      *     책 정보 객체. 또는, 책이 없을 경우 null.
  33.      */
  34.     Book get(String isbn) {
  35.         assert isbn != null, "ISBN은 null이 아니어야 합니다."
  36.         assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
  37.         transactionTemplate.execute({
  38.             def session = sessionFactory.currentSession
  39.             session.get(Book, isbn)
  40.         } as TransactionCallback)
  41.     }
  42.  
  43.     /**
  44.      * 책 정보를 생성한다.
  45.      *
  46.      * @param isbn
  47.      * @param title
  48.      * @param author
  49.      * @param year
  50.      * @param intro
  51.      * @param authorIntro
  52.      */
  53.     void save(String isbn, String title, String author, Integer year,
  54.             String intro, String authorIntro) {
  55.         assert isbn != null, "ISBN은 null이 아니어야 합니다."
  56.         assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
  57.         transactionTemplate.execute({
  58.             def session = sessionFactory.currentSession
  59.             def book = new Book()
  60.             book.isbn = isbn
  61.             book.title = title
  62.             book.author = author
  63.             book.year = year
  64.             book.intro = intro
  65.             book.authorIntro = authorIntro
  66.             session.save(book)
  67.         } as TransactionCallback)
  68.     }
  69.  
  70.     /**
  71.      * 책 정보를 갱신한다.
  72.      *
  73.      * @param isbn
  74.      * @param title
  75.      * @param author
  76.      * @param year
  77.      * @param intro
  78.      * @param authorIntro
  79.      */
  80.     void update(String isbn, String title, String author, Integer year,
  81.             String intro, String authorIntro) {
  82.         assert isbn != null, "ISBN은 null이 아니어야 합니다."
  83.         assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
  84.         transactionTemplate.execute({
  85.             def session = sessionFactory.currentSession
  86.             def book = session.load(Book, isbn) as Book
  87.             book.title = title
  88.             book.author = author
  89.             book.year = year
  90.             book.intro = intro
  91.             book.authorIntro = authorIntro
  92.         } as TransactionCallback)
  93.     }
  94.  
  95.     /**
  96.      * 책 정보를 삭제한다.
  97.      *
  98.      * @param isbn
  99.      *     삭제할 책 정보의 ISBN
  100.      */
  101.     void delete(String isbn) {
  102.         assert isbn != null, "ISBN은 null이 아니어야 합니다."
  103.         assert !isbn.empty, "ISBN은 빈 문자열이 아니어야 합니다."
  104.         transactionTemplate.execute({
  105.             def session = sessionFactory.currentSession
  106.             session.delete(new Book(isbn: isbn))
  107.         } as TransactionCallback)
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement