It seems that I missed something using Java Generics, because something that I think is simple, it seems to me that this is impossible. Maybe you can help ...
This is the scenario: I am coding a generic abstract DAO with a simple CRUD operation, so each specific DAO of my application can have it for free:
public abstract DefaultDAO<T,V> { private EntityManager manager; public BaseDAO(EntityManager em) { this.manager = em; } public void create(T entity) { manager.persist(entity); }
Now I would go and implement a specific DAO:
public PersonDAO extends DefaultDAO<PersonEntity, Long> { public PersonDAO(EntityManager em) { super(em); }
And the client code for my DAO will be:
EntityManager manager = ... PersonDAO dao = new PersonDAO(manager); Long pk = ..... PersonEntity person = dao.find(pk);
When the client executes the code, BaseDAO knows the type of object that it should return and the type of the primary key of this object, because I set it on a specific Tao, but I do not know how to encode the read () method correctly.
I hope you can help. Many thanks!
source share