Search in sources :

Example 21 with HQLQueryPlan

use of org.hibernate.engine.query.spi.HQLQueryPlan in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateHQLQueryPlan.

@Test
public void testCreateHQLQueryPlan() {
    final Collection<PersistentClass> entityBindings = new ArrayList<PersistentClass>();
    final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
    standardServiceRegistryBuilder.applySetting(AvailableSettings.DIALECT, MockDialect.class.getName());
    standardServiceRegistryBuilder.applySetting(AvailableSettings.CONNECTION_PROVIDER, MockConnectionProvider.class.getName());
    final StandardServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    Table t = new Table("FOO");
    Column c = new Column("foo");
    t.addColumn(c);
    PrimaryKey key = new PrimaryKey(t);
    key.addColumn(c);
    t.setPrimaryKey(key);
    @SuppressWarnings("deprecation") SimpleValue sv = new SimpleValue(metadata);
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.setTable(t);
    sv.addColumn(c);
    final RootClass rc = new RootClass(null);
    rc.setEntityName("foo");
    rc.setJpaEntityName("foo");
    rc.setIdentifier(sv);
    rc.setTable(t);
    rc.setOptimisticLockStyle(OptimisticLockStyle.NONE);
    entityBindings.add(rc);
    MetadataImplementor wrapper = (MetadataImplementor) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { MetadataImplementor.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("getEntityBinding".equals(method.getName()) && args != null && args.length == 1 && "foo".equals(args[0])) {
                return rc;
            } else if ("getEntityBindings".equals(method.getName())) {
                return entityBindings;
            }
            return method.invoke(metadata, args);
        }
    });
    MetadataBuildingOptions mbo = new MetadataBuilderImpl.MetadataBuildingOptionsImpl(serviceRegistry);
    BootstrapContext bc = new BootstrapContextImpl(serviceRegistry, mbo);
    SessionFactoryImpl sfi = new SessionFactoryImpl(wrapper, new SessionFactoryOptionsBuilder(serviceRegistry, bc), HQLQueryPlan::new);
    Map<String, Filter> filters = Collections.emptyMap();
    HQLQueryPlan hqlQueryPlan = new HQLQueryPlan("from foo", false, filters, sfi);
    IHQLQueryPlan facade = facadeFactory.createHQLQueryPlan(hqlQueryPlan);
    assertSame(hqlQueryPlan, ((IFacade) facade).getTarget());
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ArrayList(java.util.ArrayList) MockDialect(org.jboss.tools.hibernate.runtime.v_5_6.internal.util.MockDialect) MetadataSources(org.hibernate.boot.MetadataSources) PrimaryKey(org.hibernate.mapping.PrimaryKey) IPrimaryKey(org.jboss.tools.hibernate.runtime.spi.IPrimaryKey) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) BootstrapContext(org.hibernate.boot.spi.BootstrapContext) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) MetadataBuildingOptions(org.hibernate.boot.spi.MetadataBuildingOptions) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) Column(org.hibernate.mapping.Column) PersistentClass(org.hibernate.mapping.PersistentClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) MockConnectionProvider(org.jboss.tools.hibernate.runtime.v_5_6.internal.util.MockConnectionProvider) RootClass(org.hibernate.mapping.RootClass) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) Table(org.hibernate.mapping.Table) SessionFactoryOptionsBuilder(org.hibernate.boot.internal.SessionFactoryOptionsBuilder) Method(java.lang.reflect.Method) BootstrapContextImpl(org.hibernate.boot.internal.BootstrapContextImpl) InvocationHandler(java.lang.reflect.InvocationHandler) SimpleValue(org.hibernate.mapping.SimpleValue) HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) ITableFilter(org.jboss.tools.hibernate.runtime.spi.ITableFilter) TableFilter(org.hibernate.cfg.reveng.TableFilter) Filter(org.hibernate.Filter) PersistentClass(org.hibernate.mapping.PersistentClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) Test(org.junit.jupiter.api.Test)

Example 22 with HQLQueryPlan

