We help IT Professionals succeed at work.

spring - hibernate best practises

royjayd
royjayd asked
on
hi guys,

i am building a real time application and was just trying to understand whats a good way to impliment it in spring and hibernate.
I was reading this article on it:

http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-1.html

This above article uses Hibernate objects as Spring Beans, so that means hibernate objects will be defined in spring configuration file as beans.

But my application architecture is this:

jsp--> SpringConfigFlie --> Validatior calss ---->Controller class-->  business delegate->dao-->hibernate-->DB

Therefore in my architecture i am not calling hibernate until i reach the DAO.
comparing my architecure with the one in article, which one is better  and why?

Also in the article the database configuration is done in spring config file like this
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost/samples"/>
    <property name="username" value="root"/>
    <property name="password" value="pwForRoot"/>
</bean>

Is that good way to do it? I thought the database configuration is written in the hibernate config file (hibernate.cfg.xml).

There are too many resources on the web on spring-hibernate. Does anyone know how it is actually implimented in real tme projects ?

any responces/inputs will be greatly appreciated.
thanks.
Comment
Watch Question

Mick BarryJava Developer
Top Expert 2010
Commented:
> Is that good way to do it? I thought the database configuration is written in the hibernate config file (hibernate.cfg.xml).

you're confusing hibernate configuration with data source definition
that data source definition is used by the (hibernate) session manager to know what database to connect to. The session manager would also have hibernate mapping config
Mick BarryJava Developer
Top Expert 2010

Commented:
we use jpa to abstract away the use of hibernate, and also annotations to define the mapping
see the following example

http://www.zabada.com/tutorials/hibernate-and-jpa-with-spring-example.php

Author

Commented:
hi Objects,
so are you saying we need to define the datasource in
spring config file always(like they did in that article)

thanks
Mick BarryJava Developer
Top Expert 2010

Commented:
yes the data source needs to be defined somewhere, it defines how to get a connection to the database
if you look in the session factory definition in that example you'll see that it use the defined data source
>>> Therefore in my architecture i am not calling hibernate until i reach the DAO.
comparing my architecure with the one in article, which one is better  and why?


What is the need for calling hiberante before DAO . Hiberante use for accessing the DB that all throw DAO only u can access the DB.  That architecture  is gd only .
 
>>>>>>Is that good way to do it? I thought the database configuration is written in the hibernate config file (hibernate.cfg.xml).
>>>>so are you saying we need to define the datasource in
spring config file always(like they did in that article)

Thats is depend upon you.
but correct way is define in hiberanteConfig file only . then only it create the sessionFactory.
if u write the dataSource code in hiberanteConfig means u can put an entry in spring Config file .Where the hbm file located like that .