-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: NonBatchingBatcher.addToBatch question
PostPosted: Wed Jan 11, 2006 8:49 pm 
Newbie

Joined: Sat Feb 19, 2005 1:34 am
Posts: 5
Hibernate version:3.0


NonBatchingBatcher.addToBatch throws a HibernateException in this code block:

if ( expectedRowCount>rowCount ) {
throw new HibernateException(
"Unexpected row count: " + rowCount +
" expected: " + expectedRowCount
);
}


This code is indirectly called from BasicEntityPersister.updateOrInsert in this code:

if ( !isInverseTable( j ) ) {
final boolean isRowToUpdate;
if ( isNullableTable( j ) && oldFields != null && isAllNull( oldFields, j ) ) {
//don't bother trying to update, we know there is no row there yet
isRowToUpdate = false;
}
else if ( isNullableTable( j ) && isAllNull( fields, j ) ) {
//if all fields are null, we might need to delete existing row
isRowToUpdate = true;
delete( id, oldVersion, j, object, getSQLDeleteStrings()[j], session );
}
else {
//there is probably a row there, so try to update
//if no rows were updated, we will find out
isRowToUpdate = update( id, fields, oldFields, rowId, includeProperty, j, oldVersion, object, sql, session );
}

if ( !isRowToUpdate && !isAllNull( fields, j ) ) {
// assume that the row was not there since it previously had only null
// values, so do an INSERT instead
//TODO: does not respect dynamic-insert
insert( id, fields, getPropertyInsertability(), j, getSQLInsertStrings()[j], object, session ); // NOT EXECUTED IF EXCEPTION THROWN ABOVE
}
}



So the call to update(...) can throw an exception (from NonBatchingBatcher.addToBatch) which isn't caught here. This means that an update might fail; because the row does not exist say; and the insert(...) on the line below will not be executed.


Is this correct behaviour? Should some updates fail over to inserts and other not?


Regards,
Paul


Top
 Profile  
 
 Post subject: i've patched BasicEntityPersister.updateOrInsert
PostPosted: Wed Jan 18, 2006 2:13 am 
Newbie

Joined: Sat Feb 19, 2005 1:34 am
Posts: 5
as per below. please can you tell me whether or not this will work. i don't want to have to understand all of your code if i can avoid it. cheers. Paul


/**
* Perform an SQL UPDATE or SQL INSERT
*/
protected void updateOrInsert(final Serializable id,
final Object[] fields,
final Object[] oldFields,
final Object rowId,
final boolean[] includeProperty,
final int j,
final Object oldVersion,
final Object object,
final String sql,
final SessionImplementor session)
throws HibernateException {

if ( !isInverseTable( j ) ) {
boolean isRowToUpdate;
boolean isRowToInsert;

if ( isNullableTable( j ) && oldFields != null && isAllNull( oldFields, j ) ) {
//don't bother trying to update, we know there is no row there yet
isRowToUpdate = false;
}
else if ( isNullableTable( j ) && isAllNull( fields, j ) ) {
//if all fields are null, we might need to delete existing row
isRowToUpdate = true;
delete( id, oldVersion, j, object, getSQLDeleteStrings()[j], session );
}
else {
//there is probably a row there, so try to update
//if no rows were updated, we will find out
try{
isRowToUpdate = update( id, fields, oldFields, rowId, includeProperty, j, oldVersion, object, sql, session );
}
catch(StaleStateException he){
throw he;
}
catch(HibernateException he){
isRowToUpdate = false;
}
}

isRowToInsert = true;
if ( !isRowToUpdate && !isAllNull( fields, j ) ) {
// assume that the row was not there since it previously had only null
// values, so do an INSERT instead
//TODO: does not respect dynamic-insert
try{
insert( id, fields, getPropertyInsertability(), j, getSQLInsertStrings()[j], object, session );
}
catch(StaleStateException he){
throw he;
}
catch(HibernateException he){
isRowToInsert = false;
}
}

// allow to throw exception
if(!isRowToInsert){
isRowToUpdate = update( id, fields, oldFields, rowId, includeProperty, j, oldVersion, object, sql, session );
}

}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.