Null list returned from hibernate query with embedded id

大兔子大兔子 提交于 2019-11-29 09:57:22

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;

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.

Techflash

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.

jimy

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:

  1. make those columns non-nullable, or
  2. add a new column used as primary key, or
  3. find another embedded key using only non-nullable columns
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!