-->
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: Relationship field in database contains 0 instead of NULL
PostPosted: Wed Feb 08, 2006 1:46 pm 
Newbie

Joined: Thu Jun 30, 2005 6:58 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.1

Hibernate Annotations: 3.1beta8

I'm working with a legacy database that for some stupid reason sometimes uses zeroes in fields that link records to parents in the same table, where 0 actually means null (i.e. the record has no parent - there is no record with an id of 0).

Obviously, there is no foreign-key defined on the database for this particular relationship (since it would fail), however I want to map this relationship as a many-to-one with a fetch type of EAGER. Unsurprisingly, at present this generates an UnresolvableObjectException, since there is no parent row with an id of 0.

What is the best way to tell Hibernate that zeroes should actually be treated as nulls (and nulls should be persisted as zero)? Note that I am using Hibernate Annotations rather than a mapping file, so I'm not sure how or if I can use the custom SQL for loading option, assuming that this is the preferred approach.

I'd like to avoid mapping files, for the simple reason that the legacy database has approximately 180 tables, which is a lot of XML...

Many Thanks!

_________________
Adam


Top
 Profile  
 
 Post subject: Re: Relationship field in database contains 0 instead of NUL
PostPosted: Wed Feb 08, 2006 2:56 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
agardiner wrote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.1

Hibernate Annotations: 3.1beta8

I'm working with a legacy database that for some stupid reason sometimes uses zeroes in fields that link records to parents in the same table, where 0 actually means null (i.e. the record has no parent - there is no record with an id of 0).

Obviously, there is no foreign-key defined on the database for this particular relationship (since it would fail), however I want to map this relationship as a many-to-one with a fetch type of EAGER. Unsurprisingly, at present this generates an UnresolvableObjectException, since there is no parent row with an id of 0.

What is the best way to tell Hibernate that zeroes should actually be treated as nulls (and nulls should be persisted as zero)? Note that I am using Hibernate Annotations rather than a mapping file, so I'm not sure how or if I can use the custom SQL for loading option, assuming that this is the preferred approach.

I'd like to avoid mapping files, for the simple reason that the legacy database has approximately 180 tables, which is a lot of XML...

Many Thanks!


I don't know if there is an Annotation equivalent but... in the XML mapping the <many-to-one> mapping contains an attribute not-found="ignore|exception" with a default of exception. If you specify ignore, it will ignore missing FK associations.

Just looked at the Annotation docs...

Code:
    @ManyToOne
    @NotFound(action=NotFoundAction.IGNORE)
    public Parent getParent() { ... }

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Relationship field in database contains 0 instead of NUL
PostPosted: Thu Feb 09, 2006 6:00 am 
Newbie

Joined: Thu Jun 30, 2005 6:58 am
Posts: 5
Thanks Preston, that's solved the main part of the problem.

Cheers!

_________________
Adam


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.