Reputation: 10493
I have a method that takes a list of entities (Class
es) and does some computation. To describe my needs here is the simplest outline of the method (pseudo-code):
public void do(List<Class<?> entities) {
for (Class<?> entity : entities) {
List<?> list = session.createCriteria(entity).list();
for (Object o : list) {
System.out.println(o.getClass().getSimpleName() + " " + o.getId());
}
}
}
Is there a way that I can access/get the Id of o
?
Upvotes: 2
Views: 638
Reputation: 35848
Well, maybe it'll get many critics but all your entity classes may implement this interface:
public interface EntityWithId {
Integer getId();
void setId(Integer id);
}
If your id's are not integers may be the interface could be:
public interface EntityWithId<T> {
T getId();
setId(T id);
}
public class Entity implements EntityWithId<String> {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
.
.
.
}
Anyway, I don't see why you'd want to get the IDs of all your entities, it's a strange solution for a strange requirement, hehe.
Upvotes: 1
Reputation: 19330
Not unless you have the property named the same on all entities, then you could use reflection to call the getter for that property. If they are all named something different, then you would get a NoSuchMethodException
Upvotes: 0