2012-03-21 88 views

回答

7

SessionFactory提供稱爲getClassMetadata()獲得的元數據對象的一類(即ClassMetadata

爲了得到一個實體的標識符的屬性的名稱的方法,使用ClassMetadata.getIdentifierPropertyName()

ClassMetadata employeeMeta = sessionFactory.getClassMetadata(Employee.class); 
System.out.println("Name of the identifier property of the employee entity :" + employeeMeta .getIdentifierPropertyName()); 

要獲取被管實體實例的標識屬性的值,使用ClassMetadata.getIdentifier(Object entity, SessionImplementor session)

例如: 假設您有一個託管實體這是從會議加載TY例如:

List<Employee> employeeList = (List<Employee>)session.createQuery("from Employee where gender ='F'").list(); 
ClassMetadata employeeMeta = session.getSessionFactory().getClassMetadata(Employee.class); 
for (Employee employee : employeeList) { 
    System.out.println("Value of the Primary key:" + employeeMeta.getIdentifier(employee , session)); 
} 
+0

正如我剛纔在我自己的答案中輸入的那樣,我已經知道了,並且以同樣的方式(@KenChan)在這裏陳述過,我看到了你的帖子!謝謝! – Shyam 2012-03-21 08:53:18

+0

這裏的會議是什麼類型的會議?我在我的代碼中沒有這樣的東西,我可以告訴(沒有使用getSessionFactory) – 2013-03-16 02:08:12

3

看看PersistentClass(你得到它與configuration.getClassMapping(<classname>))。有getIdentifierProperty()getKeyClosureIterator()可能會有用 - 取決於您的需求。

2

我意識到這個問題是兩個多歲,已經回答了,但它的前10個結果谷歌認定爲「休眠得到的PrimaryKey域」之間(也可能其他類似的查詢),所以我認爲在這裏添加這些信息很重要。

最近,我正在研究一個Java系統,並且我需要獲取表的主鍵,但是,我只有Hibernate Configuration對象和表的名稱。

儘管Johanna的回答非常簡單並且幫助了我,但我發現它無法按預期方式使用具有複合主鍵的表,因爲僅找到組成主鍵的字段之一。

我挖成的PersistentClass性能和已經找到了解決主鍵的這兩種情況下(複合和單)的方式:

/** 
* Maps a table's primary key to a Map<String, String> where the keys are the names of 
* the fields that compose the primary key and the values are the type names of said 
* fields. 
* 
* @param tableName The name of the which for which the primary keys will be gotten 
* @param hibernateConfiguration The Hibernate configuration for the database 
*  IMPORTANT: $tableName must be already mapped in the configuration. 
* @returns A map with the fields names as keys and their types' names as values. 
*/ 
public static Map<String, String> retrievePrimaryKeys(String tableName, Configuration hibernateConfiguration) { 
    hibernateConfiguration.buildMappings(); 

    HashMap<String, String> primaryKeys = new HashMap<>(); 
    PersistentClass tableMapping = hibernateConfiguration.getClassMapping(tableName); 

    Object tableIdentifier = tableMapping.getIdentifier(); 

    if(tableIdentifier instanceof SimpleValue) { 

     // Whenever the identifier is a SimpleValue, it means that the table has only one PrimaryKey. 
     // At this point, it's assumed that the primary key was mapped using the <id> element in the mapping XML file. 
     // We iterate over the columns below, because it's a safer way of handling this thing. 

     SimpleValue tableIdentifierValue = (SimpleValue) tableIdentifier; 
     Iterator primaryKeysIterator = tableIdentifierValue.getColumnIterator(); 

     while(primaryKeysIterator.hasNext()) { 
      Column primaryKey = (Column) primaryKeysIterator.next(); 
      SimpleValue primaryKeyValue = (SimpleValue) primaryKey.getValue(); 
      primaryKeys.put(primaryKey.getName(), primaryKeyValue.getTypeName()); 
     } 
    } 
    else if (tableIdentifier instanceof Component) 
    { 
     // Whenever the identifier is a Component, it means that the table has a composite primary key. 
     // At this point, it's assumed that the primary key was mapped using the <composite-id> element in the mapping XML file 

     Component identifiers = (Component) tableIdentifier; 
     Iterator identifierIterator = identifiers.getPropertyIterator(); 

     while(identifierIterator.hasNext()) { 
      Property identifier = (Property) identifierIterator.next(); 
      SimpleValue value = (SimpleValue) identifier.getValue(); 

      primaryKeys.put(identifier.getName(), value.getTypeName()); 
     } 
    } 
    else 
    { 
     // If the program reaches this point, it means that Hibernate hates you and there's a third unknown way of declaring and getting a table's primary key. 
    } 

    return primaryKeys; 
} 

我必須補充一點,Java是不是我的專業,因爲它們都不是休眠,所以可能是一個更好,更簡潔的方式來處理這個(我希望如此,至少),但我找不到它。