aspose file tools*
The moose likes Object Relational Mapping and the fly likes Get primary key field name from Toplink Entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Get primary key field name from Toplink Entity" Watch "Get primary key field name from Toplink Entity" New topic
Author

Get primary key field name from Toplink Entity

allareddy suneel
Greenhorn

Joined: Sep 18, 2012
Posts: 13
I am trying list down all entities along with primary key fields from my project. I am able to get entity/model class names properly using below mentioned code snippet. I need to get primary key fields from each model but i am getting Database primary key but not java field names. Any help appreciated.


Hashtable descriptors = session.getProject().getDescriptors();
Enumeration descriptorsIterator = descriptors.elements();
TreeMap<String,String> entityMap = new TreeMap<String,String>();
while (descriptorsIterator.hasMoreElements()) {
Descriptor entityObj = (Descriptor) descriptorsIterator.nextElement();
entityMap.put(entityObj.getAlias(), entityObj.getJavaClass().getName());//get the entity name

}

I am using Toplink 9.0.3.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You can get the mapping (attribute) for any database field (column) using,

descriptor.getMappingForAttributeName(field).getAttributeName()

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get primary key field name from Toplink Entity