在休眠状态下从sessionfactory检索实体类的主键 [英] Retrieve primary key from entity class from sessionfactory in hibernate
本文介绍了在休眠状态下从sessionfactory检索实体类的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用休眠方式创建一个SessionFactory,我需要与从SessionFactory生成的实体类相关联的所有表的主键.有什么办法可以做到这一点?
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?
我已经创建了SessionFactory,并从收集的ClassMetaData中创建了.但是无法从ClassMetaData中检索主键.
I have created SessionFactory and from the gathered the ClassMetaData. But unable to retrieve the primary key from the ClassMetaData.
推荐答案
我不知道您拥有哪个Hibernate版本.这适用于4.2.x版:
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());
}
}
}
这篇关于在休眠状态下从sessionfactory检索实体类的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文