-->
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.  [ 2 posts ] 
Author Message
 Post subject: Legacy database and ManyToOne association
PostPosted: Thu Jul 31, 2008 8:43 pm 
Newbie

Joined: Wed Jul 30, 2008 1:54 pm
Posts: 11
I have a difficult problem with a ManyToOne association. I'm having to map against a legacy database that I cannot alter or change data inside.

In this database, numeric foreign keys are not null if the association does not exist, instead they are set to 0.

So, for example:

Code:

@Entity
@Table(name="PROVINCE")
public class Province
{
   @Id
   private Long id = null;
   
   @ManyToOne
   @JoinColumn(name="COUNTRY_ID")
   private Country country = null;
   
   ...
}

@Entity
@Table(name="COUNTRY")
public class Country
{
   @Id
   private Long id = null;
   
   @Column(name="NAME")
   private String name = null;
}


This type of mapping works very well, except when the COUNTRY_ID column in the PROVINCE table is set to 0. The application is assuming that means that the 0 is a null.

However, Hibernate, of course tries to load the Country with an identity of 0. Sadly, that throws an exception, and since I CANNOT change the data in the database, I cannot even add a Country with an identifier of 0, nor can I set all those columns to NULL.

Anyone have any ideas?


Top
 Profile  
 
 Post subject: Implement a UserType
PostPosted: Wed Aug 06, 2008 4:20 am 
Newbie

Joined: Wed Aug 06, 2008 3:57 am
Posts: 7
Location: Malaysia
Take a look at the third example described in the following url: http://i-proving.ca/space/Technologies/ ... +Hibernate


. . . I believe it describes a nice solution to the issue you're having.


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