Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate mapping a composite key with null values

Tags:

java

hibernate

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.


2 Answers

No. Primary keys can not be null.

like image 73
Andreas Bakurov Avatar answered Sep 05 '25 09:09

Andreas Bakurov


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.

like image 26
Rakesh Patil Avatar answered Sep 05 '25 10:09

Rakesh Patil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!