Update SnapAdmin version and improve foreign key mapping logic

Bumped the `VERSION` constant from 0.4.1 to 0.6.2. Enhanced the `mapForeignKeyType` method to scan the entire class hierarchy for `@Id` annotations, ensuring better support for inherited entity structures.
This commit is contained in:
dalbodeule 2025-05-24 19:09:12 +09:00
parent 61ff240456
commit 9963f504c5
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65

View File

@ -89,7 +89,7 @@ public class SnapAdmin {
private boolean authenticated; private boolean authenticated;
private static final String VERSION = "0.4.1"; private static final String VERSION = "0.6.2";
/** /**
* Builds the SnapAdmin instance by scanning the `@Entity` beans and loading * Builds the SnapAdmin instance by scanning the `@Entity` beans and loading
@ -396,25 +396,32 @@ public class SnapAdmin {
* @return * @return
*/ */
private DbFieldType mapForeignKeyType(Class<?> entityClass) { private DbFieldType mapForeignKeyType(Class<?> entityClass) {
try { try {
Object linkedEntity = entityClass.getConstructor().newInstance(); Object linkedEntity = entityClass.getConstructor().newInstance();
Class<?> linkType = null; Class<?> clazz = linkedEntity.getClass();
Field idField = null;
for (Field ef : linkedEntity.getClass().getDeclaredFields()) { // 상속 계층 전체를 스캔하여 @Id 필드 탐색
if (ef.getAnnotationsByType(Id.class).length != 0) { while (clazz != null && clazz != Object.class) {
linkType = ef.getType(); for (Field ef : clazz.getDeclaredFields()) {
} if (ef.getAnnotationsByType(jakarta.persistence.Id.class).length != 0) {
} idField = ef;
break;
}
}
if (idField != null) break;
clazz = clazz.getSuperclass();
}
if (linkType == null) if (idField == null)
throw new SnapAdminException("Unable to find @Id field in Entity class " + entityClass); throw new SnapAdminException("Unable to find @Id field in Entity class hierarchy: " + entityClass);
return DbFieldType.fromClass(linkType).getConstructor().newInstance(); return DbFieldType.fromClass(idField.getType()).getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) { | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new SnapAdminException(e); throw new SnapAdminException(e);
} }
} }
public boolean isAuthenticated() { public boolean isAuthenticated() {
return authenticated; return authenticated;