透過JPA註解獲取某個類主鍵欄位

吕金林發表於2024-04-11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public String getPkColumn(String className) {
String pkColumn = null;
try {
Class clazz = Class.forName(className);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if(field.getAnnotation(Id.class) != null){
Annotation annotation = field.getAnnotation(Column.class);
pkColumn = ((Column)annotation).name();
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
return pkColumn;
}
}

  

相關文章