<commit_before>package com.vladmihalcea.book.hpjp.hibernate.query;

import com.vladmihalcea.book.hpjp.util.AbstractOracleIntegrationTest;
import org.hibernate.Session;
import org.hibernate.query.*;
import org.junit.Test;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

import static org.junit.Assert.assertEquals;

/**
 * @author <NAME>
 */
public class OracleExecutionPlanNativeQueryTest extends AbstractOracleIntegrationTest {

    private static final int ENTITY_COUNT = 500;

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[]{
                Post.class,
                PostComment.class,
        };
    }

    @Override
    protected void additionalProperties(Properties properties) {
        properties.put("hibernate.jdbc.batch_size", "50");
        properties.put("hibernate.order_inserts", "true");
    }

    @Test
    public void testQueryExecutionPlan() {
        doInJPA(entityManager -> {
            for (int i = 0; i < ENTITY_COUNT; i++) {
                Post post = new Post(String.format("Post no. %d", i));

                post.addComment(new PostComment(String.format("Comment %d-1", i)));
                if (Math.random() < 0.1) {
                    post.addComment(new PostComment("Bingo"));
                }

                entityManager.persist(post);
            }
        });

        int pageStart = 20;
        int pageSize = 10;

        doInJPA(entityManager -> {

            List<Long> summaries = entityManager.createNativeQuery(
                "SELECT " +
                "     p.id " +
                "FROM " +
                "     post p " +
                "WHERE EXISTS ( " +
                "     SELECT 1 " +
                "     FROM " +
                "          post_comment pc " +
                "     WHERE " +
                "          pc.post_id = p.id AND " +
                "          pc.review = 'Bingo' " +
                ") " +
                "ORDER BY " +
                "     p.title ")
            .setFirstResult(pageStart)
            .setMaxResults(pageSize)
            .unwrap(org.hibernate.query.Query.class)
            .addQueryHint("+GATHER_PLAN_STATISTICS")
            .addQueryHint("POST_WITH_BINGO_COMMENTS")
            .getResultList();

            assertEquals(pageSize, summaries.size());

            List<String> executionPlanLines = entityManager.createNativeQuery(
                "SELECT p.* " +
                "FROM v$sql s, TABLE ( " +
                "  dbms_xplan.display_cursor ( " +
                "    s.sql_id, s.child_number, 'ALLSTATS LAST' " +
                "  ) " +
                ") p " +<commit_msg>Add minor changes<commit_after>package com.vladmihalcea.book.hpjp.hibernate.query;

import com.vladmihalcea.book.hpjp.util.AbstractOracleIntegrationTest;
import org.hibernate.Session;
import org.hibernate.query.*;
import org.junit.Test;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

import static org.junit.Assert.assertEquals;

/**
 * @author <NAME>
 */
public class OracleExecutionPlanNativeQueryTest extends AbstractOracleIntegrationTest {

    private static final int ENTITY_COUNT = 500;

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[]{
                Post.class,
                PostComment.class,
        };
    }

    @Override
    protected void additionalProperties(Properties properties) {
        properties.put("hibernate.jdbc.batch_size", "50");
        properties.put("hibernate.order_inserts", "true");
    }

    @Test
    public void testQueryExecutionPlan() {
        doInJPA(entityManager -> {
            for (int i = 0; i < ENTITY_COUNT; i++) {
                Post post = new Post(String.format("Post no. %d", i));

                post.addComment(new PostComment(String.format("Comment %d-1", i)));
                if (Math.random() < 0.1) {
                    post.addComment(new PostComment("Bingo"));
                }

                entityManager.persist(post);
            }
        });

        int pageStart = 20;
        int pageSize = 10;

        doInJPA(entityManager -> {

            List<Long> postIds = entityManager.createNativeQuery(
                "SELECT " +
                "     p.id " +
                "FROM " +
                "     post p " +
                "WHERE EXISTS ( " +
                "     SELECT 1 " +
                "     FROM " +
                "          post_comment pc " +
                "     WHERE " +
                "          pc.post_id = p.id AND " +
                "          pc.review = 'Bingo' " +
                ") " +
                "ORDER BY " +
                "     p.title ")
            .setFirstResult(pageStart)
            .setMaxResults(pageSize)
            .getResultList();

            assertEquals(pageSize, postIds.size());
        });

        doInJPA(entityManager -> {

            List<Long> summaries = entityManager.createNativeQuery(
                "SELECT " +
                "     p.id " +
                "FROM " +
                "     post p " +
                "WHERE EXISTS ( " +
                "     SELECT 1 " +
                "     FROM " +
                "          post_comment pc " +
                "     WHERE " +
                "          pc.post_id = p.id AND " +
                "          pc.review = 'Bingo' " +
                ") " +
                "ORDER BY " +
                "     p.title ")
            .setFirstResult(pageStart)
            .setMaxResults(pageSize)
            .unwrap(org.hibernate.query.Query.class)
            .addQueryHint("GATHER_PLAN_STATISTICS")
            .setComment("POST_WITH_BINGO_COMMENTS")
            .getResultList();

            List<String> executionPlanLines = entityManager.createNativeQuery(
                "SELECT p.* " +
                "FROM v$sql s, TABLE ( " +
                "  dbms_xplan.display_cursor ( " +
                "    s.sql_id, s.child_number, 'ALLSTATS LAST' " +
                "  ) " +
                ") p " +
{'fraction_non_alphanumeric': 0.11325570266390174, 'fraction_numerical': 0.004625937151060775, 'mean_word_length': 1.45113369820172, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4446995', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1697, 'n_words': 308}