ID getter returns null to Hibernate proxies when an object implements an interface

I use Hibernate and I have a problem: when loading an object using Session.load , the getter identifier returns null .

My objects inherit from the following interface:

 public interface Entity { Object getId(); } 

An example of an object that implements this interface:

 @javax.persistence.Entity @javax.persistence.Table(name = "`Dog`") public class Dog implements Entity { private java.util.UUID id; @javax.persistence.Id @javax.persistence.GeneratedValue(generator = "uuid") @org.hibernate.annotations.GenericGenerator(name = "uuid", strategy = "uuid2") @javax.persistence.Column(name = "`Id`", nullable = false) @Override public java.util.UUID getId() { return this.id; } //... 

The getId getter in the object implements the getId interface, but changes the type of the return value.

When I load this object using Session.load , getId returns null when calling Dog.getId , but not when calling Entity.getId .

 Dog dog = (Dog)session.load(Dog.class, id); assert dog.getId() != null; // This fails assert ((Entity)dog).getId() != null; // This succeeds 

When I trace the execution of dog.getId() , it is not processed by the proxy server, but goes into the actual implementation. Thus, it gets the id field value on Dog , which is null , because it is a proxy.

Why is this?

+1
source share

Source: https://habr.com/ru/post/982426/


All Articles