BAEL-6651: Embeddable Records in Hibernate 6 (#14516)
* BAEL-6651: Bumped Spring Boot version * BAEL-6651: Replaced flaky test * BAEL-6651: Embeddable example with tests
This commit is contained in:
+5
-4
@@ -27,14 +27,15 @@ public class QueryService {
|
||||
return entityManager.createQuery(query).getResultList();
|
||||
}
|
||||
|
||||
public BookRecord findBookById(Long id) {
|
||||
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.id = :id", BookRecord.class);
|
||||
query.setParameter("id", id);
|
||||
.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();
|
||||
}
|
||||
|
||||
|
||||
public List<BookRecord> findAllBooksUsingMapping() {
|
||||
Query query = entityManager.createNativeQuery("SELECT * FROM book", "BookRecordMapping");
|
||||
return query.getResultList();
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public record Author(
|
||||
String firstName,
|
||||
String lastName
|
||||
) {}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.metamodel.spi.EmbeddableInstantiator;
|
||||
import org.hibernate.metamodel.spi.ValueAccess;
|
||||
|
||||
public class AuthorInstallator implements EmbeddableInstantiator {
|
||||
|
||||
public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return object instanceof Author;
|
||||
}
|
||||
|
||||
public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return object.getClass().equals(Author.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.recordswithjpa.embeddable;
|
||||
|
||||
import jakarta.persistence.Embedded;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.annotations.EmbeddableInstantiator;
|
||||
|
||||
@Entity
|
||||
@Table(name = "embeadable_author_book")
|
||||
public class EmbeddableBook {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String title;
|
||||
@Embedded
|
||||
@EmbeddableInstantiator(AuthorInstallator.class)
|
||||
private Author author;
|
||||
private String isbn;
|
||||
|
||||
public EmbeddableBook() {
|
||||
}
|
||||
|
||||
public EmbeddableBook(Long id, String title, Author author, String isbn) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.recordswithjpa.repository;
|
||||
|
||||
import com.baeldung.recordswithjpa.embeddable.EmbeddableBook;
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface EmbeddableBookRepository extends CrudRepository<EmbeddableBook, Long> {
|
||||
@Query("SELECT b FROM EmbeddableBook b WHERE b.author = :author")
|
||||
List<EmbeddableBook> findBookByAuthor(@Param("author") Author author);
|
||||
|
||||
}
|
||||
+2
-7
@@ -1,13 +1,8 @@
|
||||
package com.baeldung.recordswithjpa;
|
||||
|
||||
import com.baeldung.recordswithjpa.entity.Book;
|
||||
import com.baeldung.recordswithjpa.records.BookRecord;
|
||||
import com.baeldung.recordswithjpa.repository.BookRepository;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -27,8 +22,8 @@ public class QueryServiceIntegrationTest extends RecordsAsJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
void findBookById() {
|
||||
BookRecord bookById = queryService.findBookById(1L);
|
||||
assertEquals("The Lord of the Rings", bookById.title());
|
||||
BookRecord bookByTitle = queryService.findBookByTitle("The Lord of the Rings");
|
||||
assertNotNull(bookByTitle);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.recordswithjpa;
|
||||
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import com.baeldung.recordswithjpa.embeddable.EmbeddableBook;
|
||||
import com.baeldung.recordswithjpa.repository.EmbeddableBookRepository;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
public class RecordsAsJpaEmbeddableIntegrationTest {
|
||||
@Autowired
|
||||
protected EmbeddableBookRepository bookRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
|
||||
Author author = new Author("J.R.R.", "Tolkien");
|
||||
EmbeddableBook book1 = new EmbeddableBook(null, "The Lord of the Rings", author, "978-0544003415");
|
||||
EmbeddableBook book2 = new EmbeddableBook(null, "The Hobbit", author, "978-0547928227");
|
||||
|
||||
bookRepository.save(book1);
|
||||
bookRepository.save(book2);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
bookRepository.deleteAll();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.recordswithjpa.repository;
|
||||
|
||||
import com.baeldung.recordswithjpa.RecordsAsJpaEmbeddableIntegrationTest;
|
||||
import com.baeldung.recordswithjpa.embeddable.Author;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmbeddableBookRepositoryIntegrationTest extends RecordsAsJpaEmbeddableIntegrationTest {
|
||||
|
||||
@Test
|
||||
void findBookByAuthor() {
|
||||
assertEquals(2, bookRepository.findBookByAuthor(new Author("J.R.R.", "Tolkien")).size());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user