これが、MrLore ソリューションと Sotirios Delimanolis ソリューションの組み合わせである私の最終的なソリューションになりました。このソリューションは、条件を使用せずに完全に動的です。
このクラスは、プロパティ名を渡すことによって名前の検索を実行します。
String propertyName = "vehicleYear";
vehicleConfiguration.getInfo(propertyName).getName()
propertyName = "vehicleMake";
vehicleConfiguration.getInfo(propertyName).getName()
このクラスは VehicleConfiguration を表します
@Entity
public class VehicleConfiguration extends StatefulEntity {
@ManyToOne
@JoinColumn(name = "year_id")
private VehicleYear vehicleYear;
@ManyToOne
@JoinColumn(name = "make_id")
private VehicleMake vehicleMake;
public LookupBaseEntity getInfo(final String fieldName) {
try {
String methodName = WordUtils.capitalize(fieldName);
Method method = VehicleConfiguration.class.getMethod("get" + methodName);
return (LookupBaseEntity) method.invoke(this);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(VehicleConfiguration.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
このクラスは VehicleYear を表します
@Entity
public class VehicleYear extends LookupBaseEntity {
}
このクラスは VehicleMake を表します
@Entity
public class VehicleMake extends LookupBaseEntity {
}
どちらも LookupBaseEntity を拡張します
public class LookupBaseEntity extends StatefulEntity {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}