if (hibernateMetadata == null)
{
// Component class (persistent but not metadata) : no associated id
// So must be considered as transient
//
throw new TransientHibernateObjectException(pojo);
}
// Retrieve ID
//
Serializable id = null;
if (hibernateClass.equals(pojo.getClass()))
{
// the pojo has the same class, simple use metadata
//
id = hibernateMetadata.getIdentifier(pojo, EntityMode.POJO);
}
else
{
// DTO case : invoke the method with the same name
//
String property = hibernateMetadata.getIdentifierPropertyName();
try
{
// compute getter method name
property = property.substring(0,1).toUpperCase() +
property.substring(1);
String getter = "get" + property;
// Find getter method
Class<?> pojoClass = pojo.getClass();
Method method = pojoClass.getMethod(getter, (Class[])null);
if (method == null)
{
throw new RuntimeException("Cannot find method " + getter + " for Class<?> " + pojoClass);
}
id = (Serializable) method.invoke(pojo,(Object[]) null);
}
catch (Exception ex)
{
throw new RuntimeException("Invocation exception ", ex);
}
}
// Post condition checking
//
if (id == null)
{
throw new TransientHibernateObjectException(pojo);
}
return id;
}