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

Thread: integration problem with hibernate

  1. #1
    Join Date
    Sep 2009
    Posts
    12

    Default integration problem with hibernate

    Hello everyone. I am trying integration hibernate to a spring app, i have readed in internet that you nead to replace the hibernate configuration file by a bean representing the sessionfactory. I tried to do so but i have a 503 error page everytime. Here is the applicationContext.xml:
    Code:
     
    <?xml version="1.0" encoding="UTF-8"?>
     
    <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
     
        <!-- the parent application context definition for the springapp application -->
     
        <bean id="productManager" class="springapp.service.SimpleProductManager">
            <property name="productDao" ref="productDao"/>
        </bean>
     
        <bean id="productDao" class="springapp.repository.JdbcProductDao">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    <!--
        <bean id="productDao" class="springapp.repository.p2">
        </bean>
    -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
     
        <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
     
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
     
        <bean id="exampleSessionFactory"
           class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
         <property name="dataSource">
             <ref local="dataSource"/>
         </property>
         <property name="mappingLocations">
             <list>
                 <value>
                     classpath:Product.hbm.xml
                 </value>
             </list>
         </property>
     
        </bean>
     
    </beans>
    the error come from the bean exampleSessionFactory because evry time i remove it everything works fine but i cant use hibernate.

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

    Default

    1) You are using a wrong transaction manager for hibernate
    2) Post the stacktrace

    Don't crosspost!!!!
    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
    Sep 2009
    Posts
    12

    Default

    Sorry for the crosspost i didn't mean to do it.
    Yes your right i used a bad transaction manager i corrected it but i have a new problem i have the following error :No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here. to correct this error i documented myself and i found that i have to use TransactionProxyFactoryBean and that's what i did but i still have the problem here is my context file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        
        <!--bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
              p:location="/WEB-INF/jdbc.properties" />
        
        <bean id="dataSource"
              class="org.springframework.jdbc.datasource.DriverManagerDataSource"
              p:driverClassName="${jdbc.driverClassName}"
              p:url="${jdbc.url}"
              p:username="${jdbc.username}"
              p:password="${jdbc.password}" /-->
        
        <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
        <bean id="implementationProduit" class="akatsuki.domain.service.implProduit">
            <property name="productDao" ref="produitDaoHibernate"/>
        </bean>
    
        <bean id="produitDaoHibernate" class="akatsuki.domain.repositry.impl.ProduitDaoHibernate">
            <property name="sessionFactory" ref="SessionFactory"/>
        </bean>
    
        <bean id="traitement" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager" ref="TransactionManager"/>
            <property name="target" ref="produitDaoHibernate"/>
            <property name="transactionAttributes">
                <props>
                    <prop key="insert*">PROPAGATION_REQUIRED</prop>
                    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                </props>
            </property>
        </bean>
    
        <bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                    <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/cmd</prop>
                    <prop key="hibernate.connection.username">root</prop>
                    <prop key="hibernate.connection.password"></prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                </props>
            </property>
        </bean>
    
        <bean id="TransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="SessionFactory"/></property>
        </bean>
    
    
    
    
    
    
    
    </beans>
    and here is the target bean :
    Code:
    /*
     *
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package akatsuki.domain.repositry.impl;
    import akatsuki.domain.model.Produit;
    import akatsuki.domain.repositry.ProductDao;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    /**
     *
     * @author hamza
     */
    public class ProduitDaoHibernate implements ProductDao{
        private SessionFactory sessionFactory;
        public List<Produit> getProduits() {
            List<Produit>lst;
            lst=new ArrayList<Produit>();
            /*Produit p = new Produit();
            p.setId(11);
            p.setNom("Giggs");
            lst.add(p);*/
            Session session = sessionFactory.getCurrentSession();
            Query req = session.createQuery("select id,nom from Produit");
            FileWriter fw=null;
            try{
            fw = new FileWriter("D:\\n.txt");
            fw.write("l");
            fw.flush();
            fw.close();
            }catch(Exception e){
    
            }
            int i=0;
            for(Iterator it = req.iterate();it.hasNext();){
                Produit p = new Produit();
                Object[]ligne=(Object[])it.next();
                p.setId(Integer.parseInt(ligne[0].toString()));
                p.setNom(String.valueOf(ligne[1]));
                lst.add(p);
                try{
                fw.write(String.valueOf(i));
                fw.flush();
                fw.close();
                }
                catch(Exception e){
    
                }
            }
    
            return lst;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        
    
    }
    Correct im if i'am wrong the TransactionProxyFactoryBean serves to wrap a bean so that he can use transaction methods taht's why i specified in the target bean my dao bean. and for the transactionattributes when i do something like that insert* it means every sql statement who begin with insert to be propageted and everything else * to be read-only?

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

    Default

    I suggest the spring reference guide, especially the part about transactions. Next to that you don't have to use a TransactionalProxyFactoryBean anymore there are better ways.
    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

  5. #5
    Join Date
    Sep 2009
    Posts
    12

    Default

    I'am reading the reference guide but still i want to know where the error is.It will help me a lot.

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

    Default

    Can be a couple of issues.

    1) You aren't using the proxied instance
    2) You aren't using an ApplicationContext but a BeanFactory
    3) Your undertanding of the transactional rules is wrong (it matched METHOD names it has nothing to do with sql).
    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

  7. #7
    Join Date
    Sep 2009
    Posts
    12

    Default

    your right for third point that's why i questioned you about it it's the name of the method that must begin with insert.but for the two other point's i dont understand you. specially for the first one you say that i dont use the proxied bean wich is produitDaoHibernate. Im using it(at least that's what i think) when i am affecting it to my service layer bean(implementationProduit). Sorry if this question are too noob but i am an asp.net developper and im stuck with an existing spring project where i have to integrate hibernate to it, so i had to learn spring and hibernate from scratch.
    Last edited by united4life; Sep 27th, 2009 at 03:42 PM.

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

    Default

    I suggest you take a look at the sample project.
    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

  9. #9
    Join Date
    Sep 2009
    Posts
    12

    Default

    can you tell me what do you mean by I'm not using the proxied bean. Because the source is at work and I don't have it with me I can only use it when I'm in work so to win some time could you clarify this point. Normally when in my dao I call the getCurrentSession() Spring must create a new session because normally if my config is correct my Dao bean should be wrapped by the transactionProxyBean but it's not so when you say I'm not using the proxied bean it makes sense with the error. So please could you clarify this point. I've read the petClinic example and I have understand it but I still didn't found the solution.

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

    Default

    I've read the petClinic example and I have understand it but I still didn't found the solution.
    If you did you would have the solution .

    Only declaring a TransactionProxyFactoryBean doesn't do anything you have to use that instead of the actual dao. That is also the reason that it isn't recommended anymore because it is verbose and quite easy to forget to use it instead of the unproxied class. (I suggest chapter 6 and proxy based aop of that chapter).

    Either replace it with the easier and more powerful aop:config and tx:advice (again check the reference guide) or make your actual dao an anonymous inner class declaration of the TPFB.

    Code:
    <bean id="produitDaoHibernate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	<property name="transactionManager" ref="TransactionManager"/>
    	<property name="target">
    		<bean class="akatsuki.domain.repositry.impl.ProduitDaoHibernate">
    			<property name="sessionFactory" ref="SessionFactory"/>
    		</bean>
    	</property>
    	<property name="transactionAttributes">
    		<props>
    			<prop key="insert*">PROPAGATION_REQUIRED</prop>
    			<prop key="update*">PROPAGATION_REQUIRED</prop>
    			<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    		</props>
    	</property>
    </bean>
    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

Posting Permissions

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