I have the following method that returns a Person object by Id:
private Person getPersonById(String client) {
Person output = null;
EntityManager entityManager = null;
try {
entityManager = entityManagement.createEntityManager(client);
output = entityManager.find(Person.class, id);
} catch (Exception e) {
// handle
} finally {
entityManagement.closeEntityManager(client, entityManager);
}
return output;
}
Is there a way I can make this method more generic so that I can pass in other object types, e.g.
Place , Cost as well? Note that these do not inherit the same super class.
E.g. should I pass e.g. Person.class as a method parameter etc?
If you want to pass in a class object and make your method generic, you could do this:
If you pass
Person.classas theclsargument, it would be equivalent to your original code.That's supposing that
entityManager.findwill accept any class and attempt to return an instance of that class.If
entityManager.findhas some restriction on the characteristics of the class it accepts, you would need to specify them in your generic type parameter<E>.