Search in sources :

Example 1 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class AbstractEmptinessExpression method getQueryableCollection.

protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory) throws HibernateException {
    final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister(entityName);
    final Type type = ownerMapping.toType(propertyName);
    if (!type.isCollectionType()) {
        throw new MappingException("Property path [" + entityName + "." + propertyName + "] does not reference a collection");
    }
    final String role = ((CollectionType) type).getRole();
    try {
        return (QueryableCollection) factory.getCollectionPersister(role);
    } catch (ClassCastException cce) {
        throw new QueryException("collection role is not queryable: " + role);
    } catch (Exception e) {
        throw new QueryException("collection role not found: " + role);
    }
}
Also used : CollectionType(org.hibernate.type.CollectionType) Type(org.hibernate.type.Type) QueryException(org.hibernate.QueryException) CollectionType(org.hibernate.type.CollectionType) PropertyMapping(org.hibernate.persister.entity.PropertyMapping) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 2 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class OrderByTest method testInverseIndex.

@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseIndex() {
    final CollectionPersister transactionsPersister = sessionFactory().getCollectionPersister(BankAccount.class.getName() + ".transactions");
    assertTrue(transactionsPersister.isInverse());
    Session s = openSession();
    s.getTransaction().begin();
    BankAccount account = new BankAccount();
    account.addTransaction("zzzzz");
    account.addTransaction("aaaaa");
    account.addTransaction("mmmmm");
    s.save(account);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    try {
        final QueryableCollection queryableCollection = (QueryableCollection) transactionsPersister;
        SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("code").addColumn("transactions_index");
        PreparedStatement preparedStatement = ((SessionImplementor) s).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
        ResultSet resultSet = ((SessionImplementor) s).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
        Map<Integer, String> valueMap = new HashMap<Integer, String>();
        while (resultSet.next()) {
            final String code = resultSet.getString(1);
            assertFalse("code column was null", resultSet.wasNull());
            final int indx = resultSet.getInt(2);
            assertFalse("List index column was null", resultSet.wasNull());
            valueMap.put(indx, code);
        }
        assertEquals(3, valueMap.size());
        assertEquals("zzzzz", valueMap.get(0));
        assertEquals("aaaaa", valueMap.get(1));
        assertEquals("mmmmm", valueMap.get(2));
    } catch (SQLException e) {
        fail(e.getMessage());
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PreparedStatement(java.sql.PreparedStatement) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SimpleSelect(org.hibernate.sql.SimpleSelect) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class PersistentListTest method testInverseListIndex.

@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseListIndex() {
    // make sure no one changes the mapping
    final CollectionPersister collectionPersister = sessionFactory().getCollectionPersister(ListOwner.class.getName() + ".children");
    assertTrue(collectionPersister.isInverse());
    // do some creations...
    Session session = openSession();
    session.beginTransaction();
    ListOwner root = new ListOwner("root");
    ListOwner child1 = new ListOwner("c1");
    root.getChildren().add(child1);
    child1.setParent(root);
    ListOwner child2 = new ListOwner("c2");
    root.getChildren().add(child2);
    child2.setParent(root);
    session.save(root);
    session.getTransaction().commit();
    session.close();
    // now, make sure the list-index column gotten written...
    final Session session2 = openSession();
    session2.beginTransaction();
    session2.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final QueryableCollection queryableCollection = (QueryableCollection) collectionPersister;
            SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("NAME").addColumn("LIST_INDEX").addCondition("NAME", "<>", "?");
            PreparedStatement preparedStatement = ((SessionImplementor) session2).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
            preparedStatement.setString(1, "root");
            ResultSet resultSet = ((SessionImplementor) session2).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
            Map<String, Integer> valueMap = new HashMap<String, Integer>();
            while (resultSet.next()) {
                final String name = resultSet.getString(1);
                assertFalse("NAME column was null", resultSet.wasNull());
                final int position = resultSet.getInt(2);
                assertFalse("LIST_INDEX column was null", resultSet.wasNull());
                valueMap.put(name, position);
            }
            assertEquals(2, valueMap.size());
            // c1 should be list index 0
            assertEquals(Integer.valueOf(0), valueMap.get("c1"));
            // c2 should be list index 1
            assertEquals(Integer.valueOf(1), valueMap.get("c2"));
        }
    });
    session2.delete(root);
    session2.getTransaction().commit();
    session2.close();
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) PreparedStatement(java.sql.PreparedStatement) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SimpleSelect(org.hibernate.sql.SimpleSelect) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class MethodNode method handleElements.

