Retrieve primary key from entity class from sessionfactory in hibernate

元气小坏坏 提交于 2021-01-29 13:21:32

问题


I am creating one SessionFactory using hibernate and I need the primary key of all the tables associated with the entity classes generated from the SessionFactory. Is there any way to achieve this?

I have created SessionFactory and from the gathered the ClassMetaData. But unable to retrieve the primary key from the ClassMetaData.


回答1:


I don't know which Hibernate version you have. This works for version 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());
        }
    }
}


来源:https://stackoverflow.com/questions/58510561/retrieve-primary-key-from-entity-class-from-sessionfactory-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!