Hello,
I have a Spring based DAO with an xml file that instantiates all the DAO like:
The file spring-hibernate-dao.xml contains more than a hundred of such bean definitions, one for each table of the schema.Code:<bean id="addressDao" class="com.thalasoft.learnintouch.core.dao.hibernate.AddressHibernateDao"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
This has worked fine when the DAO layer was targeting only one database server, namely, the MySql database server.
But the DAO layer must now also target the Oracle database server.
As it happens, this database server has an SQL syntax that is not exactly compatible with the other one, even if using Hibernate.
Therefore some Oracle database server specific DAO beans are required to instantiate some DAO with the Oracle SQL syntax.
There is an Oracle custom DAO:
At first, I thought about injecting into the original DAO, the Oracle custom DAO when needed, with an autowired injected custom DAO like:Code:<bean id="navbarLanguageCustomDao" class="com.thalasoft.learnintouch.core.dao.oracle.hibernate.NavbarLanguageCustomHibernateDao"> <property name="sessionFactory" ref="sessionFactory" /> </bean>
But this becomes problematic if a third database server is later targeted, as it may not implement the same set of DAO methods, resulting in the call to a non implemented method.Code:@Autowired(required=false) private NavbarLanguageCustomDao navbarLanguageCustomDao; public void setNavbarLanguageCustomDao(NavbarLanguageCustomDao navbarLanguageCustomDao) { this.navbarLanguageCustomDao = navbarLanguageCustomDao; } public List<NavbarLanguage> findWithNavbar(Navbar navbar) { if (navbarLanguageCustomDao != null) { return navbarLanguageCustomDao.findWithNavbar(navbar); } else { Criteria criteria = getSession().createCriteria(getPersistentClass()); criteria.add(Restrictions.eq("navbar", navbar)).addOrder(Order.asc("languageCode")); return criteria.list(); } }
The inheritance mechanism does not have this potential problem. It would be simple to have the custom DAO inherit from the original one.
But the DAO beans being defined need to have one constant id whatever the database server
Because the DAO beans are injected in an integration test suite, I cannot have beans with varying ids. For example I cannot have for the MySql database server a bean with the id navbarLanguageDao and for the Oracle database server a bean with the id navbarLanguageOracleDao.
This means loading one xml bean configuration file for each database server. This is easly done with a Maven profile.
But then, each of these files would have lots of copy pasted bean definitions, for all those beans that happen to be compatible DAOs and don't require any custom DAO.
I wonder if there would be away to avoid this copy paste of so many DAO bean definitions.
I know about bean inheritance, but this does not seem to offer a reusable id attribute, or does it ?
What would be nice would be to have one bean definition overwriting another one, with the two beans having the same id, and only the child one being seen. Is that feasible ?


Reply With Quote
