• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Looking for advice on configuring a new project using Spring and Hibernate.

 
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I currently use Spring, Hibernate at work. I have never built my own Spring, Hibernate project from the ground up. This is what I am currently trying to do. There is a ton of information out there and I am reading everything I can. However, it is a lot of information and much of it is dated. I will keep reading and investigating to try to achieve my goal.

The Project

I wish to create a project using the spring framework with hibernate. My database of choice is MySQL(mostly because it is loaded and running on my server). I have the newest spring tools downloaded and my final objective is to create a link with hibernate and mysql. I am looking for advice on the best way to do this. My understanding so far is to use spring for most of the work. I welcome all opinions and look forward to the replies.

1. Is a hibernate.cfg.xml file required. If not is all the configuation done in the spring data-access-config.xml file?
2. If I want to use hql to communicate with hibernate and have it connect to SQL how is that best configured?
(My understanding is hibernate will translate to most databases abstracting the database to a jdbc connection).
3. What jar files will be needed to make this work?


Thank You.
Greg Funston
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Funston wrote:Hi, I currently use Spring, Hibernate at work. I have never built my own Spring, Hibernate project from the ground up. This is what I am currently trying to do. There is a ton of information out there and I am reading everything I can. However, it is a lot of information and much of it is dated. I will keep reading and investigating to try to achieve my goal.

The Project

I wish to create a project using the spring framework with hibernate. My database of choice is MySQL(mostly because it is loaded and running on my server). I have the newest spring tools downloaded and my final objective is to create a link with hibernate and mysql. I am looking for advice on the best way to do this. My understanding so far is to use spring for most of the work. I welcome all opinions and look forward to the replies.

1. Is a hibernate.cfg.xml file required. If not is all the configuation done in the spring data-access-config.xml file?
2. If I want to use hql to communicate with hibernate and have it connect to SQL how is that best configured?
(My understanding is hibernate will translate to most databases abstracting the database to a jdbc connection).
3. What jar files will be needed to make this work?


Thank You.
Greg Funston



This is probably first reply to this forum... but I've been using Spring for a while now.

1. No, you don't need hibernate.cfg.xml as that can be configured in spring.
2. I would have each of your DAO to extend HibernateDaoSupport. That class can has a field called hibernateTemplate which you can use for HQL.
3. Just put everything you download for Spring core..I think it's around 20+. It really doesn't hurt unless you are really trying to save classloader memory.

Another advice...I've met many individuals recommending "Spring + Hibernate" because someone said so without understanding "why"... I know you're just doing the job told by some architect but really try to understand what Spring would benefit you by integrating with Hibernate. In short, the benefits of using Hibernate w/ Spring is that it handles the boilerplate code for you and making the DAO as "singletons" that can be wired easily on other Spring beans. For example, setting the SessionFactory. Also, Hibernate is technology that allows JDBC logics in Object Oriented Manner (well sort of since it has mysterious thing called HQL). Well, g'luck! I'm sure you'll get it working.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) No, you create a bean of type LocalSessionFactoryBean or AnnotationSessionFactoryBean to create a bean of type SessionFactory and in that bean set properties to set everything that you had had in the hibernate.cfg.xml

3) You will also need some of the Hibernate jars too.


2. I would have each of your DAO to extend HibernateDaoSupport. That class can has a field called hibernateTemplate which you can use for HQL.




Please please do not do this. If you make your DAO extend HibernateDaoSupport then your DAO is now tightly coupled with Spring. Spring itself does not want you to do this.

In the old days of Hibernate before Hibernate 3.1 and before Spring 2.5.6 this was a good idea. Now it is not, you will not gain anything by extending that class.

Make your DAO have a SessionFactory injected into it yourself. Then in the code call sessionFactory.getCurrentSession() to get a Session to run queries in. There is no more Hibernate boiler plate code to hide to even warrant using the HibernateTemplate.

Mark
 
Kevin Cho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:1) No, you create a bean of type LocalSessionFactoryBean or AnnotationSessionFactoryBean to create a bean of type SessionFactory and in that bean set properties to set everything that you had had in the hibernate.cfg.xml

3) You will also need some of the Hibernate jars too.


2. I would have each of your DAO to extend HibernateDaoSupport. That class can has a field called hibernateTemplate which you can use for HQL.




Please please do not do this. If you make your DAO extend HibernateDaoSupport then your DAO is now tightly coupled with Spring. Spring itself does not want you to do this.

In the old days of Hibernate before Hibernate 3.1 and before Spring 2.5.6 this was a good idea. Now it is not, you will not gain anything by extending that class.

Make your DAO have a SessionFactory injected into it yourself. Then in the code call sessionFactory.getCurrentSession() to get a Session to run queries in. There is no more Hibernate boiler plate code to hide to even warrant using the HibernateTemplate.

Mark



That is a good solution as well. Yes, it's true that Spring itself doesn't want to be intrusive in your own code as well but I wouldn't rule it out. For examle, you can use JEE Annotations instead of using Spring Annotations but "with" limited feature. For example, if you want to use Cache Annotations and so forth. I think you're looking at a Spring Purist perspective ;) which is fine but in real world I don't see anyone trying to avoid Spring dependency. For example, if I'm using Spring MVC then should I still follow your approach? I'm already tied w/ Spring. Anyways, I'm 100% sure that I will NEVER switch to another DI then Spring. If there is any possibility of switching to other DI then your approach would make sense.