private void handleElements(FromReferenceNode collectionNode, String propertyName) {
    FromElement collectionFromElement = collectionNode.getFromElement();
    QueryableCollection queryableCollection = collectionFromElement.getQueryableCollection();
    String path = collectionNode.getPath() + "[]." + propertyName;
    LOG.debugf("Creating elements for %s", path);
    fromElement = collectionFromElement;
    if (!collectionFromElement.isCollectionOfValuesOrComponents()) {
        getWalker().addQuerySpaces(queryableCollection.getElementPersister().getQuerySpaces());
    }
    setDataType(queryableCollection.getElementType());
    selectColumns = collectionFromElement.toColumns(fromElement.getTableAlias(), propertyName, inSelect);
}
Also used : QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Example 5 with QueryableCollection

use of org.hibernate.persister.collection.QueryableCollection in project hibernate-orm by hibernate.

the class AttributeNodeImpl method internalMakeKeySubgraph.

@SuppressWarnings("unchecked")
private <X> SubgraphImpl<X> internalMakeKeySubgraph(Class<X> type) {
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException(String.format("Non-collection attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final PluralAttributeImpl pluralAttribute = (PluralAttributeImpl) attribute;
    if (pluralAttribute.getCollectionType() != PluralAttribute.CollectionType.MAP) {
        throw new IllegalArgumentException(String.format("Non-Map attribute [%s] cannot be target of key subgraph", getAttributeName()));
    }
    final AssociationType attributeType = (AssociationType) Helper.resolveType(sessionFactory(), attribute);
    final QueryableCollection collectionPersister = (QueryableCollection) attributeType.getAssociatedJoinable(sessionFactory());
    final Type indexType = collectionPersister.getIndexType();
    if (!indexType.isAssociationType()) {
        throw new IllegalArgumentException(String.format("Map index [%s] is not an entity; cannot be target of key subgraph", getAttributeName()));
    }
    if (keySubgraphMap == null) {
        keySubgraphMap = new HashMap<>();
    }
    final AssociationType indexAssociationType = (AssociationType) indexType;
    final EntityPersister indexEntityPersister = (EntityPersister) indexAssociationType.getAssociatedJoinable(sessionFactory());
    if (type == null) {
        type = indexEntityPersister.getMappedClass();
    } else {
        if (!isTreatableAs(indexEntityPersister, type)) {
            throw new IllegalArgumentException(String.format("Map key [%s] cannot be treated as requested type [%s] : %s", getAttributeName(), type.getName(), indexEntityPersister.getMappedClass().getName()));
        }
    }
    final SubgraphImpl<X> subgraph = new SubgraphImpl<>(this.sessionFactory, attribute.getDeclaringType(), type);
    keySubgraphMap.put(type, subgraph);
    return subgraph;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ManagedType(javax.persistence.metamodel.ManagedType) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) AssociationType(org.hibernate.type.AssociationType) PluralAttributeImpl(org.hibernate.metamodel.internal.PluralAttributeImpl) QueryableCollection(org.hibernate.persister.collection.QueryableCollection)

Aggregations

QueryableCollection (org.hibernate.persister.collection.QueryableCollection)33 Type (org.hibernate.type.Type)10 QueryException (org.hibernate.QueryException)9 AssociationType (org.hibernate.type.AssociationType)7 JoinSequence (org.hibernate.engine.internal.JoinSequence)6 Joinable (org.hibernate.persister.entity.Joinable)6 JoinType (org.hibernate.sql.JoinType)6 CollectionType (org.hibernate.type.CollectionType)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)5 HashMap (java.util.HashMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 OuterJoinLoadable (org.hibernate.persister.entity.OuterJoinLoadable)4 EntityType (org.hibernate.type.EntityType)4 SemanticException (antlr.SemanticException)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 Map (java.util.Map)3 Session (org.hibernate.Session)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3