HongJeongHyeon
HongJeongHyeon

Reputation: 301

AssertionFailure: null identifier when try to save identifying one-to-one relation

I'm trying to make identifying relation with Spring Data JPA.
And following is my (simplified) entities.

@Entity
class AuthorizationCodeEntity(
    @MapsId
    @OneToOne
    @JoinColumn(name = "authorization_id")
    val authorization: AuthorizationEntity
)  {
    @Id
    @field:Size(max = 16)
    val authorizationId: UUID = UUID(0, 0)
}
@Entity
class AuthorizationEntity {
    @OneToOne(mappedBy = "authorization", cascade = [CascadeType.REMOVE])
    val authorizationCodeEntity: AuthorizationCodeEntity? = null
}

But in when I try to save Entity, follow exception occurred.

Caused by: org.hibernate.AssertionFailure: null identifier
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:51) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.generateEntityKey(AbstractSharedSessionContract.java:559) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.type.OneToOneType.isNull(OneToOneType.java:108) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:463) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:458) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.type.EntityType.replace(EntityType.java:359) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
    at org.hibernate.type.AbstractType.replace(AbstractType.java:164) ~[hibernate-core-5.6.8.Final.jar:5.6.8.Final]
...

When I change my PK to Long(AUTO_INCREMENT), Everything goes well.
But with UUID, null identifier Exception occur.
Does anyone know what's the problem with my code?

Upvotes: 0

Views: 2165

Answers (1)

dariosicily
dariosicily

Reputation: 4547

Your code is correct, the problem stands in the val authorizationId: UUID = UUID(0, 0) assignment in your entities: you are always assigning to the UUID field an empty "nil" uuid (all bits set to zero) so hibernate signals about its presence as a null identifier. To solve this issue you can choose to use a GenericGenerator (see for example this answer) to automatically generate a non null UUID for your Entity or everytime manually assign to your field a non null UUID, while there are no issues if you switch your primary key type to Long.

Upvotes: 1

Related Questions