@Override public Joinable getAssociatedJoinable(SessionFactoryImplementor factory) throws MappingException { return (Joinable) factory.getCollectionPersister( role ); }
private void copyCollectionPersisterKeys(Attribute embeddedAttr, PluralAttribute listAttr, EntityManager em) { String [] keys = createOriginalAndExpectedKeys(embeddedAttr, listAttr); try { SessionImpl session = (SessionImpl) em.getDelegate(); SessionFactoryImplementor sessionFactory = session.getSessionFactory(); CollectionPersister persister = sessionFactory.getCollectionPersister(keys[0]); sessionFactory.getCollectionPersisters().put(keys[1], persister); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public CollectionPersister getCollectionPersister(String role) throws MappingException { return delegate.getCollectionPersister( role ); }
@Override public String toString() { return "CollectionKey" + MessageHelper.collectionInfoString( factory.getCollectionPersister( role ), key, factory ); }
/** * Get the Hibernate type of the collection elements * * @param factory The session factory. * @return The type of the collection elements * @throws MappingException Indicates the underlying persister could not be located. */ public final Type getElementType(SessionFactoryImplementor factory) throws MappingException { return factory.getCollectionPersister( getRole() ).getElementType(); }
private CollectionPersister resolvePersister(CriteriaBuilderImpl criteriaBuilder, PluralAttribute attribute) { SessionFactoryImplementor sfi = criteriaBuilder.getEntityManagerFactory().getSessionFactory(); return sfi.getCollectionPersister( resolveRole( attribute ) ); }
private void addCollection(String role, String alias, Map propertyResults) { SQLLoadableCollection collectionPersister = ( SQLLoadableCollection ) factory.getCollectionPersister( role ); alias2CollectionPersister.put( alias, collectionPersister ); String suffix = generateCollectionSuffix(); LOG.tracev( "Mapping alias [{0}] to collection-suffix [{1}]", alias, suffix ); alias2CollectionSuffix.put( alias, suffix ); collectionPropertyResultMaps.put( alias, propertyResults ); if ( collectionPersister.isOneToMany() || collectionPersister.isManyToMany() ) { SQLLoadable persister = ( SQLLoadable ) collectionPersister.getElementPersister(); addPersister( alias, filter( propertyResults ), persister ); } }
public MapKeyAttribute(CriteriaBuilderImpl criteriaBuilder, MapAttribute<?, K, ?> attribute) { this.attribute = attribute; this.jpaType = attribute.getKeyType(); this.jpaBinableJavaType = attribute.getKeyJavaType(); this.jpaBindableType = Type.PersistenceType .ENTITY.equals( jpaType.getPersistenceType() ) ? BindableType.ENTITY_TYPE : BindableType.SINGULAR_ATTRIBUTE; String guessedRoleName = determineRole( attribute ); SessionFactoryImplementor sfi = criteriaBuilder.getEntityManagerFactory().getSessionFactory(); mapPersister = sfi.getCollectionPersister( guessedRoleName ); if ( mapPersister == null ) { throw new IllegalStateException( "Could not locate collection persister [" + guessedRoleName + "]" ); } mapKeyType = mapPersister.getIndexType(); if ( mapKeyType == null ) { throw new IllegalStateException( "Could not determine map-key type [" + guessedRoleName + "]" ); } this.persistentAttributeType = mapKeyType.isEntityType() ? PersistentAttributeType.MANY_TO_ONE : mapKeyType.isComponentType() ? PersistentAttributeType.EMBEDDED : PersistentAttributeType.BASIC; }
@Override public String getAssociatedEntityName(SessionFactoryImplementor factory) throws MappingException { try { QueryableCollection collectionPersister = (QueryableCollection) factory .getCollectionPersister( role ); if ( !collectionPersister.getElementType().isEntityType() ) { throw new MappingException( "collection was not an association: " + collectionPersister.getRole() ); } return collectionPersister.getElementPersister().getEntityName(); } catch (ClassCastException cce) { throw new MappingException( "collection role is not queryable " + role ); } }
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 ); } }
@Override public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException { final String entityName =criteriaQuery.getEntityName( criteria, propertyName ); final String role = entityName + '.' + criteriaQuery.getPropertyName( propertyName ); final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister( role ); final String[] fk = cp.getKeyColumnNames(); final String[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames(); final ConditionFragment subQueryRestriction = new ConditionFragment() .setTableAlias( criteriaQuery.getSQLAlias( criteria, propertyName ) ) .setCondition( pk, fk ); return String.format( Locale.ROOT, "? %s (select count(*) from %s where %s)", op, cp.getTableName(), subQueryRestriction.toFragmentString() ); }
/** * Reattach a detached (disassociated) initialized or uninitialized * collection wrapper, using a snapshot carried with the collection * wrapper */ protected void reattachCollection(PersistentCollection collection, CollectionType type) throws HibernateException { if ( collection.wasInitialized() ) { CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( type.getRole() ); getSession().getPersistenceContext() .addInitializedDetachedCollection( collectionPersister, collection ); } else { if ( !isCollectionSnapshotValid(collection) ) { throw new HibernateException( "could not reassociate uninitialized transient collection" ); } CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( collection.getRole() ); getSession().getPersistenceContext() .addUninitializedDetachedCollection( collectionPersister, collection ); } }
final Object anything, final CollectionType type) { final CollectionPersister persister = eventSource.getFactory().getCollectionPersister( type.getRole() ); final Type elemType = persister.getElementType();
protected void prepareTest() throws Exception { super.prepareTest(); isContractPartiesInverse = sessionFactory().getCollectionPersister( Contract.class.getName() + ".parties" ).isInverse(); try { sessionFactory().getEntityPersister( Party.class.getName() ).getPropertyType( "contract" ); isContractPartiesBidirectional = true; } catch ( QueryException ex) { isContractPartiesBidirectional = false; } try { sessionFactory().getEntityPersister( ContractVariation.class.getName() ).getPropertyType( "contract" ); isContractVariationsBidirectional = true; } catch ( QueryException ex) { isContractVariationsBidirectional = false; } isContractVersioned = sessionFactory().getEntityPersister( Contract.class.getName() ).isVersioned(); }
CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
@Test public void testCachedValueAfterEviction() { CollectionPersister persister = sessionFactory().getCollectionPersister( Company.class.getName() + ".users" ); Session session = openSession(); SessionImplementor sessionImplementor = (SessionImplementor) session; CollectionDataAccess cache = persister.getCacheAccessStrategy(); Object key = cache.generateCacheKey( 1, persister, sessionFactory(), session.getTenantIdentifier() ); Object cachedValue = cache.get( sessionImplementor, key ); 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 ); assertNotNull( "Collection wasn't cached", cachedValue ); session.close(); }
@Test @TestForIssue(jiraKey = "HHH-5732") public void testInverseIndex() { final CollectionPersister transactionsPersister = sessionFactory().getCollectionPersister( BankAccount.class.getName() + ".transactions" ); assertTrue( transactionsPersister.isInverse() );
final CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );
CollectionPersister persister = session.getFactory().getCollectionPersister( collectionType.getRole() );
@Test public void testCollectionInitializerCase() { CollectionPersister cp = sessionFactory().getCollectionPersister( Poster.class.getName() + ".messages" ); FetchStyleLoadPlanBuildingAssociationVisitationStrategy strategy = new FetchStyleLoadPlanBuildingAssociationVisitationStrategy( sessionFactory(), LoadQueryInfluencers.NONE, LockMode.NONE ); LoadPlan plan = MetamodelDrivenLoadPlanBuilder.buildRootCollectionLoadPlan( strategy, cp ); assertFalse( plan.hasAnyScalarReturns() ); assertEquals( 1, plan.getReturns().size() ); Return rtn = plan.getReturns().get( 0 ); CollectionReturn collectionReturn = ExtraAssertions.assertTyping( CollectionReturn.class, rtn ); assertNotNull( collectionReturn.getElementGraph() ); assertNotNull( collectionReturn.getElementGraph().getFetches() ); // the collection Message elements are fetched, but Message.poster is not fetched // (because that collection is owned by that Poster) assertEquals( 0, collectionReturn.getElementGraph().getFetches().length ); EntityReference entityReference = ExtraAssertions.assertTyping( EntityReference.class, collectionReturn.getElementGraph() ); assertNotNull( entityReference.getFetches() ); assertEquals( 0, entityReference.getFetches().length ); LoadPlanTreePrinter.INSTANCE.logTree( plan, new AliasResolutionContextImpl( sessionFactory() ) ); }