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

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

<p>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.</p>

[{'answer_id': 70925, 'author': 'Manrico Corazzi', 'author_id': 4690, 'author_profile': 'https://Stackoverflow.com/users/4690', 'pm_score': -1, 'selected': False, 'text': "<p>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?</p>\n\n<p><strong>EDIT:</strong> 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</p>\n"}, {'answer_id': 70933, 'author': 'Andreas Bakurov', 'author_id': 7400, 'author_profile': 'https://Stackoverflow.com/users/7400', 'pm_score': 5, 'selected': True, 'text': '<p>No. Primary keys can not be null.</p>\n'}, {'answer_id': 70934, 'author': 'Paul Shannon', 'author_id': 11503, 'author_profile': 'https://Stackoverflow.com/users/11503', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 70966, 'author': 'Andreas Bakurov', 'author_id': 7400, 'author_profile': 'https://Stackoverflow.com/users/7400', 'pm_score': 0, 'selected': False, 'text': '<p>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).</p>\n'}, {'answer_id': 36640391, 'author': 'Rakesh Patil', 'author_id': 1535849, 'author_profile': 'https://Stackoverflow.com/users/1535849', 'pm_score': 3, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 42247822, 'author': 'BlondCode', 'author_id': 4503189, 'author_profile': 'https://Stackoverflow.com/users/4503189', 'pm_score': 3, 'selected': False, 'text': '<p>Unfortunatly, no. I either had to use a workaround:</p>\n\n<p>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.\nSo 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.\nHope it helps for somebody..</p>\n'}]
['https://Stackoverflow.com/questions/70909', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']