I have an entity that has a self-join like following.
@Entity
public class DeviceEntity {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@OneToOne(cascade = {PERSIST}, fetch = LAZY)
private DeviceEntity predecessor;
//Some other fields
}
At this point, I have two records in the db like the following.
| id | predecessor_id | … |
| ——– | – | — |
| 1 | null |… |
| 2 | 1 |… |
I am trying to fetch some devices using the following query:
em.createQuery("SELECT s FROM DeviceEntity d " +
" WHERE d.id IN :idList", DeviceEntity.class)
.setParameter("idList", deviceIdList)
.getResultList();
Earlier, I was using Hibernate 5.6.15.Final
, and this query fetched both entities when [1, 2] was passed as the idList. However, now it fetches the device with the 2nd ID as a proxy object. I have enabled Hibernate logs and noticed there is a database hit as well.
Changing the query to fetch the predecessor explicitly has solved the issue, but I’m looking for a better solution that doesn’t involve fetching unnecessary data. How can I resolve this issue efficiently in the upgraded 6.1.7.Final
version?