-->
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.  [ 9 posts ] 
Author Message
 Post subject: Trying to insert a ZERO INTO ID --> TransientObjectExcept
PostPosted: Tue May 22, 2007 5:22 pm 
Newbie

Joined: Tue May 22, 2007 4:55 pm
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 12:33 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Hibernate_is_Awesome,

Try with unsaved value
<id name="countyId" type="int" column="CO_ID" unsaved-value="0" >
<generator class="increment"/>
</id>

and check all the not property just before save.Post your business code that might help us to dig out your problem.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject: Re: Trying to insert a ZERO INTO ID --> TransientObjectEx
PostPosted: Wed May 23, 2007 6:45 am 
Newbie

Joined: Wed May 23, 2007 6:42 am
Posts: 3
Hibernate_is_Awesome wrote:

private int countyId;



This is the error: you can't set a int value and expect that 0 is treated differenly than null (as it is the default value for a jdbc getInt()).

Just use and Integer instead.


Top
 Profile  
 
 Post subject: Re: Trying to insert a ZERO INTO ID --> TransientObjectEx
PostPosted: Wed May 23, 2007 1:58 pm 
Newbie

Joined: Tue May 22, 2007 4:55 pm
Posts: 4
Thanks for the reply.

Can you please further expalin me what you are trying to suggest. ( maybe piece of code)

I didn't get it.


blueworld wrote:
Hibernate_is_Awesome wrote:

private int countyId;



This is the error: you can't set a int value and expect that 0 is treated differenly than null (as it is the default value for a jdbc getInt()).

Just use and Integer instead.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 2:02 pm 
Newbie

Joined: Tue May 22, 2007 4:55 pm
Posts: 4
dharmendra.pandey wrote:
Hi Hibernate_is_Awesome,

Try with unsaved value
<id name="countyId" type="int" column="CO_ID" unsaved-value="0" >
<generator class="increment"/>
</id>

and check all the not property just before save.Post your business code that might help us to dig out your problem.



Hey Dharmendra,

unsaved-value="0" part didn't work. It still gives me a different error when i try to run it.

Some one told me that Hibernate treats "0" as a special case....and it never accepts that value as a primary key or a foreign key. Is that correct.

Please suggest if there are any hacks or how to go about creating a custom jdbc for just this case ( insertion of a zero).

THanks a lot for your reply.


Top
 Profile  
 
 Post subject: Re: Trying to insert a ZERO INTO ID --> TransientObjectEx
PostPosted: Wed May 23, 2007 2:46 pm 
Newbie

Joined: Wed May 23, 2007 6:42 am
Posts: 3
Hibernate_is_Awesome wrote:
Thanks for the reply.

Can you please further expalin me what you are trying to suggest. ( maybe piece of code)

I didn't get it.


Just try and replace

private int countyId;

with

private Integer countyId;

and correspondent setter/getter, if ever, and try it again. It will work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 11:55 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Hibernate_is_Awesome,


I am assuming that you are very good in java then you must be knowing that class member primitive variable has default value. So int have zero by default. Using this concept Hibernate Team has build Id logic, that is this case 0 would be treated as non persistent and other than that are would be persistent entity. So if you are willing to use 0 as PK then blueworld has suggested .For that case use unsaved-value="null”. But personally I am not agree that any business need 0 as PK .It should start with some logical number. Let say start with 1 .That means this is first entry of that table. But what dose zero means. Increasing sequence number with 1 would cast much more to DB.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 4:18 am 
Newbie

Joined: Wed May 23, 2007 6:42 am
Posts: 3
I agree with all but this:

dharmendra.pandey wrote:
But personally I am not agree that any business need 0 as PK.


My case is that I have a hierarchy of nodes related one with each other via parent-children relationship. In the first implementation I didn't need a root node, so I just created first parents with parent = null (and, FYI, the IDs were primitive ints).

Now I have to create a root node and the application is already in production. The only available node is 0 then... :)


Top
 Profile  
 
 Post subject: Re: Trying to insert a ZERO INTO ID --> TransientObjectEx
PostPosted: Wed Jul 11, 2007 4:33 pm 
Newbie

Joined: Tue May 22, 2007 4:55 pm
Posts: 4
blueworld wrote:
Hibernate_is_Awesome wrote:
Thanks for the reply.

Can you please further expalin me what you are trying to suggest. ( maybe piece of code)

I didn't get it.


Just try and replace

private int countyId;

with

private Integer countyId;

and correspondent setter/getter, if ever, and try it again. It will work.


Thanks a lot Blueworld. It worked!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.