Search in sources :

Example 61 with EntityPersister

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

the class DefaultMergeEventListener method entityIsTransient.

protected void entityIsTransient(MergeEvent event, Map copyCache) {
    LOG.trace("Merging transient instance");
    final Object entity = event.getEntity();
    final EventSource source = event.getSession();
    final String entityName = event.getEntityName();
    final EntityPersister persister = source.getEntityPersister(entityName, entity);
    final Serializable id = persister.hasIdentifierProperty() ? persister.getIdentifier(entity, source) : null;
    if (copyCache.containsKey(entity)) {
        persister.setIdentifier(copyCache.get(entity), id, source);
    } else {
        //beforeQuery cascade!
        ((MergeContext) copyCache).put(entity, source.instantiate(persister, id), true);
    }
    final Object copy = copyCache.get(entity);
    // cascade first, so that all unsaved objects get their
    // copy created beforeQuery we actually copy
    //cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
    super.cascadeBeforeSave(source, persister, entity, copyCache);
    copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.FROM_PARENT);
    saveTransientEntity(copy, entityName, event.getRequestedId(), source, copyCache);
    // cascade first, so that all unsaved objects get their
    // copy created beforeQuery we actually copy
    super.cascadeAfterSave(source, persister, entity, copyCache);
    copyValues(persister, entity, copy, source, copyCache, ForeignKeyDirection.TO_PARENT);
    event.setResult(copy);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EventSource(org.hibernate.event.spi.EventSource) Serializable(java.io.Serializable)

Example 62 with EntityPersister

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

the class FromElementFactory method addFromElement.

FromElement addFromElement() throws SemanticException {
    final FromClause parentFromClause = fromClause.getParentFromClause();
    if (parentFromClause != null) {
        // Look up class name using the first identifier in the path.
        final String pathAlias = PathHelper.getAlias(path);
        final FromElement parentFromElement = parentFromClause.getFromElement(pathAlias);
        if (parentFromElement != null) {
            return createFromElementInSubselect(path, pathAlias, parentFromElement, classAlias);
        }
    }
    final EntityPersister entityPersister = fromClause.getSessionFactoryHelper().requireClassPersister(path);
    final FromElement elem = createAndAddFromElement(path, classAlias, entityPersister, (EntityType) ((Queryable) entityPersister).getType(), null);
    // Add to the query spaces.
    fromClause.getWalker().addQuerySpaces(entityPersister.getQuerySpaces());
    return elem;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Queryable(org.hibernate.persister.entity.Queryable)

Example 63 with EntityPersister

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

the class FromElementFactory method createFromElementInSubselect.

private FromElement createFromElementInSubselect(String path, String pathAlias, FromElement parentFromElement, String classAlias) throws SemanticException {
    LOG.debugf("createFromElementInSubselect() : path = %s", path);
    // Create an DotNode AST for the path and resolve it.
    FromElement fromElement = evaluateFromElementPath(path, classAlias);
    EntityPersister entityPersister = fromElement.getEntityPersister();
    // If the first identifier in the path refers to the class alias (not the class name), then this
    // is a correlated subselect.  If it's a correlated sub-select, use the existing table alias.  Otherwise
    // generate a new one.
    String tableAlias = null;
    boolean correlatedSubselect = pathAlias.equals(parentFromElement.getClassAlias());
    if (correlatedSubselect) {
        tableAlias = fromElement.getTableAlias();
    } else {
        tableAlias = null;
    }
    // If the from element isn't in the same clause, create a new from element.
    if (fromElement.getFromClause() != fromClause) {
        LOG.debug("createFromElementInSubselect() : creating a new FROM element...");
        fromElement = createFromElement(entityPersister);
        initializeAndAddFromElement(fromElement, path, classAlias, entityPersister, (EntityType) ((Queryable) entityPersister).getType(), tableAlias);
    }
    LOG.debugf("createFromElementInSubselect() : %s -> %s", path, fromElement);
    return fromElement;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) Queryable(org.hibernate.persister.entity.Queryable)

Example 64 with EntityPersister

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

the class FromElementFactory method createJoin.

private FromElement createJoin(String entityClass, String tableAlias, JoinSequence joinSequence, EntityType type, boolean manyToMany) throws SemanticException {
    //  origin, path, implied, columns, classAlias,
    EntityPersister entityPersister = fromClause.getSessionFactoryHelper().requireClassPersister(entityClass);
    FromElement destination = createAndAddFromElement(entityClass, classAlias, entityPersister, type, tableAlias);
    return initializeJoin(path, destination, joinSequence, getColumns(), origin, manyToMany);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister)

Example 65 with EntityPersister

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

the class FromElementFactory method createEntityJoin.

public FromElement createEntityJoin(String entityClass, String tableAlias, JoinSequence joinSequence, boolean fetchFlag, boolean inFrom, EntityType type, String role, String joinPath) throws SemanticException {
    FromElement elem = createJoin(entityClass, tableAlias, joinSequence, type, false);
    elem.setFetch(fetchFlag);
    if (joinPath != null) {
        elem.applyTreatAsDeclarations(fromClause.getWalker().getTreatAsDeclarationsByPath(joinPath));
    }
    EntityPersister entityPersister = elem.getEntityPersister();
    int numberOfTables = entityPersister.getQuerySpaces().length;
    if (numberOfTables > 1 && implied && !elem.useFromFragment()) {
        LOG.debug("createEntityJoin() : Implied multi-table entity join");
        elem.setUseFromFragment(true);
    }
    // flag on the FromElement that indicates that it was implied in the FROM clause itself.
    if (implied && inFrom) {
        joinSequence.setUseThetaStyle(false);
        elem.setUseFromFragment(true);
        elem.setImpliedInFromClause(true);
    }
    if (elem.getWalker().isSubQuery()) {
        //          and 'elem' represents an implicit join
        if (elem.getFromClause() != elem.getOrigin().getFromClause() || //			        ( implied && DotNode.useThetaStyleImplicitJoins ) ) {
        DotNode.useThetaStyleImplicitJoins) {
            // the "root from-element" in correlated subqueries do need this piece
            elem.setType(FROM_FRAGMENT);
            joinSequence.setUseThetaStyle(true);
            elem.setUseFromFragment(false);
        }
    }
    elem.setRole(role);
    return elem;
}
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