Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: what is the difference between hibernate template and hibernate dao support.

  1. #1
    Join Date
    Oct 2009
    Posts
    10

    Thumbs up what is the difference between hibernate template and hibernate dao support.

    HI All,

    hibernate dao support provides a in built method getHibernateTemplate.This is only the advantage of using hibernate dao support other than hibernate template.Please suggest.

    When we need to go for a hibernate template and hibernate dao support.

    Regards,
    sameer.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    HibernateDaoSupport gives you easy access to a HibernateTemplate. But the usage of both isn't recommended anymore since about 2007 so simply use a plain SessionFactory...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    I am agree with Marten

    More information about this here
    So should you still use Spring's HibernateTemplate and/or JpaTemplate??
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  4. #4
    Join Date
    Jul 2011
    Posts
    11

    Default

    Quote Originally Posted by dr_pompeii View Post
    I am agree with Marten

    More information about this here
    So should you still use Spring's HibernateTemplate and/or JpaTemplate??
    That post is from 2007 , is it still relevant ? Things may have change since then ?!
    I find HibernateTemplate very convenient , I just define and reconfige 3 beans and get this HibernateTemplate ready for use , with less work.


    Code:
    	private HibernateTemplate hibernateTemplate;
    
    	@Autowired
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    	}
    
    	@Override
    	@Transactional(readOnly = false)
    	public void saveUser(User user) {
    		hibernateTemplate.saveOrUpdate(user);
    	}
    
    	@Override
    	@Transactional(readOnly = true)
    	public User getUser(String email) {
    		return  (User) hibernateTemplate.find("from User where email =" + "'" + email + "'").get(0);
    	}
    why isn't it recommended ?

  5. #5
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,791

    Default

    Hello

    That post is from 2007 , is it still relevant ? Things may have change since then ?!
    Yes, relevant until now, why?. Due the follow

    The point is with HibernateTemplate makes Spring and Hibernate more coupled about dependencies about Java code (API level).

    I used to work years ago with this template and really does and done very well its job, but ...

    why isn't it recommended ?
    Therefore the actual solution (since 2007) practically let you work in a 100% with pure Hibernate API breaking this level about coupling between these frameworks, some differences about synthax between pure Hibernate and Spring's HibernateTemplate exits

    With the actual solution is only necessary add the Spring's annotation called @Repository to your DAO class and some xml configuration like, scanning packages and to recognize this annotation and add an Exception Translator to let Spring Translate the own Hibernate's type Exceptions to the Spring DataAccessException

    HTH
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  6. #6
    Join Date
    Jul 2011
    Posts
    11

    Default

    Hi ,

    I still feel something is missing , from easy work , I need to write a lot more .

    Where I can learn how to implements right pure Hibernate API , Resource management , Transaction management & and Error handling ?
    I'm planning build a site with at least x000 ( and high growing potential) users online which will using this Hibernate API , using MySql . How to handle massive connections / SessionFactory usages?

    When implements pure Hibernate API using Spring , where Spring comes into the picture , I mean what's Spring's job here? Connection closed? Threads?

    (Sorry if it's basic Hibernate knowledge )

    Thanks and Regards ,

    Zakos

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You don't need to write more (not sure where you get that idea). How to implement plain hibernate api based dao's is explained in the spring reference guide.

    The HibernateTemplate isn't needed anymore since hibernate 3.0.1, since that release it became easier to plugin nicely to hibernate, before that spring needed some trickery, proxing and hackery to manage transactions, thread bound sessions and exception translation. Now with the newer versions of hibernate that isn't needed anymore, you still get all the nice stuff exception translation, session management etc. but without using any spring specific classes. Which imho is really nice it makes the use of spring even less obtrusive.

    The code you posted would be more like

    Code:
    	private SessionFactory sessionFactory;
    
    	@Autowired
    	public void setSessionFactory(final SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    
    	@Override
    	@Transactional(readOnly = false)
    	public void saveUser(final User user) {
    		sessionFactory.getCurrentSession().saveOrUpdate(user);
    	}
    
    	@Override
    	@Transactional(readOnly = true)
    	public User getUser(final String email) {
    		final String query = "from User where email=:email";
    		Query query = sessionFactory.getCurrentSession().createQuery(query);
    		query.setParameter("email", email);
    		List<User> results = query.list();
    		if (!results.isEmpty()) {
    			return results.get(0);
    		}
    		return null;
    	}
    If fixed your getUser method because that was flawed. NEVER NEVER NEVER (did I make myself clear) use string contact to include parameters into your query ALWAYS use placeholders, unless you want to be vulnerable to sql injection attacks! ALso you didn't check for any result which could potentially lead to exceptions.
    Last edited by Marten Deinum; Jul 11th, 2011 at 01:41 AM. Reason: Fixed getUser method
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Jul 2011
    Posts
    11

    Default

    1. Clear Sir.

    2. Does the spring reference guide is this ? 13.3 Hiberante ? Didn't find Hibernate annotation example there.

    I use

    Code:
    	     <bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	    <property name="sessionFactory"> 
    	    	<ref bean="mySessionFactory" /> 
    	     </property>
         </bean>
    	
    	<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
     		<property name="packagesToScan" value="com.domain" />
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
    				<prop key="hibernate.show_sql">false</prop>
    				<prop key="hibernate.hbm2ddl.auto"></prop>
    			</props>
    		</property>
    	</bean>
    3. Is the responsibility table here , is still apply on pure Hibernate API ?


    Thanks , again.

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You should use 2 to configure your sessionfactory that doesn't change you only don't use HibernateTemplate anymore... The JDBC chapter has nothing to do with hibernate so don't see your question...But spring still manages your resources (connection, session, transaction) that doesn't change... As I tried to explain in the previous posts the only thing that changes is that you don't use HibernateTemplate anymore everything else remains the same!
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Jul 2011
    Posts
    11

    Default

    But spring still manages your resources (connection, session, transaction) that doesn't change...
    That's what I wanted to ask through the JDBC table example, well got it . I'll try to do the changes you suggests. ( that's where the idea for "more code" that I meant earlier , if it wasn't like that )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •