簡體   English   中英

從 hibernate 中的 sessionfactory 中檢索實體 class 的主鍵

[英]Retrieve primary key from entity class from sessionfactory in hibernate

我正在使用 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());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM