Search in sources :

Example 71 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class SessionImpl method tryNaturalIdLoadAccess.

/**
	 * Checks to see if the CriteriaImpl is a naturalId lookup that can be done via
	 * NaturalIdLoadAccess
	 *
	 * @param criteria The criteria to check as a complete natural identifier lookup.
	 *
	 * @return A fully configured NaturalIdLoadAccess or null, if null is returned the standard CriteriaImpl execution
	 * should be performed
	 */
private NaturalIdLoadAccess tryNaturalIdLoadAccess(CriteriaImpl criteria) {
    // See if the criteria lookup is by naturalId
    if (!criteria.isLookupByNaturalKey()) {
        return null;
    }
    final String entityName = criteria.getEntityOrClassName();
    final EntityPersister entityPersister = getFactory().getMetamodel().entityPersister(entityName);
    // queries did no natural id validation
    if (!entityPersister.hasNaturalIdentifier()) {
        return null;
    }
    // Since isLookupByNaturalKey is true there can be only one CriterionEntry and getCriterion() will
    // return an instanceof NaturalIdentifier
    final CriterionEntry criterionEntry = criteria.iterateExpressionEntries().next();
    final NaturalIdentifier naturalIdentifier = (NaturalIdentifier) criterionEntry.getCriterion();
    final Map<String, Object> naturalIdValues = naturalIdentifier.getNaturalIdValues();
    final int[] naturalIdentifierProperties = entityPersister.getNaturalIdentifierProperties();
    // Verify the NaturalIdentifier criterion includes all naturalId properties, first check that the property counts match
    if (naturalIdentifierProperties.length != naturalIdValues.size()) {
        return null;
    }
    final String[] propertyNames = entityPersister.getPropertyNames();
    final NaturalIdLoadAccess naturalIdLoader = this.byNaturalId(entityName);
    // Build NaturalIdLoadAccess and in the process verify all naturalId properties were specified
    for (int naturalIdentifierProperty : naturalIdentifierProperties) {
        final String naturalIdProperty = propertyNames[naturalIdentifierProperty];
        final Object naturalIdValue = naturalIdValues.get(naturalIdProperty);
        if (naturalIdValue == null) {
            // A NaturalId property is missing from the critera query, can't use NaturalIdLoadAccess
            return null;
        }
        naturalIdLoader.using(naturalIdProperty, naturalIdValue);
    }
    // Criteria query contains a valid naturalId, use the new API
    log.warn("Session.byNaturalId(" + entityName + ") should be used for naturalId queries instead of Restrictions.naturalId() from a Criteria");
    return naturalIdLoader;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) SimpleNaturalIdLoadAccess(org.hibernate.SimpleNaturalIdLoadAccess) CriterionEntry(org.hibernate.internal.CriteriaImpl.CriterionEntry) NaturalIdentifier(org.hibernate.criterion.NaturalIdentifier)

Example 72 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class JoinWalker method isJoinedFetchEnabledInMapping.

/**
	 * Does the mapping, and Hibernate default semantics, specify that
	 * this association should be fetched by outer joining
	 */
protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type) throws MappingException {
    if (!type.isEntityType() && !type.isCollectionType()) {
        return false;
    } else {
        if (config == FetchMode.JOIN) {
            return true;
        }
        if (config == FetchMode.SELECT) {
            return false;
        }
        if (type.isEntityType()) {
            //TODO: look at the owning property and check that it 
            //      isn't lazy (by instrumentation)
            EntityType entityType = (EntityType) type;
            EntityPersister persister = getFactory().getEntityPersister(entityType.getAssociatedEntityName());
            return !persister.hasProxy();
        } else {
            return false;
        }
    }
}
Also used : EntityType(org.hibernate.type.EntityType) EntityPersister(org.hibernate.persister.entity.EntityPersister)

Example 73 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class JpaDeleteEventListener method performDetachedEntityDeletionCheck.

@Override
protected void performDetachedEntityDeletionCheck(DeleteEvent event) {
    EventSource source = event.getSession();
    String entityName = event.getEntityName();
    EntityPersister persister = source.getEntityPersister(entityName, event.getObject());
    Serializable id = persister.getIdentifier(event.getObject(), source);
    entityName = entityName == null ? source.guessEntityName(event.getObject()) : entityName;
    throw new IllegalArgumentException("Removing a detached instance " + entityName + "#" + id);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable)

Example 74 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister 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)

Example 75 with EntityPersister

use of org.hibernate.persister.entity.EntityPersister in project hibernate-orm by hibernate.

the class PersistenceUnitUtilImpl method getIdentifierFromPersister.

private Object getIdentifierFromPersister(Object entity) {
    Class<?> entityClass = Hibernate.getClass(entity);
    EntityPersister persister = sessionFactory.getMetamodel().entityPersister(entityClass);
    if (persister == null) {
        throw new IllegalArgumentException(entityClass.getName() + " is not an entity");
    }
    return persister.getIdentifier(entity, null);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister)

Aggregations

EntityPersister (org.hibernate.persister.entity.EntityPersister)166 Test (org.junit.Test)59 Serializable (java.io.Serializable)34 Session (org.hibernate.Session)31 Type (org.hibernate.type.Type)29 EntityEntry (org.hibernate.engine.spi.EntityEntry)21 EventSource (org.hibernate.event.spi.EventSource)15 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)13 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 TestForIssue (org.hibernate.testing.TestForIssue)13 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)12 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)12 EntityType (org.hibernate.type.EntityType)12 ArrayList (java.util.ArrayList)11 CompositeType (org.hibernate.type.CompositeType)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)10 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 EntityKey (org.hibernate.engine.spi.EntityKey)8 QueryParameters (org.hibernate.engine.spi.QueryParameters)8