Search in sources :

Example 1 with TypeMismatchException

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

the class DefaultLoadEventListener method checkIdClass.

private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadEventListener.LoadType loadType, final Class idClass) {
    // is part of its generally goofy "derived identity" "feature"
    if (persister.getEntityMetamodel().getIdentifierProperty().isEmbedded()) {
        final EmbeddedComponentType dependentIdType = (EmbeddedComponentType) persister.getEntityMetamodel().getIdentifierProperty().getType();
        if (dependentIdType.getSubtypes().length == 1) {
            final Type singleSubType = dependentIdType.getSubtypes()[0];
            if (singleSubType.isEntityType()) {
                final EntityType dependentParentType = (EntityType) singleSubType;
                final Type dependentParentIdType = dependentParentType.getIdentifierOrUniqueKeyType(event.getSession().getFactory());
                if (dependentParentIdType.getReturnedClass().isInstance(event.getEntityId())) {
                    // yep that's what we have...
                    loadByDerivedIdentitySimplePkValue(event, loadType, persister, dependentIdType, event.getSession().getFactory().getEntityPersister(dependentParentType.getAssociatedEntityName()));
                    return;
                }
            }
        }
    }
    throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
Also used : EntityType(org.hibernate.type.EntityType) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) EventType(org.hibernate.event.spi.EventType) Type(org.hibernate.type.Type) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) TypeMismatchException(org.hibernate.TypeMismatchException)

Example 2 with TypeMismatchException

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

the class ASTParserLoadingTest method testParameterTypeMismatch.

@Test
@FailureExpected(jiraKey = "unknown")
public void testParameterTypeMismatch() {
    Session s = openSession();
    s.beginTransaction();
    Query query = s.createQuery("from Animal a where a.description = :nonstring").setParameter("nonstring", Integer.valueOf(1));
    try {
        query.list();
        fail("query execution should have failed");
    } catch (IllegalArgumentException e) {
        assertTyping(TypeMismatchException.class, e.getCause());
    } catch (TypeMismatchException tme) {
    // expected behavior
    }
    s.getTransaction().commit();
    s.close();
}
Also used : Query(org.hibernate.Query) TypeMismatchException(org.hibernate.TypeMismatchException) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 3 with TypeMismatchException

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

the class SessionImpl method find.

@Override
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
    checkOpen();
    LockOptions lockOptions = null;
    try {
        if (properties != null && !properties.isEmpty()) {
            getLoadQueryInfluencers().setFetchGraph((EntityGraph) properties.get(QueryHints.HINT_FETCHGRAPH));
            getLoadQueryInfluencers().setLoadGraph((EntityGraph) properties.get(QueryHints.HINT_LOADGRAPH));
        }
        final IdentifierLoadAccess<T> loadAccess = byId(entityClass);
        loadAccess.with(determineAppropriateLocalCacheMode(properties));
        if (lockModeType != null) {
            if (!LockModeType.NONE.equals(lockModeType)) {
                checkTransactionNeeded();
            }
            lockOptions = buildLockOptions(lockModeType, properties);
            loadAccess.with(lockOptions);
        }
        return loadAccess.load((Serializable) primaryKey);
    } catch (EntityNotFoundException ignored) {
        // which find() should not throw.  Find() should return null if the entity was not found.
        if (log.isDebugEnabled()) {
            String entityName = entityClass != null ? entityClass.getName() : null;
            String identifierValue = primaryKey != null ? primaryKey.toString() : null;
            log.ignoringEntityNotFound(entityName, identifierValue);
        }
        return null;
    } catch (ObjectDeletedException e) {
        //the spec is silent about people doing remove() find() on the same PC
        return null;
    } catch (ObjectNotFoundException e) {
        //should not happen on the entity itself with get
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (MappingException | TypeMismatchException | ClassCastException e) {
        throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
    } catch (RuntimeException e) {
        throw exceptionConverter.convert(e, lockOptions);
    } finally {
        getLoadQueryInfluencers().setFetchGraph(null);
        getLoadQueryInfluencers().setLoadGraph(null);
    }
}
Also used : LockOptions(org.hibernate.LockOptions) TypeMismatchException(org.hibernate.TypeMismatchException) EntityNotFoundException(javax.persistence.EntityNotFoundException) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException) JPA_LOCK_TIMEOUT(org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) ObjectDeletedException(org.hibernate.ObjectDeletedException)

Aggregations

TypeMismatchException (org.hibernate.TypeMismatchException)3 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 LockOptions (org.hibernate.LockOptions)1 MappingException (org.hibernate.MappingException)1 ObjectDeletedException (org.hibernate.ObjectDeletedException)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 JPA_LOCK_TIMEOUT (org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT)1 EventType (org.hibernate.event.spi.EventType)1 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)1 FailureExpected (org.hibernate.testing.FailureExpected)1 EmbeddedComponentType (org.hibernate.type.EmbeddedComponentType)1 EntityType (org.hibernate.type.EntityType)1 Type (org.hibernate.type.Type)1 Test (org.junit.Test)1