Search in sources :

Example 6 with ManagedEntity

use of org.hibernate.engine.spi.ManagedEntity in project hibernate-orm by hibernate.

the class BasicEnhancementTestTask method execute.

public void execute() {
    SimpleEntity entity = new SimpleEntity();
    // Call the new ManagedEntity methods
    assertTyping(ManagedEntity.class, entity);
    ManagedEntity managedEntity = (ManagedEntity) entity;
    assertSame(entity, managedEntity.$$_hibernate_getEntityInstance());
    assertNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setEntityEntry(EnhancerTestUtils.makeEntityEntry());
    assertNotNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setEntityEntry(null);
    assertNull(managedEntity.$$_hibernate_getEntityEntry());
    managedEntity.$$_hibernate_setNextManagedEntity(managedEntity);
    managedEntity.$$_hibernate_setPreviousManagedEntity(managedEntity);
    assertSame(managedEntity, managedEntity.$$_hibernate_getNextManagedEntity());
    assertSame(managedEntity, managedEntity.$$_hibernate_getPreviousManagedEntity());
    // Add an attribute interceptor...
    assertTyping(PersistentAttributeInterceptable.class, entity);
    PersistentAttributeInterceptable interceptableEntity = (PersistentAttributeInterceptable) entity;
    assertNull(interceptableEntity.$$_hibernate_getInterceptor());
    interceptableEntity.$$_hibernate_setInterceptor(new ObjectAttributeMarkerInterceptor());
    assertNotNull(interceptableEntity.$$_hibernate_getInterceptor());
    assertNull(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"));
    entity.setAnObject(new Object());
    assertSame(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"), ObjectAttributeMarkerInterceptor.WRITE_MARKER);
    assertSame(entity.getAnObject(), ObjectAttributeMarkerInterceptor.READ_MARKER);
    entity.setAnObject(null);
    assertSame(EnhancerTestUtils.getFieldByReflection(entity, "anUnspecifiedObject"), ObjectAttributeMarkerInterceptor.WRITE_MARKER);
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable)

Example 7 with ManagedEntity

use of org.hibernate.engine.spi.ManagedEntity in project hibernate-orm by hibernate.

the class EvictionTestTask method execute.

public void execute() {
    // Delete the Parent
    Session s = getFactory().openSession();
    s.beginTransaction();
    Parent loadedParent = (Parent) s.createQuery("SELECT p FROM Parent p WHERE name=:name").setParameter("name", "PARENT").uniqueResult();
    assertTyping(ManagedEntity.class, loadedParent);
    ManagedEntity managedParent = (ManagedEntity) loadedParent;
    // before eviction
    assertNotNull(managedParent.$$_hibernate_getEntityInstance());
    assertNotNull(managedParent.$$_hibernate_getEntityEntry());
    assertNull(managedParent.$$_hibernate_getPreviousManagedEntity());
    assertNull(managedParent.$$_hibernate_getNextManagedEntity());
    assertTrue(s.contains(managedParent));
    s.evict(managedParent);
    // after eviction
    assertFalse(s.contains(managedParent));
    assertNotNull(managedParent.$$_hibernate_getEntityInstance());
    assertNull(managedParent.$$_hibernate_getEntityEntry());
    assertNull(managedParent.$$_hibernate_getPreviousManagedEntity());
    assertNull(managedParent.$$_hibernate_getNextManagedEntity());
    // evict again
    s.evict(managedParent);
    assertFalse(s.contains(managedParent));
    assertNotNull(managedParent.$$_hibernate_getEntityInstance());
    assertNull(managedParent.$$_hibernate_getEntityEntry());
    assertNull(managedParent.$$_hibernate_getPreviousManagedEntity());
    assertNull(managedParent.$$_hibernate_getNextManagedEntity());
    s.delete(managedParent);
    s.getTransaction().commit();
    s.close();
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity) Session(org.hibernate.Session)

Example 8 with ManagedEntity

use of org.hibernate.engine.spi.ManagedEntity in project hibernate-orm by hibernate.

the class EntityEntryContext method addEntityEntry.

/**
	 * Adds the entity and entry to this context, associating them together
	 *
	 * @param entity The entity
	 * @param entityEntry The entry
	 */
public void addEntityEntry(Object entity, EntityEntry entityEntry) {
    // IMPORTANT!!!!!
    //		add is called more than once of some entities.  In such cases the first
    //		call is simply setting up a "marker" to avoid infinite looping from reentrancy
    // any addition (even the double one described above) should invalidate the cross-ref array
    dirty = true;
    assert AbstractEntityEntry.class.isInstance(entityEntry);
    // ImmutableEntityEntry#getPersistenceContext() throws an exception (HHH-10251).
    if (entityEntry.getPersister().isMutable()) {
        assert AbstractEntityEntry.class.cast(entityEntry).getPersistenceContext() == persistenceContext;
    }
    // Determine the appropriate ManagedEntity instance to use based on whether the entity is enhanced or not.
    // Throw an exception if entity is a mutable ManagedEntity that is associated with a different
    // PersistenceContext.
    ManagedEntity managedEntity = getAssociatedManagedEntity(entity);
    final boolean alreadyAssociated = managedEntity != null;
    if (!alreadyAssociated) {
        if (ManagedEntity.class.isInstance(entity)) {
            if (entityEntry.getPersister().isMutable()) {
                managedEntity = (ManagedEntity) entity;
                // We know that managedEntity is not associated with the same PersistenceContext.
                // Check if managedEntity is associated with a different PersistenceContext.
                checkNotAssociatedWithOtherPersistenceContextIfMutable(managedEntity);
            } else {
                // Create a holder for PersistenceContext-related data.
                managedEntity = new ImmutableManagedEntityHolder((ManagedEntity) entity);
                if (immutableManagedEntityXref == null) {
                    immutableManagedEntityXref = new IdentityHashMap<ManagedEntity, ImmutableManagedEntityHolder>();
                }
                immutableManagedEntityXref.put((ManagedEntity) entity, (ImmutableManagedEntityHolder) managedEntity);
            }
        } else {
            if (nonEnhancedEntityXref == null) {
                nonEnhancedEntityXref = new IdentityHashMap<Object, ManagedEntity>();
            }
            managedEntity = new ManagedEntityImpl(entity);
            nonEnhancedEntityXref.put(entity, managedEntity);
        }
    }
    // associate the EntityEntry with the entity
    managedEntity.$$_hibernate_setEntityEntry(entityEntry);
    if (alreadyAssociated) {
        // if the entity was already associated with the context, skip the linking step.
        return;
    }
    // finally, set up linking and count
    if (tail == null) {
        assert head == null;
        // Protect against stale data in the ManagedEntity and nullify previous/next references.
        managedEntity.$$_hibernate_setPreviousManagedEntity(null);
        managedEntity.$$_hibernate_setNextManagedEntity(null);
        head = managedEntity;
        tail = head;
        count = 1;
    } else {
        tail.$$_hibernate_setNextManagedEntity(managedEntity);
        managedEntity.$$_hibernate_setPreviousManagedEntity(tail);
        // Protect against stale data left in the ManagedEntity nullify next reference.
        managedEntity.$$_hibernate_setNextManagedEntity(null);
        tail = managedEntity;
        count++;
    }
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity)

Example 9 with ManagedEntity

use of org.hibernate.engine.spi.ManagedEntity in project hibernate-orm by hibernate.

the class EntityEntryContext method downgradeLocks.

/**
	 * Down-grade locks to NONE for all entities in this context
	 */
public void downgradeLocks() {
    if (head == null) {
        return;
    }
    ManagedEntity node = head;
    while (node != null) {
        node.$$_hibernate_getEntityEntry().setLockMode(LockMode.NONE);
        node = node.$$_hibernate_getNextManagedEntity();
    }
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity)

Example 10 with ManagedEntity

use of org.hibernate.engine.spi.ManagedEntity in project hibernate-orm by hibernate.

the class EntityEntryContext method serialize.

/**
	 * JDK serialization hook for serializing
	 *
	 * @param oos The stream to write ourselves to
	 *
	 * @throws IOException Indicates an IO exception accessing the given stream
	 */
public void serialize(ObjectOutputStream oos) throws IOException {
    log.tracef("Starting serialization of [%s] EntityEntry entries", count);
    oos.writeInt(count);
    if (count == 0) {
        return;
    }
    ManagedEntity managedEntity = head;
    while (managedEntity != null) {
        // so we know whether or not to build a ManagedEntityImpl on deserialize
        oos.writeBoolean(managedEntity == managedEntity.$$_hibernate_getEntityInstance());
        oos.writeObject(managedEntity.$$_hibernate_getEntityInstance());
        // we need to know which implementation of EntityEntry is being serialized
        oos.writeInt(managedEntity.$$_hibernate_getEntityEntry().getClass().getName().length());
        oos.writeChars(managedEntity.$$_hibernate_getEntityEntry().getClass().getName());
        managedEntity.$$_hibernate_getEntityEntry().serialize(oos);
        managedEntity = managedEntity.$$_hibernate_getNextManagedEntity();
    }
}
Also used : ManagedEntity(org.hibernate.engine.spi.ManagedEntity)

Aggregations

ManagedEntity (org.hibernate.engine.spi.ManagedEntity)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 Session (org.hibernate.Session)1 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)1 PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)1