下面列出了怎么用org.hibernate.TypeMismatchException的API类实例代码及写法,或者点击链接到github查看源代码。
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 );
}
}
}
}
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 );
}
}
}
}
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();
}
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 );
}
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() );
}
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()
);
}
@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() );
}
/**
* 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;
}
}