Null list returned from hibernate query with embedded id

前端 未结 4 1242
独厮守ぢ
独厮守ぢ 2020-12-31 12:52

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:

相关标签:
4条回答
  • 2020-12-31 13:19

    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;
    
    0 讨论(0)
  • 2020-12-31 13:24

    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
    0 讨论(0)
  • 2020-12-31 13:28

    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.

    0 讨论(0)
  • 2020-12-31 13:34

    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.

    0 讨论(0)
提交回复
热议问题