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

Thread: updating persistent entities (spring newbie)

  1. #1

    Default updating persistent entities (spring newbie)

    I'm trying to update a persistent pojo, by first loading the object, then changing one of it's properties. The update attribute for this property in the mapping is set to "true." However, I'm getting an exception saying the session has been closed. Do I have to somehow force the session to stay open? Any advice would be greatly appreciated.
    ex.

    Employee emp = (Employee)getHibernateTemplate().load(Employee.cla ss,new Integer(id));

    //update
    emp.setStatus("inactive");

  2. #2

    Default

    The only way I was able to get it to work was to explicitly get the session, load the entity with the session, change the state of the pojo, call flush, then explicitly close the session.

    It works, but I guess I'm wondering if there's an easier way to do it with Spring api. Thanks.

  3. #3
    Join Date
    Jun 2005
    Posts
    19

    Default

    Be sure you have a transactionManager bean configured

    Code:
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory"/></property>
    	</bean>
    Also create a transactionTemplate if you want to do programmatic transaction management

    Code:
    	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    		<property name="transactionManager"><ref bean="transactionManager"/></property>
    	</bean>
    Once this is created, you can use the transactionTemplate bean like so:

    Code:
    Object result = transactionTemplate.execute&#40;new TransactionCallback&#40;&#41; &#123;
        public Object doInTransaction&#40;TransactionStatus status&#41; &#123;
          Employee emp = &#40;Employee&#41;getHibernateTemplate&#40;&#41;.load&#40;Employee.class,new Integer&#40;id&#41;&#41;;
    
          //update
          emp.setStatus&#40;"inactive"&#41;;
    
          return null;
        &#125;
    &#125;&#41;;
    Since the transaction template uses the HibernateTransactionManager, all executions will automatically be provided the Hibernate session, so you don't have to do it yourself.

    Read the docs that cover this thoroughly:

    http://static.springframework.org/sp...n.html#d0e4797

    You should also grab a book or three on Spring, along with the Hibernate book (supports the developers, too).

    (Disclaimer: I'm writing this from memory and haven't tested the code shown above.)

  4. #4

    Default

    Thanks alot. Can I do the same if I get a list of results and iterate through the results, updating each entity? In other words, when I change the state of each iterative entity, will state of the entity be synchronized with the db? Thanks again.

  5. #5
    Join Date
    Jun 2005
    Posts
    19

    Default

    Absolutely. That's what is so great about Hibernate. You don't think about "working with the database." You just work with Java objects that are automatically persisted.

  6. #6

    Default

    Thanks alot for your help. Everything you pointed me too is working. I agree, it's nice to just be able to think about object state.

  7. #7

    Default

    One more thing. Will the transactionManager automatically handle rollback?

  8. #8

    Default

    out of curiosity, why aren't you doing a getHibernateTemplate().update(emp); ?

  9. #9

    Default

    No. I'm first loading the entity, then simply changing the value of a property within the entity. The entity then gets synchronized with the database. I'm using the TransactionTemplate. See example above.

  10. #10
    Join Date
    Jun 2005
    Posts
    19

    Default

    Quote Originally Posted by graeder
    One more thing. Will the transactionManager automatically handle rollback?
    Yes. Take a look at the source code:

    Code:
    	/**
    	 * Execute the action specified by the given callback object within a transaction.
    	 * <p>Allows for returning a result object created within the transaction, i.e.
    	 * a domain object or a collection of domain objects. A RuntimeException thrown
    	 * by the callback is treated as application exception that enforces a rollback.
    	 * An exception gets propagated to the caller of the template.
    	 * @param action callback object that specifies the transactional action
    	 * @return a result object returned by the callback, or null
    	 * @throws TransactionException in case of initialization, rollback, or system errors
    	 */
    	public Object execute&#40;TransactionCallback action&#41; throws TransactionException &#123;
    		TransactionStatus status = this.transactionManager.getTransaction&#40;this&#41;;
    		Object result = null;
    		try &#123;
    			result = action.doInTransaction&#40;status&#41;;
    		&#125;
    		catch &#40;RuntimeException ex&#41; &#123;
    			// transactional code threw application exception -> rollback
    			rollbackOnException&#40;status, ex&#41;;
    			throw ex;
    		&#125;
    		catch &#40;Error err&#41; &#123;
    			// transactional code threw error -> rollback
    			rollbackOnException&#40;status, err&#41;;
    			throw err;
    		&#125;
    		this.transactionManager.commit&#40;status&#41;;
    		return result;
    	&#125;

Similar Threads

  1. Spring MVC Web Framework versus Struts
    By biguniverse in forum Web Flow
    Replies: 27
    Last Post: Aug 29th, 2012, 03:57 AM
  2. Persistent Job using Quartz and Spring
    By MmarcoM in forum Container
    Replies: 8
    Last Post: Dec 16th, 2008, 03:57 AM
  3. Newbie: moving from avalon to spring
    By Leonets in forum Container
    Replies: 5
    Last Post: Sep 2nd, 2005, 08:45 AM
  4. Newbie Question - The Ideal Spring Solution
    By conorp in forum Architecture
    Replies: 3
    Last Post: Aug 23rd, 2005, 03:22 AM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

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