Jpa composite key nullable columns

Miguel Ping picture Miguel Ping · Mar 23, 2009 · Viewed 13.2k times · Source

I'm using Hibernate's JPA impl to model some tables. I'm having trouble mapping a table that:

  • Has no primary key
  • Has a unique index on 4 columns, 3 of which can be nullable

I tried to hack it and define the index as a composite Id, but since some columns are nullable this is not working properly. Is this possible with JPA/Hibernate?

Thanks

Answer

Kevin Crowell picture Kevin Crowell · Mar 23, 2009

It seems that the column that is not nullable should be your primary key. No parts of a composite key should ever be nullable.

You will need to take the nullable properties and place them outside of your primary/composite key.

Also, this looks like a duplicate of Hibernate mapping a composite key with null values which came up as #3 when I googled "null composite key".