org.hibernate.TypeMismatchException Java Examples

The following examples show how to use org.hibernate.TypeMismatchException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BinaryLogicOperatorNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected final void mutateRowValueConstructorSyntaxesIfNecessary(Type lhsType, Type rhsType) {
	// TODO : this really needs to be delayed until after we definitively know all node types
	// where this is currently a problem is parameters for which where we cannot unequivocally
	// resolve an expected type
	SessionFactoryImplementor sessionFactory = getSessionFactoryHelper().getFactory();
	if ( lhsType != null && rhsType != null ) {
		int lhsColumnSpan = getColumnSpan( lhsType, sessionFactory );
		if ( lhsColumnSpan != getColumnSpan( rhsType, sessionFactory ) ) {
			throw new TypeMismatchException(
					"left and right hand sides of a binary logic operator were incompatibile [" +
							lhsType.getName() + " : " + rhsType.getName() + "]"
			);
		}
		if ( lhsColumnSpan > 1 ) {
			// for dialects which are known to not support ANSI-SQL row-value-constructor syntax,
			// we should mutate the tree.
			if ( !sessionFactory.getDialect().supportsRowValueConstructorSyntax() ) {
				mutateRowValueConstructorSyntax( lhsColumnSpan );
			}
		}
	}
}
 
Example #2
Source File: BinaryLogicOperatorNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected final void mutateRowValueConstructorSyntaxesIfNecessary(Type lhsType, Type rhsType) {
	// TODO : this really needs to be delayed unitl after we definitively know all node types
	// where this is currently a problem is parameters for which where we cannot unequivocally
	// resolve an expected type
	SessionFactoryImplementor sessionFactory = getSessionFactoryHelper().getFactory();
	if ( lhsType != null && rhsType != null ) {
		int lhsColumnSpan = lhsType.getColumnSpan( sessionFactory );
		if ( lhsColumnSpan != rhsType.getColumnSpan( sessionFactory ) ) {
			throw new TypeMismatchException(
					"left and right hand sides of a binary logic operator were incompatibile [" +
					lhsType.getName() + " : "+ rhsType.getName() + "]"
			);
		}
		if ( lhsColumnSpan > 1 ) {
			// for dialects which are known to not support ANSI-SQL row-value-constructor syntax,
			// we should mutate the tree.
			if ( !sessionFactory.getDialect().supportsRowValueConstructorSyntax() ) {
				mutateRowValueConstructorSyntax( lhsColumnSpan );
			}
		}
	}
}
 
Example #3
Source File: ASTParserLoadingTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void testParameterTypeMismatchFailureExpected() {
	Session s = openSession();
	s.beginTransaction();

	Query query = s.createQuery( "from Animal a where a.description = :nonstring" )
			.setParameter( "nonstring", new Integer(1) );
	try {
		query.list();
		fail( "query execution should have failed" );
	}
	catch( TypeMismatchException tme ) {
		// expected behavior
	}

	s.getTransaction().commit();
	s.close();
}
 
Example #4
Source File: ReactiveQuery.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
static <T> T convertQueryException(T result, Throwable e,
								   AbstractProducedQuery<?> query) {
	if ( e instanceof QueryExecutionRequestException) {
		throw new IllegalStateException( e );
	}
	if ( e instanceof TypeMismatchException) {
		throw new IllegalStateException( e );
	}
	if ( e instanceof HibernateException) {
		throw query.getProducer().getExceptionConverter()
				.convert( (HibernateException) e, query.getLockOptions() );
	}
	return CompletionStages.returnOrRethrow( e, result );
}
 
Example #5
Source File: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CompletionStage<Void> checkIdClass(
		final EntityPersister persister,
		final LoadEvent event,
		final LoadEventListener.LoadType loadType,
		final Class<?> idClass) {
	// we may have the kooky jpa requirement of allowing find-by-id where
	// "id" is the "simple pk value" of a dependent objects parent.  This
	// is part of its generally goofy "derived identity" "feature"
	final IdentifierProperty identifierProperty = persister.getEntityMetamodel().getIdentifierProperty();
	if ( identifierProperty.isEmbedded() ) {
		final EmbeddedComponentType dependentIdType = (EmbeddedComponentType) identifierProperty.getType();
		if ( dependentIdType.getSubtypes().length == 1 ) {
			final Type singleSubType = dependentIdType.getSubtypes()[0];
			if ( singleSubType.isEntityType() ) {
				final EntityType dependentParentType = (EntityType) singleSubType;
				final SessionFactoryImplementor factory = event.getSession().getFactory();
				final Type dependentParentIdType = dependentParentType.getIdentifierOrUniqueKeyType( factory );
				if ( dependentParentIdType.getReturnedClass().isInstance( event.getEntityId() ) ) {
					// yep that's what we have...
					return loadByDerivedIdentitySimplePkValue( event, loadType, persister,
							dependentIdType, factory.getMetamodel().entityPersister( dependentParentType.getAssociatedEntityName() )
					);
				}
			}
		}
	}
	throw new TypeMismatchException(
			"Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass() );
}
 
Example #6
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void checkIdClass(
		final EntityPersister persister,
		final LoadEvent event,
		final LoadEventListener.LoadType loadType,
		final Class idClass) {
			// we may have the kooky jpa requirement of allowing find-by-id where
		// "id" is the "simple pk value" of a dependent objects parent.  This
		// 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()
		);
}
 