use of org.hibernate.engine.query.spi.HQLQueryPlan in project jbosstools-hibernate by jbosstools.

the class ServiceImplTest method testNewHQLQueryPlan.

@Test
public void testNewHQLQueryPlan() throws Exception {
    IConfiguration configuration = service.newDefaultConfiguration();
    configuration.setProperty(AvailableSettings.DIALECT, MockDialect.class.getName());
    configuration.setProperty(AvailableSettings.CONNECTION_PROVIDER, MockConnectionProvider.class.getName());
    File testFile = File.createTempFile("test", "tmp");
    testFile.deleteOnExit();
    FileWriter fileWriter = new FileWriter(testFile);
    fileWriter.write(TEST_HBM_STRING);
    fileWriter.close();
    configuration.addFile(testFile);
    ISessionFactory sfi = configuration.buildSessionFactory();
    IHQLQueryPlan queryPlan = service.newHQLQueryPlan("from ServiceImplTest$Foo", true, sfi);
    assertNotNull(queryPlan);
    Object target = ((IFacade) queryPlan).getTarget();
    assertNotNull(target);
    assertTrue(target instanceof HQLQueryPlan);
}
Also used : MockConnectionProvider(org.jboss.tools.hibernate.runtime.v_5_6.internal.util.MockConnectionProvider) HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) FileWriter(java.io.FileWriter) MockDialect(org.jboss.tools.hibernate.runtime.v_5_6.internal.util.MockDialect) ISessionFactory(org.jboss.tools.hibernate.runtime.spi.ISessionFactory) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) IFacade(org.jboss.tools.hibernate.runtime.common.IFacade) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 23 with HQLQueryPlan

use of org.hibernate.engine.query.spi.HQLQueryPlan in project hibernate-orm by hibernate.

the class CompositeIdTest method testDistinctCountOfEntityWithCompositeId.

