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
<dependency> | |
<groupId>com.yannbriancon</groupId> | |
<artifactId>spring-hibernate-query-utils</artifactId> | |
<version>1.0.3</version> | |
</dependency> |
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
@RunWith(MockitoJUnitRunner.class) | |
@SpringBootTest | |
@Transactional | |
class NPlusOneQueriesLoggingTest { | |
@Autowired | |
private MessageRepository messageRepository; | |
@Test | |
void nPlusOneQueriesDetection_isLoggingWhenDetectingNPlusOneQueries() { |
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
@RunWith(SpringRunner.class) | |
@SpringBootTest("hibernate.query.interceptor.error-level=ERROR") | |
@Transactional | |
class NPlusOneQueriesLoggingTest { | |
... | |
} |
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
@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); |
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
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() |
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
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); |
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
@Query("SELECT * | |
FROM Message m | |
LEFT JOIN FETCH m.author") | |
List<Message> getAllBy(); |
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
@EntityGraph(attributePaths = {"author"}) | |
List<Message> getAllBy(); |
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
class Message { | |
... | |
@ManyToOne(fetch = FetchType.LAZY) | |
@JoinColumn(name = "author_id") | |
private User author; | |
} |
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
... | |
import com.yannbriancon.interceptor.HibernateQueryCountInterceptor; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
@Transactional | |
public class NotificationResourceIntTest { | |
@Autowired | |
private HibernateQueryCountInterceptor hibernateQueryCountInterceptor; |
NewerOlder