Get actual type of generic type argument on abstract superclass

It’s definitely possible to extract it from Class#getGenericSuperclass() because it’s not defined during runtime, but during compiletime by FooDao extends BaseDao<Foo>. Here’s a kickoff example how you could extract the desired generic super type in the constructor of the abstract class, taking a hierarchy of subclasses into account (along with a real world use case … Read more

Single DAO & generic CRUD methods (JPA/Hibernate + Spring)

Here is an example interface: public interface GenericDao<T, PK extends Serializable> { T create(T t); T read(PK id); T update(T t); void delete(T t); } And an implementation: public class GenericDaoJpaImpl<T, PK extends Serializable> implements GenericDao<T, PK> { protected Class<T> entityClass; @PersistenceContext protected EntityManager entityManager; public GenericDaoJpaImpl() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass … Read more

Data access object (DAO) in Java

The Data Access Object is basically an object or an interface that provides access to an underlying database or any other persistence storage. That definition from: http://en.wikipedia.org/wiki/Data_access_object Check also the sequence diagram here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html Maybe a simple example can help you understand the concept: Let’s say we have an entity to represent an employee: public … Read more

JSF Controller, Service and DAO

Is this the correct way of doing things? Apart from performing business logic the inefficient way in a managed bean getter method, and using a too broad managed bean scope, it looks okay. If you move the service call from the getter method to a @PostConstruct method and use either @RequestScoped or @ViewScoped instead of … Read more