Determining the primary key property name using Hibernate

Simple method:

String idPropertyName = sessionFactory.getClassMetadata(User.class).getIdentifierPropertyName();

To get the actual value see this tutorial

Elaboration

Example bean:

 

The property name we’re after is “userId”

To do it manually using reflection please see this tutorial

All comments welcome