PersistenceService:
why is the interface sometimes used, and sometimes the DBPersistenceService? If you cast directly to DBPersistenceService, interfaces are of little use (especially if you depend on them to make them configurable).
Now I have to make a subclass of DBPersistenceService to avoid ClassCastExceptions, so not relying on the interface usage anymore.
public void setSessionFactory(SessionFactory sessionFactory) { PersistenceService persistenceService = getPersistenceService(); if (persistenceService==null) return; persistenceService.setSessionFactory(sessionFactory); } /** * gets the hibernate session from the default configured persistence * service. * @throws ClassCastException if another persistence service is configured then the default. */ public Session getSession() { DbPersistenceService persistenceService = (DbPersistenceService) getPersistenceService(); if (persistenceService==null) return null; return persistenceService.getSession(); } /** * sets the hibernate session into the default configured persistence * service, preventing the creation of a session from the configured * session factory (if there is one configured). * @throws ClassCastException if another persistence service is configured then the default. */ public void setSession(Session session) { DbPersistenceService persistenceService = (DbPersistenceService) getPersistenceService(); if (persistenceService==null) return; persistenceService.setSession(session); }
I hope the message was clear. We design by interfaces, we inject by interfaces. So using a class instead of a defined interface totally breaks this concept.