The following is the error that i am getting
when i try to insert a value of zero into the id field of county table....
.using setCountyId(0).
THe application works perfectly if i try to set the countyId to any other integer........it ONLY has a problem with ZERO.
I am using DB2 version 7.
Using eclipse based IDE
Any help would be greatly appreciated.
[5/22/07 17:02:47:859 EDT] 00000026 SystemOut O ERROR SimpleHibernateTransactionImpl:200 - org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: County
**************
County.java
**************
public class County implements Serializable{
static Logger logger = Logger.getLogger(County.class);
private int countyId;
private String countyCode;
private String countyName;
private String countyShortName;
private LiabilityData liabilityData;
/**
* @return the countyCode
*/
public String getCountyCode() {
return countyCode;
}
/**
* @param countyCode the countyCode to set
*/
public void setCountyCode(String countyCode) {
this.countyCode = countyCode;
}
.....
*************
county.hbm.xml
*************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="County" table="TRCECO" lazy="true">
<!-- ID field -->
<id name="countyId" type="int" column="CO_ID">
<generator class="increment"/>
</id>
<!-- Component fields -->
<component name="liabilityData" class="LiabilityData">
<property name="addedDate" column="CO_ADD_DT" type="date" not-null="true"/>
<property name="updatedDate" column="CO_LAST_UPD" type="date" not-null="true"/>
<property name="addedId" column="CO_ADD_USERID" type="string" length="8" not-null="true" />
<property name="updatedId" column="CO_LAST_UPD_USERID" type="string" length="8" not-null="true" />
</component>
<!-- String fields -->
<property name="countyName" column="CO_COUNTY_NM" type="string" length="30" not-null="true" />
<property name="countyCode" column="CO_COUNTY_CDE" type="string" length="3" not-null="true" />
<property name="countyShortName" column="CO_SHORT_NM" type="string" length="30" not-null="true" />
</class>
</hibernate-mapping>
************************************************************
SimpleHibernateTransactionImpl.java
*************************************
public class SimpleHibernateTransactionImpl extends HibernateDaoSupport
implements SimpleTransaction
{
static Logger logger = Logger.getLogger( SimpleHibernateTransactionImpl.class );
/* (non-Javadoc)
* @see
SimpleTransaction#process(java.lang.Object[], int[])
*/
public boolean process( Object[] objects, int[] operationCodes )
{
validateUnitOfWork( objects, operationCodes );
boolean success = false;
SessionFactory factory = getSessionFactory( );
Session session = getSession( );
Transaction tx = null;
try
{
tx = session.beginTransaction( );
int count = objects == null ? 0 : objects.length;
Object object = null;
int actionCode = 0;
for ( int i = 0; i < count; ++i )
{
process( objects[ i ], operationCodes[ i ], session );
}
tx.commit( );
//success = true;
}
catch ( HibernateException e )
{
tx.rollback( );
logger.error( e.toString( ) );
}
finally
{
tx = null;
try
{
if ( session != null )
{
session.close( );
session = null;
}
}
catch ( HibernateException e )
{
logger.warn( e.toString( ) );
}
}
return ( success );
}
/**
* Process a single action within the confines of a shared session.
*
* @param object the object to process
* @param operationCode the operation code
* @param session the session in which to process the work
* @throws HibernateException if an exception occurs during process
*/
private void process( Object object, int operationCode , Session session )
throws HibernateException
{
switch( operationCode )
{
case SimpleTransaction.CREATE:
{
session.save( object );
break;
}
case SimpleTransaction.UPDATE:
{
session.update( object );
break;
}
case SimpleTransaction.CREATE_OR_UPDATE:
{
session.saveOrUpdate( object );
break;
}
case SimpleTransaction.DELETE:
{
session.delete( object );
break;
}
default:
{
session.saveOrUpdate( object );
break;
}
}
}
/**
* Validate the objects and operationCodes
* @param objects
* @param operationCodes
*/
private void validateUnitOfWork( Object[] objects, int[] operationCodes )
{
if ( objects == null )
{
logger.error( "The objects array is null" );
throw new RuntimeException( "The objects array is null" );
}
if ( operationCodes == null )
{
logger.error( "The operation codes array is null" );
throw new RuntimeException( "The operation codes array is null" );
}
if ( objects.length != operationCodes.length )
{
logger.error( "The objects array length (" + objects.length + ")" +
" does not equal the operation codes array length (" +
operationCodes.length + ")" );
throw new RuntimeException( "The objects array and the operation" +
"codes array are the not the same length" );
}
}
/* (non-Javadoc)
* @see SimpleTransaction#process(java.lang.Object[], int)
*/
public boolean process( Object[] objects, int operationCode )
{
// validateUnitOfWork( objects, operationCodes );
boolean success = false;
SessionFactory factory = getSessionFactory( );
Session session = getSession( );
Transaction tx = null;
try
{
tx = session.beginTransaction( );
int count = objects == null ? 0 : objects.length;
Object object = null;
int actionCode = 0;
for ( int i = 0; i < count; ++i )
{
process( objects[ i ], operationCode, session );
}
//session.save(objects[0]);
tx.commit( );
success = true;
}
catch ( HibernateException e )
{
tx.rollback( );
logger.error( e.toString( ) );
}
finally
{
tx = null;
try
{
if ( session != null )
{
session.close( );
session = null;
}
}
catch ( HibernateException e )
{
logger.warn( e.toString( ) );
}
}
return ( success );
}
}
Hibernate version:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html