Search in sources :

Example 51 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class MapBinder method makeOneToManyMapKeyColumnNullableIfNotInProperty.

private void makeOneToManyMapKeyColumnNullableIfNotInProperty(final XProperty property) {
    final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
    if (map.isOneToMany() && property.isAnnotationPresent(MapKeyColumn.class)) {
        final Value indexValue = map.getIndex();
        if (indexValue.getColumnSpan() != 1) {
            throw new AssertionFailure("Map key mapped by @MapKeyColumn does not have 1 column");
        }
        final Selectable selectable = indexValue.getColumnIterator().next();
        if (selectable.isFormula()) {
            throw new AssertionFailure("Map key mapped by @MapKeyColumn is a Formula");
        }
        Column column = (Column) map.getIndex().getColumnIterator().next();
        if (!column.isNullable()) {
            final PersistentClass persistentClass = ((OneToMany) map.getElement()).getAssociatedClass();
            // need to check "un-joined" properties.
            if (!propertyIteratorContainsColumn(persistentClass.getUnjoinedPropertyIterator(), column)) {
                // The index column is not mapped to an associated entity property so we can
                // safely make the index column nullable.
                column.setNullable(true);
            }
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Selectable(org.hibernate.mapping.Selectable) Ejb3Column(org.hibernate.cfg.Ejb3Column) MapKeyColumn(javax.persistence.MapKeyColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Value(org.hibernate.mapping.Value) MapKeyColumn(javax.persistence.MapKeyColumn) OneToMany(org.hibernate.mapping.OneToMany) Map(java.util.Map) HashMap(java.util.HashMap) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 52 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class EnumType method setParameterValues.

@Override
public void setParameterValues(Properties parameters) {
    // IMPL NOTE: we handle 2 distinct cases here:
    // 1) we are passed a ParameterType instance in the incoming Properties - generally
    // speaking this indicates the annotation-binding case, and the passed ParameterType
    // represents information about the attribute and annotation
    // 2) we are not passed a ParameterType - generally this indicates a hbm.xml binding case.
    final ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
    if (reader != null) {
        enumClass = reader.getReturnedClass().asSubclass(Enum.class);
        final boolean isOrdinal;
        final javax.persistence.EnumType enumType = getEnumType(reader);
        if (enumType == null) {
            isOrdinal = true;
        } else if (javax.persistence.EnumType.ORDINAL.equals(enumType)) {
            isOrdinal = true;
        } else if (javax.persistence.EnumType.STRING.equals(enumType)) {
            isOrdinal = false;
        } else {
            throw new AssertionFailure("Unknown EnumType: " + enumType);
        }
        final EnumJavaTypeDescriptor enumJavaDescriptor = (EnumJavaTypeDescriptor) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor(enumClass);
        if (isOrdinal) {
            this.enumValueConverter = new OrdinalEnumValueConverter(enumJavaDescriptor);
        } else {
            this.enumValueConverter = new NamedEnumValueConverter(enumJavaDescriptor);
        }
    } else {
        final String enumClassName = (String) parameters.get(ENUM);
        try {
            enumClass = ReflectHelper.classForName(enumClassName, this.getClass()).asSubclass(Enum.class);
        } catch (ClassNotFoundException exception) {
            throw new HibernateException("Enum class not found: " + enumClassName, exception);
        }
        this.enumValueConverter = interpretParameters(parameters);
    }
    LOG.debugf("Using %s-based conversion for Enum %s", isOrdinal() ? "ORDINAL" : "NAMED", enumClass.getName());
}
Also used : EnumJavaTypeDescriptor(org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor) NamedEnumValueConverter(org.hibernate.metamodel.model.convert.internal.NamedEnumValueConverter) AssertionFailure(org.hibernate.AssertionFailure) HibernateException(org.hibernate.HibernateException) OrdinalEnumValueConverter(org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)

Example 53 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StandardCacheEntryImpl method assemble.

/**
 * Assemble the previously disassembled state represented by this entry into the given entity instance.
 *
 * Additionally manages the PreLoadEvent callbacks.
 *
 * @param instance The entity instance
 * @param id The entity identifier
 * @param persister The entity persister
 * @param interceptor (currently unused)
 * @param session The session
 *
 * @return The assembled state
 *
 * @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
 *
 * @see org.hibernate.type.Type#assemble
 * @see org.hibernate.type.Type#disassemble
 */
public Object[] assemble(final Object instance, final Serializable id, final EntityPersister persister, final Interceptor interceptor, final EventSource session) throws HibernateException {
    if (!persister.getEntityName().equals(subclass)) {
        throw new AssertionFailure("Tried to assemble a different subclass instance");
    }
    // assembled state gets put in a new array (we read from cache by value!)
    final Object[] state = TypeHelper.assemble(disassembledState, persister.getPropertyTypes(), session, instance);
    // persister.setIdentifier(instance, id); //before calling interceptor, for consistency with normal load
    // TODO: reuse the PreLoadEvent
    final PreLoadEvent preLoadEvent = new PreLoadEvent(session).setEntity(instance).setState(state).setId(id).setPersister(persister);
    final EventListenerGroup<PreLoadEventListener> listenerGroup = session.getFactory().getServiceRegistry().getService(EventListenerRegistry.class).getEventListenerGroup(EventType.PRE_LOAD);
    for (PreLoadEventListener listener : listenerGroup.listeners()) {
        listener.onPreLoad(preLoadEvent);
    }
    persister.setPropertyValues(instance, state);
    return state;
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) PreLoadEventListener(org.hibernate.event.spi.PreLoadEventListener) PreLoadEvent(org.hibernate.event.spi.PreLoadEvent) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 54 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StatefulPersistenceContext method addCollection.

/**
 * Add a collection to the cache, with a given collection entry.
 *
 * @param coll The collection for which we are adding an entry.
 * @param entry The entry representing the collection.
 * @param key The key of the collection's entry.
 */
private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key) {
    collectionEntries.put(coll, entry);
    final CollectionKey collectionKey = new CollectionKey(entry.getLoadedPersister(), key);
    final PersistentCollection old = collectionsByKey.put(collectionKey, coll);
    if (old != null) {
        if (old == coll) {
            throw new AssertionFailure("bug adding collection twice");
        }
        // or should it actually throw an exception?
        old.unsetSession(session);
        collectionEntries.remove(old);
    // watch out for a case where old is still referenced
    // somewhere in the object graph! (which is a user error)
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AssertionFailure(org.hibernate.AssertionFailure) CollectionKey(org.hibernate.engine.spi.CollectionKey)

Example 55 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class TwoPhaseLoad method initializeEntity.

/**
 * Perform the second step of 2-phase load. Fully initialize the entity
 * instance.
 * <p/>
 * After processing a JDBC result set, we "resolve" all the associations
 * between the entities which were instantiated and had their state
 * "hydrated" into an array
 *
 * @param entity The entity being loaded
 * @param readOnly Is the entity being loaded as read-only
 * @param session The Session
 * @param preLoadEvent The (re-used) pre-load event
 */
public static void initializeEntity(final Object entity, final boolean readOnly, final SharedSessionContractImplementor session, final PreLoadEvent preLoadEvent) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityEntry entityEntry = persistenceContext.getEntry(entity);
    if (entityEntry == null) {
        throw new AssertionFailure("possible non-threadsafe access to the session");
    }
    doInitializeEntity(entity, entityEntry, readOnly, session, preLoadEvent);
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Aggregations

AssertionFailure (org.hibernate.AssertionFailure)56 Serializable (java.io.Serializable)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)11 Property (org.hibernate.mapping.Property)10 AnnotationException (org.hibernate.AnnotationException)9 PersistentClass (org.hibernate.mapping.PersistentClass)9 SimpleValue (org.hibernate.mapping.SimpleValue)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SQLException (java.sql.SQLException)6 HashMap (java.util.HashMap)6 HibernateException (org.hibernate.HibernateException)6 PreparedStatement (java.sql.PreparedStatement)5 Column (org.hibernate.mapping.Column)5 Component (org.hibernate.mapping.Component)5 Join (org.hibernate.mapping.Join)5 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)5 Iterator (java.util.Iterator)4 Map (java.util.Map)4 XProperty (org.hibernate.annotations.common.reflection.XProperty)4 EntityDataAccess (org.hibernate.cache.spi.access.EntityDataAccess)4