Example #7
Source File: ReactiveSessionImpl.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
	public <T> CompletionStage<T> reactiveFind(
			Class<T> entityClass,
			Object id,
			LockMode lockMode,
			Map<String, Object> properties) {
		checkOpen();

		getLoadQueryInfluencers().getEffectiveEntityGraph().applyConfiguredGraph( properties );

		Boolean readOnly = properties == null ? null : (Boolean) properties.get( QueryHints.HINT_READONLY );
		getLoadQueryInfluencers().setReadOnly( readOnly );

		final ReactiveIdentifierLoadAccessImpl<T> loadAccess =
				new ReactiveIdentifierLoadAccessImpl<>(entityClass)
						.with( determineAppropriateLocalCacheMode( properties ) );

		LockOptions lockOptions;
		if ( lockMode != null ) {
//			if ( !LockModeType.NONE.equals( lockModeType) ) {
//					checkTransactionNeededForUpdateOperation();
//			}
			lockOptions = buildLockOptions(
					LockModeConverter.convertToLockModeType(lockMode),
					properties
			);
			loadAccess.with( lockOptions );
		}
		else {
			lockOptions = null;
		}

		return loadAccess.load( (Serializable) id )
				.handle( (result, e) -> {
					if ( e instanceof EntityNotFoundException) {
						// DefaultLoadEventListener.returnNarrowedProxy may throw ENFE (see HHH-7861 for details),
						// 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 = id != null ? id.toString() : null ;
						//				log.ignoringEntityNotFound( entityName, identifierValue );
						//			}
						return null;
					}
					if ( e instanceof ObjectDeletedException) {
						//the spec is silent about people doing remove() find() on the same PC
						return null;
					}
					if ( e instanceof ObjectNotFoundException) {
						//should not happen on the entity itself with get
						throw new IllegalArgumentException( e.getMessage(), e );
					}
					if ( e instanceof MappingException
							|| e instanceof TypeMismatchException
							|| e instanceof ClassCastException ) {
						throw getExceptionConverter().convert( new IllegalArgumentException( e.getMessage(), e ) );
					}
					if ( e instanceof JDBCException ) {
//						if ( accessTransaction().getRollbackOnly() ) {
//							// assume this is the similar to the WildFly / IronJacamar "feature" described under HHH-12472
//							return null;
//						}
						throw getExceptionConverter().convert( (JDBCException) e, lockOptions );
					}
					if ( e instanceof RuntimeException ) {
						throw getExceptionConverter().convert( (RuntimeException) e, lockOptions );
					}

					return result;
				} )
				.whenComplete( (v, e) -> getLoadQueryInfluencers().getEffectiveEntityGraph().clear() );
	}
 
Example #8
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/** 
 * Handle the given load event.
 *
 * @param event The load event to be handled.
 * @throws HibernateException
 */
public void onLoad(LoadEvent event, LoadEventListener.LoadType loadType) throws HibernateException {

	final SessionImplementor source = event.getSession();

	EntityPersister persister;
	if ( event.getInstanceToLoad() != null ) {
		persister = source.getEntityPersister( null, event.getInstanceToLoad() ); //the load() which takes an entity does not pass an entityName
		event.setEntityClassName( event.getInstanceToLoad().getClass().getName() );
	}
	else {
		persister = source.getFactory().getEntityPersister( event.getEntityClassName() );
	}

	if ( persister == null ) {
		throw new HibernateException( 
				"Unable to locate persister: " + 
				event.getEntityClassName() 
			);
	}

	if ( persister.getIdentifierType().isComponentType() && EntityMode.DOM4J == event.getSession().getEntityMode() ) {
		// skip this check for composite-ids relating to dom4j entity-mode;
		// alternatively, we could add a check to make sure the incoming id value is
		// an instance of Element...
	}
	else {
		Class idClass = persister.getIdentifierType().getReturnedClass();
		if ( idClass != null && ! idClass.isInstance( event.getEntityId() ) ) {
			throw new TypeMismatchException(
					"Provided id of the wrong type. Expected: " + idClass + ", got " + event.getEntityId().getClass()
			);
		}
	}

	EntityKey keyToLoad = new EntityKey( event.getEntityId(), persister, source.getEntityMode()  );

	try {
		if ( loadType.isNakedEntityReturned() ) {
			//do not return a proxy!
			//(this option indicates we are initializing a proxy)
			event.setResult( load(event, persister, keyToLoad, loadType) );
		}
		else {
			//return a proxy if appropriate
			if ( event.getLockMode() == LockMode.NONE ) {
				event.setResult( proxyOrLoad(event, persister, keyToLoad, loadType) );
			}
			else {
				event.setResult( lockAndLoad(event, persister, keyToLoad, loadType, source) );
			}
		}
	}
	catch(HibernateException e) {
		log.info("Error performing load command", e);
		throw e;
	}
}