1

In our grails application we have a database table with a composite unique key(i.e: one of the columns in the key can be nullable therefore null is a unique value).

My problem is any time we try to retrieve a row where this column has null in it, hibernate returns us a null object. Does anyone know how to get the object when one of the keys are null? Also it returns all of the rows that do not contain the null just fine.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
southpaul
  • 199
  • 1
  • 10
  • Do you have to have a nullable field in your key ? Is there any alternative column to be used instead? http://stackoverflow.com/questions/386040/whats-wrong-with-nullable-columns-in-composite-primary-keys – Alidad Jun 28 '13 at 17:44
  • We do have a nullable field in the key and unfortunately no, this column is required. – southpaul Jun 28 '13 at 19:12
  • 2
    Having null be a unique value is craziness. – Gregg Jun 28 '13 at 22:27
  • Agree with @Gregg, you can't guarantee uniqueness if you permit nulls in any composite key column; I suggest rethinking your approach. – raffian Jun 29 '13 at 02:31
  • Just my 2c here as a strictly database person for a long time. Null is absolutely possible to be part of a unique key. In Oracle you cannot have null values in a primary key, however they can exist in unique keys. I'm not sure how the other databases handle it. – James Kleeh Jun 29 '13 at 05:21
  • @OP - I don't think it's possible given the current Hibernate version. Lord knows I've tried many times. – James Kleeh Jun 29 '13 at 05:21
  • I agree with @James, the purpose of Unique Keys are so that you can have null values as part of the composite key. We'll try to find some work around and let ya'll know what we come up with. – southpaul Jul 01 '13 at 13:59

1 Answers1

0

Try to add another column that you can use as alternate key. then use another value for this for null values. that way you dont have to deal with nulls

JavaDev
  • 442
  • 3
  • 14