Search in sources :

Example 6 with JoinedIterator

use of org.hibernate.internal.util.collections.JoinedIterator in project hibernate-orm by hibernate.

the class UnionSubclassEntityPersister method generateSubquery.

protected String generateSubquery(PersistentClass model, Mapping mapping) {
    Dialect dialect = getFactory().getDialect();
    Settings settings = getFactory().getSettings();
    if (!model.hasSubclasses()) {
        return model.getTable().getQualifiedName(dialect, settings.getDefaultCatalogName(), settings.getDefaultSchemaName());
    }
    HashSet columns = new LinkedHashSet();
    Iterator titer = model.getSubclassTableClosureIterator();
    while (titer.hasNext()) {
        Table table = (Table) titer.next();
        if (!table.isAbstractUnionTable()) {
            Iterator citer = table.getColumnIterator();
            while (citer.hasNext()) {
                columns.add(citer.next());
            }
        }
    }
    StringBuilder buf = new StringBuilder().append("( ");
    Iterator siter = new JoinedIterator(new SingletonIterator(model), model.getSubclassIterator());
    while (siter.hasNext()) {
        PersistentClass clazz = (PersistentClass) siter.next();
        Table table = clazz.getTable();
        if (!table.isAbstractUnionTable()) {
            //TODO: move to .sql package!!
            buf.append("select ");
            Iterator citer = columns.iterator();
            while (citer.hasNext()) {
                Column col = (Column) citer.next();
                if (!table.containsColumn(col)) {
                    int sqlType = col.getSqlTypeCode(mapping);
                    buf.append(dialect.getSelectClauseNullString(sqlType)).append(" as ");
                }
                buf.append(col.getQuotedName(dialect));
                buf.append(", ");
            }
            buf.append(clazz.getSubclassId()).append(" as clazz_");
            buf.append(" from ").append(table.getQualifiedName(dialect, settings.getDefaultCatalogName(), settings.getDefaultSchemaName()));
            buf.append(" union ");
            if (dialect.supportsUnionAll()) {
                buf.append("all ");
            }
        }
    }
    if (buf.length() > 2) {
        //chop the last union (all)
        buf.setLength(buf.length() - (dialect.supportsUnionAll() ? 11 : 7));
    }
    return buf.append(" )").toString();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Table(org.hibernate.mapping.Table) Column(org.hibernate.mapping.Column) SingletonIterator(org.hibernate.internal.util.collections.SingletonIterator) Dialect(org.hibernate.dialect.Dialect) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) SingletonIterator(org.hibernate.internal.util.collections.SingletonIterator) Settings(org.hibernate.cfg.Settings) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 7 with JoinedIterator

use of org.hibernate.internal.util.collections.JoinedIterator in project hibernate-orm by hibernate.

the class FooBarTest method testTransientOrphanDelete.

@Test
public void testTransientOrphanDelete() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Baz baz = new Baz();
    Set bars = new HashSet();
    baz.setCascadingBars(bars);
    bars.add(new Bar());
    bars.add(new Bar());
    bars.add(new Bar());
    List foos = new ArrayList();
    foos.add(new Foo());
    foos.add(new Foo());
    baz.setFooBag(foos);
    s.save(baz);
    Iterator i = new JoinedIterator(new Iterator[] { foos.iterator(), bars.iterator() });
    while (i.hasNext()) {
        FooComponent cmp = ((Foo) i.next()).getComponent();
        s.delete(cmp.getGlarch());
        cmp.setGlarch(null);
    }
    t.commit();
    s.close();
    bars.remove(bars.iterator().next());
    foos.remove(1);
    s = openSession();
    t = s.beginTransaction();
    s.update(baz);
    assertEquals(2, s.createQuery("From Bar bar").list().size());
    assertEquals(3, s.createQuery("From Foo foo").list().size());
    t.commit();
    s.close();
    foos.remove(0);
    s = openSession();
    t = s.beginTransaction();
    s.update(baz);
    bars.remove(bars.iterator().next());
    assertEquals(1, s.createQuery("From Foo foo").list().size());
    s.delete(baz);
    //s.flush();
    assertEquals(0, s.createQuery("From Foo foo").list().size());
    t.commit();
    s.close();
}
Also used : SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Session(org.hibernate.Session) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with JoinedIterator

