Back Forum Reply New

using both DAO and ORM in the Spring framework

Sense I am new to the Spring Forum and unsure which sub-forum to post a question to regarding architecture and/or data acccess I am posted the same question here and in Architecture Discussion.  

We are starting a new development project and want to use the best of breed technologies.  We do have a business requirement which requires we get data from multiple Oracle databases.  We want to use Spring with Hibernate for our data access layer.  It is our understanding that we have to have a one-to-one mapping with data objects (tables or views) in the database.  Some of the business rules will need data from different databases and tables (based on the same primary key) to support the business process.

Has anyone used Spring, Hibernate, and DAO (sql statements through JDBC) together?  If so, how difficult is it?  What are the pit falls?  Does this even make sence?  Are there any OLTP systems using this approach?

Need less to say, I am a newbie...!

Thanks in advance....

Hello.

I've used Spring+Hibernate+Spring JDBC in a project with a model consisting of about 80 beans.

It's quite simple, you have to have two datasources defined in your appContext, and configure your JDBC-DAO and ORM-DAO objects to use the appropriate one.

sample JDBC:Code:
lt;bean id=quot;jdbcTemplatequot;
class=quot;org..jdbc.core.JdbcTemplatequot;gt;
lt;descriptiongt;
This is declaration of JdbcTemplate instance which will be
used by DAOs for database access via JDBC and configured
data source.
lt;/descriptiongt;
lt;property name=quot;dataSourcequot;gt;
lt;ref bean=quot;jdbcDataSourcequot; /gt;
lt;/propertygt;
lt;/beangt;lt;bean id=quot;daoConfigTemplateJDBCquot; abstract=quot;truequot;gt;
lt;descriptiongt;
This is template for DAO configurations. Template abandons
the need for specifying a 'jdbcTemplate' property in every
DAO configuration so overall complexity of configuration is
somewhat reduced. To use this template all DAOs
configurations must declare it via 'parent' attribute.
lt;/descriptiongt;
lt;property name=quot;jdbcTemplatequot;gt;
lt;ref bean=quot;jdbcTemplatequot; /gt;
lt;/propertygt;
lt;/beangt;

lt;bean id=quot;someDaoJdbcquot; class=quot;some.package.JdbcDaoquot;       parent=quot;daoConfigTemplateJDBCquot;/gt;
sample ORM:

Code:
lt;bean name=quot;sessionFactoryquot; id=quot;MySessionFactoryquot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot;gt;
   lt;property name=quot;mappingDirectoryLocationsquot;gt;
lt;listgt;
lt;valuegt;classpath:/some/model/packagelt;/valuegt;
lt;/listgt;
lt;/propertygt;
   lt;property name=quot;hibernatePropertiesquot;gt;     lt;propsgt;       .....some props     lt;/propsgt;   lt;/propertygt;   lt;property name=quot;dataSourcequot;gt;     lt;ref bean=quot;ormDataSourcequot;/gt;   lt;/propertygt;

lt;/beangt;lt;bean id=quot;daoConfigTemplateHibernatequot; abstract=quot;truequot;gt;   lt;property name=quot;sessionFactoryquot;gt;     lt;ref bean=quot;MySessionFactoryquot;/gt;       lt;/propertygt;
lt;/beangt;

lt;bean id=quot;someDaoOrmquot; class=quot;some.package.OrmDaoquot; parent=quot;daoConfigTemplateHibernatequot;/gt;
Hope this helps...

Cheers!

Yes, it is perfectly possible, and actually very common

Things to consider though is that if you wish to use both DAOs in the same transaction, most ORM tools (at least Hibernate) will cache the SQL and *not* send it to the DB until it deems it necessary, so you may need to do an explicit session.flush() in the Hibernate DAO.

Have you checked out the excellent Spring reference manual? docs/reference/jdbc.html
¥
Back Forum Reply New