Skip to content

Commit

Permalink
Records update - formatting correction (eugenp#17639)
Browse files Browse the repository at this point in the history
* Added Records as IDClass example

* Formatting correction

* Formatting correction
  • Loading branch information
abh1navv authored Oct 6, 2024
1 parent 81fc90a commit f095ac3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ public List<BookRecord> findAllBooks() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<BookRecord> query = cb.createQuery(BookRecord.class);
Root<Book> root = query.from(Book.class);
query.select(cb
.construct(BookRecord.class, root.get("id"), root.get("title"), root.get("author"), root.get("isbn")));
query.select(cb.construct(BookRecord.class, root.get("id"), root.get("title"),
root.get("author"), root.get("isbn")));
return entityManager.createQuery(query).getResultList();
}

public BookRecord findBookByTitle(String title) {
TypedQuery<BookRecord> query = entityManager
.createQuery("SELECT new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " +
"FROM Book b WHERE b.title = :title", BookRecord.class);
.createQuery("SELECT " +
"new com.baeldung.recordswithjpa.records.BookRecord(b.id, b.title, b.author, b.isbn) " +
"FROM Book b WHERE b.title = :title", BookRecord.class);
query.setParameter("title", title);
return query.getSingleResult();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public boolean isSameClass(Object object, SessionFactoryImplementor sessionFacto
}

@Override
public Object instantiate(final ValueAccess valueAccess, final SessionFactoryImplementor sessionFactoryImplementor) {
public Object instantiate(final ValueAccess valueAccess,
final SessionFactoryImplementor sessionFactoryImplementor) {
final String firstName = valueAccess.getValue(0, String.class);
final String secondName = valueAccess.getValue(1, String.class);
return new Author(firstName, secondName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public CustomBookRepositoryImpl(JdbcTemplate jdbcTemplate) {
}

public List<CustomBookRecord> findAllBooks() {
return jdbcTemplate.query("SELECT id, title FROM book", (rs, rowNum) -> new CustomBookRecord(rs.getLong("id"), rs.getString("title")));
return jdbcTemplate.query("SELECT id, title FROM book", (rs, rowNum) ->
new CustomBookRecord(rs.getLong("id"), rs.getString("title")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public class CompositeBookRepositoryIntegrationTest {

@Test
public void givenCompositeBook_whenSave_thenSaveToDatabase() {
CompositeBook compositeBook = new CompositeBook(new BookId(1L, 1234567890L), "Book Title", "Author Name");
CompositeBook compositeBook = new CompositeBook(new BookId(1L, 1234567890L),
"Book Title", "Author Name");
compositeBookRepository.save(compositeBook);

CompositeBook savedBook = compositeBookRepository.findById(new BookId(1L, 1234567890L)).orElse(null);
CompositeBook savedBook = compositeBookRepository
.findById(new BookId(1L, 1234567890L))
.orElse(null);
assertNotNull(savedBook);
assertEquals("Book Title", savedBook.getTitle());
assertEquals("Author Name", savedBook.getAuthor());
Expand Down

0 comments on commit f095ac3

Please sign in to comment.