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());
}
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);
}
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();
}
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;
}
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();
}
Aggregations