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());
}
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();
}
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);
}
}
Aggregations