Skip to content

Instantly share code, notes, and snippets.

Avatar

Yann Briançon yannbriancon

View GitHub Profile
@yannbriancon
yannbriancon / UserRepository.java
Last active May 5, 2020
Eager Fetching With Result Limiting
View UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@EntityGraph(attributePaths = {"messages"})
List<User> findTop5By();
// JPQL does not support LIMIT
// We need to use Pageable to set the number of ids we want
@Query("SELECT id " +
"FROM User ")
List<Long> findIds(Pageable pageable);
@yannbriancon
yannbriancon / MessageRepository.java
Last active May 2, 2020
Criteria query with JoinType LEFT
View MessageRepository.java
List<Message> getAllBy() {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Message> query = criteriaBuilder.createQuery(Message.class);
Root<Message> message = query.from(Message.class);
// Add fetching of the author field
message.fetch(Message_.author, JoinType.LEFT);
query.select(message);
TypedQuery<Message> typedQuery = entityManager.createQuery(query);
View MessageRepository.java
@Query("SELECT *
FROM Message m
LEFT JOIN FETCH m.author")
List<Message> getAllBy();
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020
JPA query with entity graph
View MessageRepository.java
@EntityGraph(attributePaths = {"author"})
List<Message> getAllBy();
@yannbriancon
yannbriancon / NPlusOneQueryLoggingTest.java
Last active May 8, 2020
Test changing interceptor configuration to ERROR
View NPlusOneQueryLoggingTest.java
@RunWith(SpringRunner.class)
@SpringBootTest("hibernate.query.interceptor.error-level=ERROR")
@Transactional
class NPlusOneQueriesLoggingTest {
...
}
View NPlusOneQueryLoggingTest.java
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@Transactional
class NPlusOneQueriesLoggingTest {
@Autowired
private MessageRepository messageRepository;
@Test
void nPlusOneQueriesDetection_isLoggingWhenDetectingNPlusOneQueries() {
@yannbriancon
yannbriancon / NotificationResourceIntTest.java
Created Apr 26, 2020
Integration test asserting the number of queries generated
View NotificationResourceIntTest.java
...
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class NotificationResourceIntTest {
@Autowired
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor;
@yannbriancon
yannbriancon / pom.xml
Last active May 10, 2020
spring-hibernate-query-count dependency
View pom.xml
<dependency>
<groupId>com.yannbriancon</groupId>
<artifactId>spring-hibernate-query-utils</artifactId>
<version>1.0.3</version>
</dependency>
@yannbriancon
yannbriancon / NotificationService.java
Last active May 4, 2020
Service triggering N+1 queries
View NotificationService.java
void logMessages() {
// Get all the messages from the database
// -> Triggers 1 query
Set<Message> messages = messageDao.findAll();
// Map through the N messages to create the DTO with the author display name
// -> Triggers 1 query to fetch each author so N queries!
messages.stream.map(
message -> logger.info(
message.getAuthor().getName() + ": " + message.getText()
View Message.java
class Message {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private User author;
}
You can’t perform that action at this time.