@Test
@SkipForDialect(value = Oracle8iDialect.class, comment = "Cannot count distinct over multiple columns in Oracle")
@SkipForDialect(value = SQLServerDialect.class, comment = "Cannot count distinct over multiple columns in SQL Server")
public void testDistinctCountOfEntityWithCompositeId() {
    // today we do not account for Dialects supportsTupleDistinctCounts() is false.  though really the only
    // "option" there is to throw an error.
    final HQLQueryPlan plan = sessionFactory().getQueryPlanCache().getHQLQueryPlan("select count(distinct o) from Order o", false, Collections.EMPTY_MAP);
    assertEquals(1, plan.getTranslators().length);
    final QueryTranslator translator = plan.getTranslators()[0];
    final String generatedSql = translator.getSQLString();
    System.out.println("Generated SQL : " + generatedSql);
    final int countExpressionListStart = generatedSql.indexOf("count(");
    final int countExpressionListEnd = generatedSql.indexOf(")", countExpressionListStart);
    final String countExpressionFragment = generatedSql.substring(countExpressionListStart + 6, countExpressionListEnd + 1);
    assertTrue(countExpressionFragment.startsWith("distinct"));
    assertTrue(countExpressionFragment.contains(","));
    Session s = openSession();
    s.beginTransaction();
    Customer c = new Customer();
    c.setCustomerId("1");
    c.setAddress("123 somewhere");
    c.setName("Brett");
    Order o1 = new Order(c);
    o1.setOrderDate(Calendar.getInstance());
    Order o2 = new Order(c);
    o2.setOrderDate(Calendar.getInstance());
    s.persist(c);
    s.persist(o1);
    s.persist(o2);
    s.getTransaction().commit();
    s.clear();
    s.beginTransaction();
    try {
        long count = (Long) s.createQuery("select count(distinct o) FROM Order o").uniqueResult();
        if (!getDialect().supportsTupleDistinctCounts()) {
            fail("expected SQLGrammarException");
        }
        assertEquals(2l, count);
    } catch (SQLGrammarException e) {
        if (getDialect().supportsTupleDistinctCounts()) {
            throw e;
        }
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.createQuery("delete from Order").executeUpdate();
    s.createQuery("delete from Customer").executeUpdate();
    s.getTransaction().commit();
    s.close();
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) SQLGrammarException(org.hibernate.exception.SQLGrammarException) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 24 with HQLQueryPlan

use of org.hibernate.engine.query.spi.HQLQueryPlan in project hibernate-orm by hibernate.

the class SessionImpl method list.

@Override
public List list(String query, QueryParameters queryParameters) throws HibernateException {
    checkOpenOrWaitingForAutoClose();
    checkTransactionSynchStatus();
    queryParameters.validateParameters();
    HQLQueryPlan plan = queryParameters.getQueryPlan();
    if (plan == null) {
        plan = getQueryPlan(query, false);
    }
    autoFlushIfRequired(plan.getQuerySpaces());
    List results = Collections.EMPTY_LIST;
    boolean success = false;
    // stops flush being called multiple times if this method is recursively called
    dontFlushFromFind++;
    try {
        results = plan.performList(queryParameters, this);
        success = true;
    } finally {
        dontFlushFromFind--;
        afterOperation(success);
        delayedAfterCompletion();
    }
    return results;
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) List(java.util.List)

Example 25 with HQLQueryPlan

use of org.hibernate.engine.query.spi.HQLQueryPlan in project hibernate-orm by hibernate.

the class GetHqlQueryPlanTest method testHqlQueryPlan.

@Test
public void testHqlQueryPlan() {
    Session s = openSession();
    QueryPlanCache cache = ((SessionImplementor) s).getFactory().getQueryPlanCache();
    assertTrue(getEnabledFilters(s).isEmpty());
    HQLQueryPlan plan1 = cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s));
    HQLQueryPlan plan2 = cache.getHQLQueryPlan("from Person where name is null", false, getEnabledFilters(s));
    HQLQueryPlan plan3 = cache.getHQLQueryPlan("from Person where name = :name", false, getEnabledFilters(s));
    HQLQueryPlan plan4 = cache.getHQLQueryPlan("from Person where name = ?1", false, getEnabledFilters(s));
    assertNotSame(plan1, plan2);
    assertNotSame(plan1, plan3);
    assertNotSame(plan1, plan4);
    assertNotSame(plan2, plan3);
    assertNotSame(plan2, plan4);
    assertNotSame(plan3, plan4);
    assertSame(plan1, cache.getHQLQueryPlan("from Person", false, getEnabledFilters(s)));
    assertSame(plan2, cache.getHQLQueryPlan("from Person where name is null", false, getEnabledFilters(s)));
    assertSame(plan3, cache.getHQLQueryPlan("from Person where name = :name", false, getEnabledFilters(s)));
    assertSame(plan4, cache.getHQLQueryPlan("from Person where name = ?1", false, getEnabledFilters(s)));
    s.close();
}
Also used : HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) QueryPlanCache(org.hibernate.engine.query.spi.QueryPlanCache) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

HQLQueryPlan (org.hibernate.engine.query.spi.HQLQueryPlan)46 IHQLQueryPlan (org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan)27 Filter (org.hibernate.Filter)18 TableFilter (org.hibernate.cfg.reveng.TableFilter)18 ITableFilter (org.jboss.tools.hibernate.runtime.spi.ITableFilter)18 Test (org.junit.jupiter.api.Test)18 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)16 IFacade (org.jboss.tools.hibernate.runtime.common.IFacade)11 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)11 File (java.io.File)9 FileWriter (java.io.FileWriter)9 Column (org.hibernate.mapping.Column)9 PrimaryKey (org.hibernate.mapping.PrimaryKey)9 RootClass (org.hibernate.mapping.RootClass)9 SimpleValue (org.hibernate.mapping.SimpleValue)9 Table (org.hibernate.mapping.Table)9 IColumn (org.jboss.tools.hibernate.runtime.spi.IColumn)9 IPrimaryKey (org.jboss.tools.hibernate.runtime.spi.IPrimaryKey)9 ISessionFactory (org.jboss.tools.hibernate.runtime.spi.ISessionFactory)9 ITable (org.jboss.tools.hibernate.runtime.spi.ITable)9