31

With Hibernate, can you create a composite ID where one of the columns you are mapping to the ID can have null values?

This is to deal with a legacy table that has a unique key which can have null values but no primary key.

I realise that I could just add a new primary key column to the table, but I'm wondering if there's any way to avoid doing this.

  • I would like to know if this is also possible, my database (OpenEdge) doesn't support views and I can not add new fields to the table, how did you overcome this? – Brett Ryan Sep 08 '10 at 08:42
  • they might update the javadoc in the future https://hibernate.atlassian.net/browse/HHH-14535 – tremendous7 Jul 20 '21 at 13:12

6 Answers6

15

No. Primary keys can not be null.

Andreas Bakurov
  • 1,535
  • 1
  • 10
  • 10
  • 3
    Our customer provides a view without any primary key. For Hibernate I have to define a PK. But the only unique combination of fields includes a nullable field. – StefanTo Nov 18 '16 at 08:29
  • can we have a solution to ignore entries with null values ? – Amine ABBAOUI May 16 '23 at 10:55
8

You wont get error but Hibernate wont be able to map those rows with NULL value for composite column to your Entity. That means you get entity with NULL values in result.

Rakesh Patil
  • 370
  • 4
  • 12
6

Unfortunatly, no. I either had to use a workaround:

I used composit Id for a view(! not table) where rows can be identified by 2 cols exactly (A, B). Although one of the cols (B) can have null values as well as positive integers. So my workaround is that i created a new col in the view: "BKey" and my view is written as if B is null then value of BKey is -1 else BKey = B. (Only positive integers occour in B and null). I also changed my composit id implementation to use BKey instead of B. Hope it helps for somebody..

BlondCode
  • 4,009
  • 1
  • 19
  • 18
0

This is not advisable. Could you use a view and map that instead? You could use COALESCE to supply a default if you are stuck with legacy data. We had lots of trouble with composite keys and I imagine null values will cause even more issues.

Paul Shannon
  • 1,145
  • 10
  • 15
0

For composite keys (assumed that database allows nulls in PKs) you can have maximum number_of_cols^2 - 1 entries containing nulls, (for example for composite key of 2 columns you can have 3 rows having in their primary key null, the fourth is the PK without nulls).

Andreas Bakurov
  • 1,535
  • 1
  • 10
  • 10
-2

Why would you want to do that? Your composite ID should map the primary key of your table, and it doesn't sound wise to put null values in a key, does it?

EDIT: Hibernate does not allow to do so; you might put the property outside the key and tweak the DAO a little to take the field into account wherever necessary

Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
  • 3
    Mapping to legacy tables is one reason why this might be required. I have a case where this is also required and the DB does not support views (Progress OpenEdge). – Brett Ryan Sep 08 '10 at 08:44
  • 1
    I am having the same problem. I have a @Embedded with 4 attributes (this is a view and I cannot control it). And some of them can be null, but the composition of them cannot be the same (ford,red,4,4), (ford,red,4,null) for example. How can I do? – Rafael Andrade Aug 06 '19 at 02:08