Sunday, November 6, 2011

EclipseLink: Null primary key encountered in unit of work clone

I came across this weird issue while using EclipseLink 2.0 with JPA 2.0. It isn't actually that complicated as it looks. Though I spent lot of time figuring out whether any objects have null id or negative id. Finally figured out that, The database had rows with id as '0'. By default EclipseLink treats zero ids as null. I don't


Simple fix is to tell EclipseLink to treat '0' as a valid id and not null. It can be done by specifying the property in persistence.xml 


In persistence.xml add the following property to get rid of this exception. 


     <property name="eclipselink.allow-zero-id" value="true"/>


This property simply tells EclipseLink to treat '0' as a valid primary key instead of null.

8 comments:

  1. Thanks a lot.. Straight n simple. It worked !!

    ReplyDelete
  2. A little jewel. thanks for sharing it.

    ReplyDelete
  3. Thank you man, it works!!!

    ReplyDelete
  4. Wow, thank you so much.....i spend a few hours looking for that problem...

    ReplyDelete
  5. Two and a half years after your post I have encountered the problem and cannot thank you enough for for the time I could not afford to waste that you saved!

    ReplyDelete