views:

1566

answers:

2

Hi,

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

+1  A: 

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 http://stackoverflow.com/questions/70909/hibernate-mapping-a-composite-key-with-null-values which came up as #3 when I googled "null composite key".

Kevin Crowell
Notice that I can't put the non-nullable column as PK, since the column itself is not unique, only the combination of columns. I know this is bad design, but I don't have control over the DB model.
Miguel Ping
Since the combination of the columns is always unique (wether there are null values or not) and the set of columns is never null (because of the not-nullable column) I thought that this could be done.
Miguel Ping
Databases are not supposed to allow this, but some do. It is unfortunate that you do not have control over the database schema. Are you able to talk to the people that are in control of the schema?Sorry, but I do not know of a workaround/hack to make this work with hibernate.
Kevin Crowell
Thanks for all the help, but unfortunately I do not have control over the schema, but I'll try and see if the owners can add a true PK.
Miguel Ping
+1  A: