Hibernate loading a lazy proxy, but I only need the PK
hibernate, java, jpa
Solution
Hibernate should keep the object's ids linked also to the current `Session`. And there is a getIdentifier() method on the `Session` that should get the id. The documentation is not saying anything about it, but normally it shouldn't initialize the object.
Problem
I have these entities: ``` @Entity public class Room { @ManyToOne(optional=true,fetch=FetchType.LAZY) private Player player1; ... } @Entity public class Player { @Id @Column(updatable=false) private long id; public long getId() { return id; } ... } ``` Now, this statement inside `Room`... ``` player1.getId(); ``` ...will cause that entire `Player` entity to be fetched from the database. However, I just need its primary key, `id`, which it should already have (otherwise how can it fetch the data?). How can I access the lazy `Player` proxy's `id` without triggering database access?