Skip to content

Instantly share code, notes, and snippets.

View yannbriancon's full-sized avatar

Yann Briançon yannbriancon

View GitHub Profile
@yannbriancon
yannbriancon / pom.xml
Last active May 10, 2020 13:42
spring-hibernate-query-count dependency
<dependency>
<groupId>com.yannbriancon</groupId>
<artifactId>spring-hibernate-query-utils</artifactId>
<version>1.0.3</version>
</dependency>
@yannbriancon
yannbriancon / NPlusOneQueryLoggingTest.java
Last active May 8, 2020 14:44
Test triggering N+1 query
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@Transactional
class NPlusOneQueriesLoggingTest {
@Autowired
private MessageRepository messageRepository;
@Test
void nPlusOneQueriesDetection_isLoggingWhenDetectingNPlusOneQueries() {
@yannbriancon
yannbriancon / NPlusOneQueryLoggingTest.java
Last active May 8, 2020 14:43
Test changing interceptor configuration to ERROR
@RunWith(SpringRunner.class)
@SpringBootTest("hibernate.query.interceptor.error-level=ERROR")
@Transactional
class NPlusOneQueriesLoggingTest {
...
}
@yannbriancon
yannbriancon / UserRepository.java
Last active May 5, 2020 07:04
Eager Fetching With Result Limiting
@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 / NotificationService.java
Last active May 4, 2020 19:59
Service triggering N+1 queries
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()
@yannbriancon
yannbriancon / MessageRepository.java
Last active May 2, 2020 17:53
Criteria query with JoinType LEFT
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);
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020 17:47
JPQL with JOIN FETCH
@Query("SELECT *
FROM Message m
LEFT JOIN FETCH m.author")
List<Message> getAllBy();
@yannbriancon
yannbriancon / MessageRepository.java
Created May 2, 2020 17:46
JPA query with entity graph
@EntityGraph(attributePaths = {"author"})
List<Message> getAllBy();
@yannbriancon
yannbriancon / Message.java
Last active May 2, 2020 10:08
Message class
class Message {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private User author;
}
@yannbriancon
yannbriancon / NotificationResourceIntTest.java
Created April 26, 2020 10:07
Integration test asserting the number of queries generated
...
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class NotificationResourceIntTest {
@Autowired
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor;