facebook

object are not save without any exception ????

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #254210 Reply

    Sword
    Member

    Hi,

    I build a simple example with generated objects (DAO’s, Factory etc …) by MyEclipse Struts, Hibernate and Spring Wizards.
    My problem is that the objects are never saved in database without any exception ???
    Here the context (Eclipse 3.1.2 / MyEclipse 4.1.1 / Hib 3 / Spring 1.2)

    Action :

    
            WebApplicationContext springContext = (WebApplicationContext)this.getServlet().getServletContext().getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
    
            PaysDAO pDAO = PaysDAO.getFromApplicationContext(springContext);
            Pays pays = new Pays();
            pays.setCodeIso("uk");
            pays.setLibelle("united kingdom");
            pDAO.save(pays);

    spring config.xml

        
        <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
        </bean>
            <bean id="PaysDAO" class="com.test.spring.persistance.entity.PaysDAO">
            <property name="sessionFactory">
                <ref bean="hibernateSessionFactory" />
            </property>
        </bean>

    When I request the database, I don’t find any record in the table !!!
    How can I commit ?
    How can i manage the transaction (only with this configuration without AOP addon) ?
    I don’t see what I missed (the MyEclipse tutorial is empty !!!)

    Jean-Louis

    #254243 Reply

    Haris Peco
    Member

    Jean-Louis,

    You have do dml (insert,update,save) in transaction.With spring you can do it programmatic or declarative.

    This is example for programmatic transaction :

    1) add bean in your spring context

    
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionId" />
    </bean>
    

    2) call your method in transaction

    
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    PlatformTransactionManager txManager = (PlatformTransactionManager) springContext.getBean("txManager");
    TransactionStatus status = txManager.getTransaction(def);
    try {
        // your dml methods
        txManager.commit(status);
    } catch (Exception ex) {
        txManager.rollback(status);
        ex.printStackTrace();
    }
    

    This is hard and tiring, of course and spring have declarative development.
    this is simple example (you have to exchange myBeanDAO1, myBeanDAO with your
    bena definition).When you add this all your DAO’s methods will be in transaction and
    your example will save data in database.
    You can see spring documentation for details and I will se if we can add transaction managment to generate DAO wizard.

    
    <bean id="hibInterceptor"
            class="org.springframework.orm.hibernate3.HibernateInterceptor">
            <property name="sessionFactory">
                <ref local="sessionId" />
            </property>
        </bean>
        <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionId" />
        </bean>
        <bean id="matchAllWithPropReq"
            class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
            <property name="transactionAttribute"
                value="PROPAGATION_REQUIRED" />
        </bean>
        <bean id="matchAllTxInterceptor"
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager" ref="txManager" />
            <property name="transactionAttributeSource"
                ref="matchAllWithPropReq" />
        </bean>
        
        <bean id="autoProxy" 
            class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="proxyTargetClass" value="true"/>
            
            <property name="interceptorNames">
                <list>
                    <value>matchAllTxInterceptor</value>
                    <value>hibInterceptor</value>
                </list>
            </property>
            <property name="beanNames">
                <list>
                    <value>myBeanDAO1</value>
                    <value>myBeanDAO2</value>                
                </list>
            </property>
            
        </bean>
    

    Best regards

    #254269 Reply

    Sword
    Member

    Thanks for your answer,
    but when I see the quantity of lines that we must write, I don’t see the interest of the wizard and the utility of
    generated objects especially save/delete/udate methods in DAO’s ?
    Is there some templates for generating transactionnal objects and configuration files ?
    Best regards,
    Jean-Louis

    #254271 Reply

    Haris Peco
    Member

    Jean-Louis,

    It’s impossible for us that decide what is your transaction.It can be:
    ‘update bean A’
    ‘save bean B’
    ‘compute some totals and update bean C’

    We can’t know what is it
    You have to decide this and set transaction handling in your spring context or programmatic.
    If you use spring you will want declarative handling.
    We can make your job easier with some sample, but complete transaction handling is your part.
    You can copy last part in your spring context , but it work just for single methods.It mean your save, merge etc will be in transaction, but it will be commited after every method – you can do same if you set next property in hibernate.cfg.xml
    <property name=”hibernate.connection.autocommit”>true</property>
    but it you want not commiting every sql command.
    If you want do more methods in same transaction you have to set spring context (or do it programatic)
    However , this is just example, but when you set transaction handling in spring correct once, then you call DAO methods how you expect and don’t care about transaction anymore.

    It’s possible that we add base transaction handling for you in next releases.

    Best regards

    #257615 Reply

    iaga
    Member

    Hello, i am a new user, new to hibernate & spring technologies! I have encountered the following problem while developing a web application running on tomcat.
    I have used the solution proposed by support-snpe. Everything is working ok when i am using DAO objects in an applet that is running on my machine directly.
    I have the same problem of Jean Luis when i try to make everything work through tomcat (i.e. with a servlet that uses DAOs).

    I attach some configuration info & some code.

    THE APPLICATION CONTEXT

    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    
        <bean id="MySessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation">
                <value>hibernate.cfg.xml</value>
            </property>
        </bean>
        <bean id="CustomersDAO"
            class="com.arke.dominodb.dao.CustomersDAO">
            <property name="sessionFactory">
                <ref bean="MySessionFactory" />
            </property>
        </bean>
        <bean id="hibInterceptor"
            class="org.springframework.orm.hibernate3.HibernateInterceptor">
            <property name="sessionFactory">
                <ref local="MySessionFactory" />
            </property>
        </bean>
        <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="MySessionFactory" />
        </bean>
        <bean id="matchAllWithPropReq"
            class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource">
            <property name="transactionAttribute"
                value="PROPAGATION_REQUIRED" />
        </bean>
        <bean id="matchAllTxInterceptor"
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager" ref="txManager" />
            <property name="transactionAttributeSource"
                ref="matchAllWithPropReq" />
        </bean>
    
        <bean id="autoProxy"
            class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="proxyTargetClass" value="true" />
    
            <property name="interceptorNames">
                <list>
                    <value>matchAllTxInterceptor</value>
                    <value>hibInterceptor</value>
                </list>
            </property>
            <property name="beanNames">
                <list>
                    <value>CustomersDAO</value>
                </list>
            </property>
    
        </bean>
    
    </beans>
    

    HIBERNATE CONFIGURATION FILE

    
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration>
    
        <session-factory>
            <property name="connection.username">gdl</property>
            <property name="connection.url">
                jdbc:sqlserver://XXXXX:1433
            </property>
            <property name="dialect">
                org.hibernate.dialect.SQLServerDialect
            </property>
            <property name="myeclipse.connection.profile">foo2</property>
            <property name="connection.password">xxx</property>
            <property name="connection.driver_class">
                com.microsoft.sqlserver.jdbc.SQLServerDriver
            </property>
            <property name="hibernate.connection.autocommit">true</property> 
            <mapping resource="com/society/dominodb/dao/Customers.hbm.xml" />
    
        </session-factory>
    
    </hibernate-configuration>
    

    THE SERVLET

    
    
    package com.society.dominodb.servlet;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.transaction.support.*;
    import org.springframework.transaction.*;
    import com.society.dominodb.dao.*;
    import java.util.*;
    import java.io.*;
    
    public class DbAccessServlet extends HttpServlet {
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
    
    CustomersDAO customersDAO = CustomersDAO
                    .getFromApplicationContext(appContext);
    
                Customers newCustomer = new Customers();
                newCustomer.setCustomerId(request.getParameter("CustomerID"));
                newCustomer.setAddress(request.getParameter("Address"));
    
    
                customersDAO.save(newCustomer);
    
    
        }
    
    }
    

    thank you for your support, i hope that i’m not OT 😳

    #258433 Reply

    leepd
    Member

    or , you can add the Hibernate transaction code in the Dao save function , I try this , it is work 😉

    Transaction tx = getSession().getTransaction();
    tx.begin();
    getSession().save(transientInstance);
    log.debug(“save successful”);
    // getSession().flush();
    tx.commit();

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: object are not save without any exception ????

You must be logged in to post in the forum log in