从休眠中的 sessionfactory 中检索实体 class 的主键

Retrieve primary key from entity class from sessionfactory in hibernate

我正在使用休眠创建一个 SessionFactory,我需要与从 SessionFactory 生成的实体 类 关联的所有表的主键。有什么办法可以实现吗?

我已经创建了 SessionFactory 并从中收集了 ClassMetaData。但是无法从 ClassMetaData 中检索主键。

我不知道你的 Hibernate 是哪个版本。这适用于版本 4。2.x:

Configuration con = // get the org.hibernate.cfg.Configuration
for(Iterator<PersistentClass> itpc = con.getClassMappings();itpc.hasNext();)
{
    PersistentClass pc = itpc.next();
    System.out.println(pc.getEntityName() + ", " + pc.getNodeName());
    System.out.println("Identifier(s):");
    Property idpy = pc.getIdentifierProperty();
    for(Iterator<?> itpy = idpy.getColumnIterator();itpy.hasNext();)
    {
        Object o = itpy.next();
        if(o instanceof Column)
        {
            Column c = (Column)o;
            System.out.println(c.getName());
        }
    }
}