|
8 | 8 | import org.hibernate.SessionFactory; |
9 | 9 | import org.hibernate.transform.Transformers; |
10 | 10 |
|
11 | | -import com.iluwatar.cqrs.dto.AuthorDTO; |
12 | | -import com.iluwatar.cqrs.dto.BookDTO; |
| 11 | +import com.iluwatar.cqrs.dto.Author; |
| 12 | +import com.iluwatar.cqrs.dto.Book; |
13 | 13 | import com.iluwatar.cqrs.util.HibernateUtil; |
14 | 14 |
|
| 15 | +/** |
| 16 | + * This class is an implementation of {@link IQueryService}. It uses Hibernate native queries to return DTOs from the |
| 17 | + * database. |
| 18 | + * |
| 19 | + */ |
15 | 20 | public class QueryServiceImpl implements IQueryService { |
16 | 21 |
|
17 | 22 | private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); |
18 | 23 |
|
19 | 24 | @Override |
20 | | - public AuthorDTO getAuthorByUsername(String username) { |
| 25 | + public Author getAuthorByUsername(String username) { |
21 | 26 | Session session = sessionFactory.openSession(); |
22 | 27 | SQLQuery sqlQuery = session |
23 | 28 | .createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\"" |
24 | 29 | + "FROM Author a where a.username=:username"); |
25 | 30 | sqlQuery.setParameter("username", username); |
26 | | - AuthorDTO authorDTO = (AuthorDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(AuthorDTO.class)) |
27 | | - .uniqueResult(); |
| 31 | + Author authorDTo = (Author) sqlQuery.setResultTransformer(Transformers.aliasToBean(Author.class)).uniqueResult(); |
28 | 32 | session.close(); |
29 | | - return authorDTO; |
| 33 | + return authorDTo; |
30 | 34 | } |
31 | 35 |
|
32 | 36 | @Override |
33 | | - public BookDTO getBook(String title) { |
| 37 | + public Book getBook(String title) { |
34 | 38 | Session session = sessionFactory.openSession(); |
35 | 39 | SQLQuery sqlQuery = session |
36 | 40 | .createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title"); |
37 | 41 | sqlQuery.setParameter("title", title); |
38 | | - BookDTO bookDTO = (BookDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).uniqueResult(); |
| 42 | + Book bookDTo = (Book) sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).uniqueResult(); |
39 | 43 | session.close(); |
40 | | - return bookDTO; |
| 44 | + return bookDTo; |
41 | 45 | } |
42 | 46 |
|
43 | 47 | @Override |
44 | | - public List<BookDTO> getAuthorBooks(String username) { |
| 48 | + public List<Book> getAuthorBooks(String username) { |
45 | 49 | Session session = sessionFactory.openSession(); |
46 | 50 | SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" |
47 | 51 | + " FROM Author a , Book b where b.author_id = a.id and a.username=:username"); |
48 | 52 | sqlQuery.setParameter("username", username); |
49 | | - List<BookDTO> bookDTOs = sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).list(); |
| 53 | + List<Book> bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list(); |
50 | 54 | session.close(); |
51 | | - return bookDTOs; |
| 55 | + return bookDTos; |
52 | 56 | } |
53 | 57 |
|
54 | 58 | @Override |
|
0 commit comments