2012-03-21 74 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是不是我的专业,因为它们都不是休眠,所以可能是一个更好,更简洁的方式来处理这个(我希望如此,至少),但我找不到它。

相关问题