-->
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.  [ 3 posts ] 
Author Message
 Post subject: asking Hibernate to handle 0 and null id as "unsaved&qu
PostPosted: Sun Mar 22, 2009 10:52 pm 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
I am using a Flex client with BlazeDS remoting. The entity on the client side has a corresponding DTO object and the Long id translates to a Number class on the Flex side. Unfortunately Adobe designed the Number class in such a way that it cannot be null. Yes, a single class out of thousands of ActionScript classes that cannot be null!!!

When a new entity rematerializes on the server side the id is 0 instead of null. My entity is defined simply:
Code:
@Id
@Column(name="ID")
@GeneratedValue
protected Long id;


Is there any way to ask hibernate to interpret null and 0 as the same thing - unsaved entity?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 12:02 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
what does the generated hibernate mapping file look like?
does it have any unsaved value specified in it??

have you tried some how through annotations to set the unsaved value to 0 and see if that helps..
<id name=”id” column=”id” unsaved-value=”0″>

Also initialize the id to zero by default and see if that helps at least from your client side..

@Id
@Column(name="ID")
@GeneratedValue
protected Long id = new Long(0);

Hope this helps,
Latha.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 24, 2009 9:29 pm 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
Thanks Latha. I ended up trying @PrePersist annotation which did not solve the problem. I could not alter the id before the entity persisted.

Code:
//inside entity
@PrePersist
@PreUpdate
private void fixId(){
   if (id == 0)
          id = null;
}


Then i found a bug on Flex serialization - abandoned and unlikely to be fixed.

Finally i decided to change id to primitive long, so now 0's are not a problem. Hibernate assumes 0 as uninitialized entity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.