![](/img/trans.png)
[英]How to retrieve sessionFactory from Hibernate 4.3.4.Final?
[英]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.