Java 在hibernate中从sessionfactory的实体类检索主键

Java 在hibernate中从sessionfactory的实体类检索主键,java,hibernate,jpa,sessionfactory,Java,Hibernate,Jpa,Sessionfactory,我正在使用hibernate创建一个SessionFactory,我需要与从SessionFactory生成的实体类关联的所有表的主键。有没有办法做到这一点 我已经创建了SessionFactory并从中收集了类元数据。但是无法从类元数据中检索主键。我不知道您使用的是哪个Hibernate版本。这适用于4.2.x版: Configuration con = // get the org.hibernate.cfg.Configuration for(Iterator<PersistentC

我正在使用hibernate创建一个SessionFactory,我需要与从SessionFactory生成的实体类关联的所有表的主键。有没有办法做到这一点


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

我不知道您使用的是哪个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());
        }
    }
}
Configuration con=//获取org.hibernate.cfg.Configuration
for(迭代器itpc=con.getClassMappings();itpc.hasNext();)
{
PersistentClass pc=itpc.next();
System.out.println(pc.getEntityName()+”,“+pc.getNodeName());
System.out.println(“标识符):”;
属性idpy=pc.getIdentifierProperty();
for(迭代器itpy=idpy.getColumnIterator();itpy.hasNext();)
{
对象o=itpy.next();
if(o列的实例)
{
列c=(列)o;
System.out.println(c.getName());
}
}
}

是。。。这实际上有助于。。。非常感谢@user7399085