use of org.hibernate.internal.util.collections.JoinedIterator in project hibernate-orm by hibernate.

the class HQLQueryPlan method performIterate.

/**
	 * Coordinates the efforts to perform an iterate across all the included query translators.
	 *
	 * @param queryParameters The query parameters
	 * @param session The session
	 *
	 * @return The query result iterator
	 *
	 * @throws HibernateException Indicates a problem performing the query
	 */
@SuppressWarnings("unchecked")
public Iterator performIterate(QueryParameters queryParameters, EventSource session) throws HibernateException {
    if (traceEnabled) {
        LOG.tracev("Iterate: {0}", getSourceQuery());
        queryParameters.traceParameters(session.getFactory());
    }
    if (translators.length == 0) {
        return EmptyIterator.INSTANCE;
    }
    final boolean many = translators.length > 1;
    Iterator[] results = null;
    if (many) {
        results = new Iterator[translators.length];
    }
    Iterator result = null;
    for (int i = 0; i < translators.length; i++) {
        result = translators[i].iterate(queryParameters, session);
        if (many) {
            results[i] = result;
        }
    }
    return many ? new JoinedIterator(results) : result;
}
Also used : JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) EmptyIterator(org.hibernate.internal.util.collections.EmptyIterator) Iterator(java.util.Iterator) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator)

Example 9 with JoinedIterator

use of org.hibernate.internal.util.collections.JoinedIterator in project hibernate-orm by hibernate.

the class DenormalizedTable method getIndexIterator.

@Override
public Iterator getIndexIterator() {
    List indexes = new ArrayList();
    Iterator iter = includedTable.getIndexIterator();
    while (iter.hasNext()) {
        Index parentIndex = (Index) iter.next();
        Index index = new Index();
        index.setName(getName() + parentIndex.getName());
        index.setTable(this);
        index.addColumns(parentIndex.getColumnIterator());
        indexes.add(index);
    }
    return new JoinedIterator(indexes.iterator(), super.getIndexIterator());
}
Also used : ArrayList(java.util.ArrayList) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator)

Example 10 with JoinedIterator

use of org.hibernate.internal.util.collections.JoinedIterator in project hibernate-orm by hibernate.

the class PersistentClass method getSubclassIterator.

/**
	 * Iterate over subclasses in a special 'order', most derived subclasses
	 * first.
	 */
public Iterator getSubclassIterator() {
    Iterator[] iters = new Iterator[subclasses.size() + 1];
    Iterator iter = subclasses.iterator();
    int i = 0;
    while (iter.hasNext()) {
        iters[i++] = ((Subclass) iter.next()).getSubclassIterator();
    }
    iters[i] = subclasses.iterator();
    return new JoinedIterator(iters);
}
Also used : JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) SingletonIterator(org.hibernate.internal.util.collections.SingletonIterator) EmptyIterator(org.hibernate.internal.util.collections.EmptyIterator) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator)

Aggregations

JoinedIterator (org.hibernate.internal.util.collections.JoinedIterator)11 Iterator (java.util.Iterator)8 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)3 EmptyIterator (org.hibernate.internal.util.collections.EmptyIterator)3 SingletonIterator (org.hibernate.internal.util.collections.SingletonIterator)3 List (java.util.List)2 Set (java.util.Set)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 Session (org.hibernate.Session)2 Test (org.junit.Test)2 LinkedHashSet (java.util.LinkedHashSet)1 Transaction (org.hibernate.Transaction)1 Settings (org.hibernate.cfg.Settings)1 Dialect (org.hibernate.dialect.Dialect)1 Column (org.hibernate.mapping.Column)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Table (org.hibernate.mapping.Table)1