1

I have an Order entity and table and inside the entity Order class, I have this:

@ManyToOne
@JoinColumn(name = "CUST_COMP_ID")
private CustomerCompany custCompany;

public CustomerCompany getCustCompany() {
    return custCompany;
}

public void setCustCompany(CustomerCompany _custCompany) {
    this.custCompany = _custCompany;
}

In the DB, the column CUST_COMP_ID is a FK from Order to another table. The problem is that the DB table Order contains zeroes (in that FK column) when it means NULLs which is a pretty non-standard way to store NULLs (i.e. empty FK values).

Can I somehow tell Hibernate to map those zero values so that when the FK value in the DB is zero, the customerCompany field/property is loaded/populated as null by hibernate in the Order entity?


While researching, I found this old Hibernate forums topic:

https://forum.hibernate.org/viewtopic.php?f=1&t=933785

but I have 2 problems with it: 1) I don't know how exactly to extend ManyToOneType to achieve this; 2) I don't know if this information is valid and up-to-date at all (the topic is very old).

I guess there should be some simpler, more elegant way to achieve my goal.

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • if it has "0" in the FK column, then there is an associated record in the other table with id "0" ? or is it not set as an actual "FOREIGN KEY"? – Neil Stockton Feb 15 '15 at 19:21
  • @NeilStockton Oh, sorry. Sure. Actually I was misled, there is just an index named `FK_Order_CustomerCompany`. There's no actual foreign key there. When I script the table definition I see just a KEY as noted here: http://stackoverflow.com/questions/924265/what-does-the-key-keyword-mean – peter.petrov Feb 16 '15 at 08:13

0 Answers0