[英]Hibernate get all foreign keys for mapped entities
i need to get the column names for foreign keys for all mapped hibernate entities.我需要为所有映射的休眠实体获取外键的列名。 Does anyone know how to do that?
有谁知道这是怎么做到的吗? I tried by sessionFactory.getClassMetadata - i can see all the properties names and types for all entities there, but i cannot find information about foreign keys.
我尝试通过 sessionFactory.getClassMetadata - 我可以看到那里所有实体的所有属性名称和类型,但我找不到有关外键的信息。 Does anyone have any idea?
有谁有想法吗? I may not use direct database query - i must extract it from hibernate metadata.
我可能不会使用直接数据库查询 - 我必须从休眠元数据中提取它。
You can use Java Reflection:您可以使用 Java 反射:
// Loop through joined columns that has @JoinColumn annotation
for (Method method : testClass.getMethods())
{
if (method.isAnnotationPresent(JoinColumn.class))
{
// name parameter is foreign key
String foreignKey = method.getAnnotation(JoinColumn.class).name;
// if the referencedColumnName is explicitly defined
String foreignKey = method.getAnnotation(JoinColumn.class).referencedColumnName;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.