Search in sources :

Example 6 with JiraKey

use of org.hibernate.testing.orm.junit.JiraKey in project hibernate-orm by hibernate.

the class NotFoundIgnoreManyToOneTest method testQueryOwnerSelection.

@Test
@JiraKey("HHH-15060")
public void testQueryOwnerSelection(SessionFactoryScope scope) {
    final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
    statementInspector.clear();
    scope.inTransaction((session) -> {
        final String hql = "select c from Coin c";
        final List<Coin> coins = session.createSelectionQuery(hql, Coin.class).getResultList();
        assertThat(coins).hasSize(1);
        assertThat(coins.get(0).getCurrency()).isNull();
        // at the moment this uses a subsequent-select.  on the bright side, it is at least eagerly fetched.
        assertThat(statementInspector.getSqlQueries()).hasSize(2);
        assertThat(statementInspector.getSqlQueries().get(0)).contains(" from Coin ");
        assertThat(statementInspector.getSqlQueries().get(1)).contains(" from Currency ");
    // but I believe a jon would be better
    // assertThat( statementInspector.getSqlQueries() ).hasSize( 1 );
    // assertThat( statementInspector.getSqlQueries().get( 0 ) ).contains( " join " );
    // assertThat( statementInspector.getSqlQueries().get( 0 ) ).doesNotContain( " inner " );
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 7 with JiraKey

use of org.hibernate.testing.orm.junit.JiraKey in project hibernate-orm by hibernate.

the class NotFoundIgnoreManyToOneTest method testGet.

@Test
@JiraKey("HHH-15060")
public void testGet(SessionFactoryScope scope) {
    final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
    statementInspector.clear();
    scope.inTransaction((session) -> {
        final Coin coin = session.get(Coin.class, 1);
        assertThat(coin.getCurrency()).isNull();
        // technically we could use a subsequent-select rather than a join...
        assertThat(statementInspector.getSqlQueries()).hasSize(1);
        assertThat(statementInspector.getSqlQueries().get(0)).contains(" join ");
        assertThat(statementInspector.getSqlQueries().get(0)).doesNotContain(" inner ");
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 8 with JiraKey

use of org.hibernate.testing.orm.junit.JiraKey in project hibernate-orm by hibernate.

the class NotFoundExceptionManyToOneTest method testGet.

@Test
@JiraKey("HHH-15060")
@FailureExpected(reason = "ObjectNotFoundException is thrown but caught and null is returned - see " + "org.hibernate.internal.SessionImpl.IdentifierLoadAccessImpl#doLoad")
public void testGet(SessionFactoryScope scope) {
    final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
    statementInspector.clear();
    scope.inTransaction((session) -> {
        try {
            // should fail here loading the Coin due to missing currency (see NOTE#1)
            session.get(Coin.class, 1);
            fail("Expecting ObjectNotFoundException for broken fk");
        } catch (ObjectNotFoundException expected) {
            // technically we could use a subsequent-select rather than a join...
            assertThat(statementInspector.getSqlQueries()).hasSize(1);
            assertThat(statementInspector.getSqlQueries().get(0)).contains(" join ");
            assertThat(statementInspector.getSqlQueries().get(0)).doesNotContain(" inner ");
            assertThat(expected.getEntityName()).isEqualTo(Currency.class.getName());
            assertThat(expected.getIdentifier()).isEqualTo(1);
        }
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 9 with JiraKey

use of org.hibernate.testing.orm.junit.JiraKey in project hibernate-orm by hibernate.

the class NotFoundExceptionManyToOneTest method testQueryImplicitPathDereferencePredicate.

@Test
@JiraKey("HHH-15060")
@FailureExpected(reason = "EntityNotFoundException thrown rather than ObjectNotFoundException; " + "ObjectNotFoundException is thrown but caught and then converted to EntityNotFoundException")
public void testQueryImplicitPathDereferencePredicate(SessionFactoryScope scope) {
    final SQLStatementInspector statementInspector = scope.getCollectingStatementInspector();
    statementInspector.clear();
    scope.inTransaction((session) -> {
        final String hql = "select c from Coin c where c.currency.id = 1";
        try {
            session.createQuery(hql, Coin.class).getResultList();
            fail("Expecting ObjectNotFoundException for broken fk");
        } catch (ObjectNotFoundException expected) {
            assertThat(statementInspector.getSqlQueries()).hasSize(1);
            assertThat(statementInspector.getSqlQueries().get(0)).contains(" join ");
            assertThat(statementInspector.getSqlQueries().get(0)).doesNotContain(" inner ");
            assertThat(expected.getEntityName()).isEqualTo(Currency.class.getName());
            assertThat(expected.getIdentifier()).isEqualTo(1);
        }
    });
}
Also used : SQLStatementInspector(org.hibernate.testing.jdbc.SQLStatementInspector) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Test(org.junit.jupiter.api.Test) FailureExpected(org.hibernate.testing.orm.junit.FailureExpected) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Example 10 with JiraKey

use of org.hibernate.testing.orm.junit.JiraKey in project hibernate-orm by hibernate.

the class CorrelatedSubqueryTest method testCorrelationExplicitSelectionCorrelation.

@Test
@JiraKey("HHH-3032")
@SkipForDialect(dialectClass = SybaseASEDialect.class, majorVersion = 15)
public void testCorrelationExplicitSelectionCorrelation() {
    CriteriaBuilder builder = entityManagerFactory().getCriteriaBuilder();
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    CriteriaQuery<Customer> customerCriteria = builder.createQuery(Customer.class);
    Root<Customer> customer = customerCriteria.from(Customer.class);
    Join<Customer, Order> o = customer.join(Customer_.orders);
    Subquery<Order> sq = customerCriteria.subquery(Order.class);
    Join<Customer, Order> sqo = sq.correlate(o);
    Join<Order, LineItem> sql = sqo.join(Order_.lineItems);
    sq.where(builder.gt(sql.get(LineItem_.quantity), 3));
    // use the correlation itself as the subquery selection (initially caused problems wrt aliases)
    sq.select(sqo);
    customerCriteria.select(customer).distinct(true);
    customerCriteria.where(builder.exists(sq));
    em.createQuery(customerCriteria).getResultList();
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Order(org.hibernate.orm.test.jpa.metamodel.Order) EntityManager(jakarta.persistence.EntityManager) Customer(org.hibernate.orm.test.jpa.metamodel.Customer) LineItem(org.hibernate.orm.test.jpa.metamodel.LineItem) SkipForDialect(org.hibernate.testing.orm.junit.SkipForDialect) AbstractMetamodelSpecificTest(org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest) Test(org.junit.jupiter.api.Test) JiraKey(org.hibernate.testing.orm.junit.JiraKey)

Aggregations

JiraKey (org.hibernate.testing.orm.junit.JiraKey)20 Test (org.junit.jupiter.api.Test)20 SQLStatementInspector (org.hibernate.testing.jdbc.SQLStatementInspector)10 FailureExpected (org.hibernate.testing.orm.junit.FailureExpected)6 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)5 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)5 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)3 EntityManager (jakarta.persistence.EntityManager)2 AbstractMetamodelSpecificTest (org.hibernate.orm.test.jpa.metamodel.AbstractMetamodelSpecificTest)2 Order (org.hibernate.orm.test.jpa.metamodel.Order)2 SkipForDialect (org.hibernate.testing.orm.junit.SkipForDialect)2 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)1 Predicate (jakarta.persistence.criteria.Predicate)1 ScrollableResults (org.hibernate.ScrollableResults)1 AbstractHANADialect (org.hibernate.dialect.AbstractHANADialect)1 Customer (org.hibernate.orm.test.jpa.metamodel.Customer)1 LineItem (org.hibernate.orm.test.jpa.metamodel.LineItem)1 ProcedureCall (org.hibernate.procedure.ProcedureCall)1 Query (org.hibernate.query.Query)1 Output (org.hibernate.result.Output)1