This kind of reminds me of using JPA instead of using Hibernate on one project and regret it Trying to implement findByExample in JPA was a pain in the ass...yes, I know new JPA spec has that now. Even with Hibernate, I still haven't encounter a case where someone said "Hibernate ORM sucks and we should use other ORM". Yes, I make sacrifice by tying Hibernate dependency in my own code but I gain additional functionalities that's not in JPA.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"which is fine but in real world I don't see anyone trying to avoid Spring dependency"

Actually in the real world those that write good code follow basic OO principles, which have nothing to do with Spring. Loose Coupling and High Cohesion are extremely important for making code that is clean, easy to maintain, easy to change, easy to upgrade, easy to add new features, easy to test. That is why you don't want to extend the DaoSupport class.

As a former SpringSource employee, and working with the Spring Framework, SpringSource developers, as a SpringSource trainer with the Spring training materials, I can tell you that advice is everywhere in SpringSource and their employees. To tell you all not to implement or extend any class from a framework, if at all possible, including Spring's own classes and interfaces.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's make this a little easier.

1) What benefits do you get from extending a DaoSupport class? To me there is zero benefits, and one small disadvantage of coupling your code. Or if you think there are benefits, there are other ways to get the same benefit just as easy or easier without coupling your code.

Mark

 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Thank you for the replies. Okay, I am trying to get this project up and running using best practices. So far I have it wired using annotations. I have a connection to my database but I do not have auto-table creation working. Here is my spring configuration file web-application-conf. I would like to use auto table creation and would be interested in any advice to improve upon my configuration. Thanks very much.



Greg Funston
 
Kevin Cho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:Let's make this a little easier.

1) What benefits do you get from extending a DaoSupport class? To me there is zero benefits, and one small disadvantage of coupling your code. Or if you think there are benefits, there are other ways to get the same benefit just as easy or easier without coupling your code.

Mark



That's exactly my point as well. I find it hard to believe that anyone would not have Spring coupling. If you make your DAO spring independent and use subjprojects like Spring MVC, Spring Security, Spring Integration... then I don't think it matters if you use DAO templates or not. My point is that, if your code already use any Spring Specfic feature then I think it's great to use the templates! In my DAO, I frequently tag w/ Spring Security annotations and Spring Cache... so should I not do that because it's adding more coupling? Still, I envy your work at SpringSource... I put in my resume but nope...!!! Anyways, I'm still going to stick w/ my belief ^_^
 
Kevin Cho
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


From what I remember you want to use


create-drop will create tables and delete all tables when Spring Container is shutting down.

 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I tried that and it did not work. In the meantime I had a jar conflict and foolishly as I tried to resolve it I deleted all my jars and I had to start over. I have not been able to get back to where I was previously. I had all functionality other than DB table creation. My problem seems to be the jars. Which jars to use.

Greg Funston.
 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my current list of jars using everything but the DB jars. I need to add hibernate and mysql functionality. The jdbc mysql jar should be the one from Oracle so I believe only compatible hibernate and spring jars are required to complete the functionality I am looking for. Is there a reference for doing this somewhere which I have not found yet?

commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
jstl-1.1.2.jar
junit-4.4.jar
list.txt
log4j-1.2.15.jar
spring-2.5.2-20080220-410.jar
spring-webmvc-2.5.2-20080220-410.jar
standard-1.1.2.jar
tiles-api-2.0.5.jar
tiles-core-2.0.5.jar
tiles-jsp-2.0.5.jar

I appreciate any and all advice.

Thanks,
Greg Funston
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Cho wrote:

Mark Spritzler wrote:Let's make this a little easier.

1) What benefits do you get from extending a DaoSupport class? To me there is zero benefits, and one small disadvantage of coupling your code. Or if you think there are benefits, there are other ways to get the same benefit just as easy or easier without coupling your code.

Mark



That's exactly my point as well. I find it hard to believe that anyone would not have Spring coupling. If you make your DAO spring independent and use subjprojects like Spring MVC, Spring Security, Spring Integration... then I don't think it matters if you use DAO templates or not. My point is that, if your code already use any Spring Specfic feature then I think it's great to use the templates! In my DAO, I frequently tag w/ Spring Security annotations and Spring Cache... so should I not do that because it's adding more coupling? Still, I envy your work at SpringSource... I put in my resume but nope...!!! Anyways, I'm still going to stick w/ my belief ^_^



Yes and no. In terms of using the Spring Annotations. You only need the jars from Spring in your classpath at compile time. So those classes with Spring Annotations can still run normally without needing to use Spring at runtime. Those annotations would just be ignored. So yes, you have some coupling, but it is less coupling than extending or implementing a Spring class. It is a subtle distinction, in the depth of coupling. ;) I try to reduce the minimum.

Oh, and I think you posted something about a purist creating a bean just for a List. I wouldn't do that myself either. If I need a List I just write the code with the keyword new.

Mark
 
Greg Funston
Ranch Hand
Posts: 81
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My current state of affairs is I have this error and I have not found a solution yet. Any ideas?



reply
    Bookmark Topic Watch Topic
  • New Topic