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
@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
@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
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
@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); |
OlderNewer