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?