Announcement Announcement Module
Collapse
No announcement yet.
Hibernate - saveOrUpdate problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate - saveOrUpdate problem

    Hy,

    I'm using Hibernate 2.1.6 and Spring 1.1.0. whit HibernateInterceptor in my HibernateDaoSupport class.

    Every time that I try to update a saved object, it just doesn't work.
    I'm trying to do this after a post in a web app.
    The funny things is : When I try a similar action outside the webserver the update works.

    I think i'm using rigth the unsaved-value attribute:
    ...
    <hibernate-mapping>
    <class
    name="hotel.domain.Customer"
    table="CUSTOMER"
    dynamic-update="false"
    dynamic-insert="false"
    >

    <id
    name="customerId"
    column="ID_CUSTOMER"
    type="long"
    unsaved-value="0"
    >
    <generator class="native">
    </generator>
    </id>
    ...

    What could be wrong ?

    []'s
    Diego[/quote]

  • #2
    Every time that I try to update a saved object, it just doesn't work.
    Could you give more explanations: log, stacktrace, configuration, code...

    Comment


    • #3
      Could you give more explanations: log, stacktrace, configuration, code...
      This is my controller that handle the post of a Customer update
      Code:
      public class CustomerFormController extends SimpleFormController
      &#123;
          private HotelFacade hotelFacade;
          private final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
          
          public void setHotelFacade&#40;HotelFacade hotelFacade&#41;
          &#123; this.hotelFacade = hotelFacade;&#125;
          
          protected ModelAndView onSubmit&#40;HttpServletRequest request,
                  HttpServletResponse response, Object command, BindException errors&#41;  throws Exception&#123;
              Customer customer = &#40;Customer&#41;command;
              hotelFacade.saveCustomer&#40;customer&#41;;                        
              return new ModelAndView&#40;"show_customer","customer", customer&#41;;                
          &#125;
      &#125;
      The HotelFacade implementation:
      Code:
      public class HotelFacadeImpl implements HotelFacade
      &#123;
          CustomerDao customerDao;
      
          public void setCustomerDao&#40;CustomerDao customerDao&#41;  &#123;
             this.customerDao = customerDao;
          &#125;
      
          public void saveCustomer&#40;Customer customer&#41; &#123;
              customerDao.saveCustomer&#40;customer&#41;;
          &#125;
      ....
      Customer DAO

      Code:
      public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao
      &#123;           
          public void saveCustomer&#40;Customer customer&#41; &#123;
      		try &#123;
      			Session session = SessionFactoryUtils.getSession&#40;getSessionFactory&#40;&#41;, true&#41;;
      			session.saveOrUpdate&#40;customer&#41;;						
      		&#125; catch &#40;HibernateException ex&#41; &#123;
      			throw SessionFactoryUtils.convertHibernateAccessException&#40;ex&#41;;
      		&#125;
          &#125; ...
      Nothing happens when session.saveOrUpdate(customer) is executed.

      Comment


      • #4
        You could use:
        Code:
        public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao &#123;            
          public void saveCustomer&#40;Customer customer&#41; throws DataAccessException &#123; 
            getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;customer&#41;;                   
          &#125; ...
        Are you using any transactions?

        Comment


        • #5
          I'm not using transaction.
          Anyway, I've put a flush after the saveOrUpdate and them everything worked fine:
          Code:
          public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao
          &#123;           
              public void saveCustomer&#40;Customer customer&#41; &#123;
                try &#123;
                   Session session = SessionFactoryUtils.getSession&#40;getSessionFactory&#40;&#41;, true&#41;;
          		   session.saveOrUpdate&#40;customer&#41;;		
          		   session.flush&#40;&#41;;               
                &#125; catch &#40;HibernateException ex&#41; &#123;
                   throw SessionFactoryUtils.convertHibernateAccessException&#40;ex&#41;;
                &#125;
              &#125; ...

          Comment


          • #6
            Using Spring Declarative Transaction Demarcation will have the same effect. And you no longer need to "manually" flush the session.

            Comment

            Working...
            X