Search in sources :

Example 16 with CollectionPersister

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

the class LoadQueryJoinAndFetchProcessor method renderManyToManyJoin.

private void renderManyToManyJoin(Join join, JoinFragment joinFragment) {
    // for many-to-many we have 3 table aliases.  By way of example, consider a normal m-n: User<->Role
    // where User is the FetchOwner and Role (User.roles) is the Fetch.  We'd have:
    //		1) the owner's table : user - in terms of rendering the joins (not the fetch select fragments), the
    // 			lhs table alias is only needed to qualify the lhs join columns, but we already have the qualified
    // 			columns here (aliasedLhsColumnNames)
    //final String ownerTableAlias = ...;
    //		2) the m-n table : user_role
    //		3) the element table : role
    final EntityPersister entityPersister = ((EntityQuerySpace) join.getRightHandSide()).getEntityPersister();
    final String entityTableAlias = aliasResolutionContext.resolveSqlTableAliasFromQuerySpaceUid(join.getRightHandSide().getUid());
    if (StringHelper.isEmpty(entityTableAlias)) {
        throw new IllegalStateException("Collection element (many-to-many) table alias cannot be empty");
    }
    final String manyToManyFilter;
    if (JoinDefinedByMetadata.class.isInstance(join) && CollectionPropertyNames.COLLECTION_ELEMENTS.equals(((JoinDefinedByMetadata) join).getJoinedPropertyName())) {
        final CollectionQuerySpace leftHandSide = (CollectionQuerySpace) join.getLeftHandSide();
        final CollectionPersister persister = leftHandSide.getCollectionPersister();
        manyToManyFilter = persister.getManyToManyFilterFragment(entityTableAlias, buildingParameters.getQueryInfluencers().getEnabledFilters());
    } else {
        manyToManyFilter = null;
    }
    addJoins(join, joinFragment, (Joinable) entityPersister, manyToManyFilter);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) JoinDefinedByMetadata(org.hibernate.loader.plan.spi.JoinDefinedByMetadata) EntityQuerySpace(org.hibernate.loader.plan.spi.EntityQuerySpace) CollectionQuerySpace(org.hibernate.loader.plan.spi.CollectionQuerySpace)

Example 17 with CollectionPersister

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

the class MapType method replaceElements.

@Override
public Object replaceElements(final Object original, final Object target, final Object owner, final java.util.Map copyCache, final SharedSessionContractImplementor session) throws HibernateException {
    CollectionPersister cp = session.getFactory().getMetamodel().collectionPersister(getRole());
    java.util.Map result = (java.util.Map) target;
    result.clear();
    for (Object o : ((Map) original).entrySet()) {
        Map.Entry me = (Map.Entry) o;
        Object key = cp.getIndexType().replace(me.getKey(), null, session, owner, copyCache);
        Object value = cp.getElementType().replace(me.getValue(), null, session, owner, copyCache);
        result.put(key, value);
    }
    return result;
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) Map(java.util.Map) PersistentMap(org.hibernate.collection.internal.PersistentMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister 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 19 with CollectionPersister

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

the class CollectionCacheEvictionTest method testCachedValueAfterEviction.

@Test
public void testCachedValueAfterEviction() {
    CollectionPersister persister = sessionFactory().getCollectionPersister(Company.class.getName() + ".users");
    Session session = openSession();
    SessionImplementor sessionImplementor = (SessionImplementor) session;
    CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
    Object key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    Object cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNull(cachedValue);
    Company company = session.get(Company.class, 1);
    //should add in cache
    assertEquals(1, company.getUsers().size());
    session.close();
    session = openSession();
    sessionImplementor = (SessionImplementor) session;
    key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
    cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
    assertNotNull("Collection wasn't cached", cachedValue);
    session.close();
}
Also used : CollectionPersister(org.hibernate.persister.collection.CollectionPersister) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) CollectionRegionAccessStrategy(org.hibernate.cache.spi.access.CollectionRegionAccessStrategy) Session(org.hibernate.Session) Test(org.junit.Test)

Example 20 with CollectionPersister

use of org.hibernate.persister.collection.CollectionPersister 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)

Aggregations

CollectionPersister (org.hibernate.persister.collection.CollectionPersister)44 HibernateException (org.hibernate.HibernateException)11 Type (org.hibernate.type.Type)9 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)8 Serializable (java.io.Serializable)7 CollectionRegionAccessStrategy (org.hibernate.cache.spi.access.CollectionRegionAccessStrategy)7 CollectionType (org.hibernate.type.CollectionType)7 HashMap (java.util.HashMap)6 Session (org.hibernate.Session)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Test (org.junit.Test)6 Map (java.util.Map)5 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)5 EntityEntry (org.hibernate.engine.spi.EntityEntry)5 CompositeType (org.hibernate.type.CompositeType)5 EntityType (org.hibernate.type.EntityType)5 ResultSet (java.sql.ResultSet)4 AssertionFailure (org.hibernate.AssertionFailure)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4