我们是否可以通过编程方式找到Hibernate实体的主键字段(类似于JPA的PersistenceUnitUtil
)?查找Hibernate实体的主键字段
3
A
回答
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));
}
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是不是我的专业,因为它们都不是休眠,所以可能是一个更好,更简洁的方式来处理这个(我希望如此,至少),但我找不到它。
相关问题
- 1. 非主键字段的实体框架外键
- 2. Hibernate:从两个可能的地方查询实体的字段?
- 3. 使用用户类型字段查询hibernate实体
- 4. java hibernate eclipse xml - 使用没有主键的实体
- 5. 如何使用UUID作为Hibernate实体的主键?
- 6. Hibernate寻找错误的主键
- 7. 实体中的外键字段?
- 8. 原则2:通过实体与查找复合主键
- 9. 实体框架代码第一。查找主键
- 10. 主键的独立实体
- 11. Hibernate的外键的主键
- 12. 查询一个特定的多值字段为主要实体
- 13. 实体框架4.1 - 数据库优先方法 - 如何查找主键字段的列名称?
- 14. Hibernate通过非主键获取实体并更新它+ Spring mvc
- 15. Hibernate @GeneratedValue在持久化实体之前不会生成主键
- 16. 实体框架中的主键/外键
- 17. 如何使用两个外键作为Hibernate的实体注释主键
- 18. 主义坚持以前实体为关键实体的实体
- 19. 查找与一个字段匹配的相关实体
- 20. 如何查找openjpa实体分离时加载的字段
- 21. 使用QueryDSL查找唯一字段的实体
- 22. 如何查找插件CRM的相应实体和字段
- 23. 实体框架7(核心)基于组合键查找实体?
- 24. Hibernate的标准内加入全部的实体类的字段
- 25. Hibernate,查找对象字段变量的子字符串
- 26. Hibernate HBM2DDL为主外键生成可空字段?
- 27. Dropwizard Hibernate - 通过不是ID的值查找实体?
- 28. 实体字段查询中的多个实体类型
- 29. JPQL 2.0 - 基于超类实体字段的查询实体
- 30. 具有@Lob字段的实体是否可在Hibernate中缓存?
正如我刚才在我自己的答案中输入的那样,我已经知道了,并且以同样的方式(@KenChan)在这里陈述过,我看到了你的帖子!谢谢! – Shyam 2012-03-21 08:53:18
这里的会议是什么类型的会议?我在我的代码中没有这样的东西,我可以告诉(没有使用getSessionFactory) – 2013-03-16 02:08:12