I have an entity with an embedded key. The entity has only the key as a field and the key has 7 fields, some of which can be null.
When I run the following query:
In general it's probably not a good idea to have nullable columns as a part of composite id. In my case it was required because I had a view created using outer join and I faced exactly the same problem as you described. I was able to solve it using information provided on https://hibernate.atlassian.net/browse/HHH-1109. I used NullableStringType provided in the attachment to this Hibernate Jira. In my composite id class I used:
@Type(type = "nl.pinkroccade.quarant.common.model.hibernate.type.NullableStringType")
private String nullableField;
As user405935 said, it is not a good idea to have nullable columns as a part of your composite key.
I was in the same situation as yours and the problem was that the entries from the table had NULL values on the columns that took part from my composite key, so it couldn't create my embedded key at runtime.
Solutions:
You are facing issue because you have included columns which may have null value. Do no include nullable columns in composite key. By RDBMS definition a key must be not null. Rethink the columns which can uniquely identify each row and make them part of your composite key.
I had the same issue no exception and a list of nulls the same size as the query result. I started commenting out stuff and comparing to another EmbeddedId JPA. Here is what I found out, all columns in the EmbeddedId must be non null add nullable = false to Columns on the @AttributeOverride and in the @Embedded id class. Remove null columns from the PK class.
I don't know why this works but it did.