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
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.
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
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
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 ^_^
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|