Created
January 26, 2017 08:49
-
-
Save HelloThisIsFlo/e06fdde3ed50fd44226b0cbcc7fb788a to your computer and use it in GitHub Desktop.
This file contains 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
public class HibernateBookRepository implements BookRepository { | |
private final HibernateCrudBookRepository crudBookRepository; | |
private final JpaMapper jpaMapper; | |
@Autowired | |
public HibernateBookRepository(HibernateCrudBookRepository crudBookRepository, JpaMapper jpaMapper) { | |
this.crudBookRepository = crudBookRepository; | |
this.jpaMapper = jpaMapper; | |
} | |
@Override | |
public BookId save(Book book) { | |
BookJpaEntity bookJpaEntity = jpaMapper.map(book); | |
crudBookRepository.save(bookJpaEntity); | |
Book saved = jpaMapper.map(bookJpaEntity); | |
// Set id | |
book.setId(saved.id()); | |
return saved.id(); | |
} | |
@Override | |
public Book findById(BookId bookId) { | |
BookJpaEntity jpaEntity = crudBookRepository.findOne(parseId(bookId)); | |
return jpaMapper.map(jpaEntity); | |
} | |
private long parseId(BookId bookId) { | |
return Long.parseLong(bookId.idString()); | |
} | |
@Override | |
public List<Book> findAll() { | |
Iterable<BookJpaEntity> allJpaEntities = crudBookRepository.findAll(); | |
List<Book> result = new ArrayList<>(); | |
for (BookJpaEntity jpaEntity : allJpaEntities) { | |
Book book = jpaMapper.map(jpaEntity); | |
result.add(book); | |
} | |
return result; | |
} | |
@Override | |
public Book findByTitle(String title) { | |
// Implementation ... | |
} | |
@Override | |
public Book findByAuthor(String author) { | |
// Implementation ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment